From 786888d93ff2c5acb25fe8b44a4a9777411f8a57 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 5 Jun 2025 21:28:11 +0300 Subject: [PATCH] Instead of a fixed TCP port for metrics, listen on a unix domain socket That avoids clashes if you run two computes at the same time. More secure too. We might want to have a TCP port in the long run, but this is less trouble for now. To see the metrics with curl you can use: curl --unix-socket .neon/endpoints/ep-main/pgdata/.metrics.socket http://localhost/metrics --- .../src/worker_process/metrics_exporter.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pgxn/neon/communicator/src/worker_process/metrics_exporter.rs b/pgxn/neon/communicator/src/worker_process/metrics_exporter.rs index b1042b928c..ff7aa20810 100644 --- a/pgxn/neon/communicator/src/worker_process/metrics_exporter.rs +++ b/pgxn/neon/communicator/src/worker_process/metrics_exporter.rs @@ -12,6 +12,10 @@ use metrics; use metrics::proto::MetricFamily; use metrics::{Encoder, TextEncoder}; +use std::path::PathBuf; + +use tokio::net::UnixListener; + use crate::worker_process::main_loop::CommunicatorWorkerProcessStruct; impl<'a> CommunicatorWorkerProcessStruct<'a> { @@ -22,10 +26,10 @@ impl<'a> CommunicatorWorkerProcessStruct<'a> { .route("/dump_cache_map", get(dump_cache_map)) .with_state(self); - // TODO: make configurable. Or listen on unix domain socket? - let listener = tokio::net::TcpListener::bind("127.0.0.1:9090") - .await - .unwrap(); + // Listen on unix domain socket, in the data directory. That should be unique. + let path = PathBuf::from(".metrics.socket"); + + let listener = UnixListener::bind(path.clone()).unwrap(); tokio::spawn(async { tracing::info!("metrics listener spawned");