Skip to content

Commit d23c39a

Browse files
authored
ref(server): Emit a metric for all currently open connections (#4231)
1 parent a7d9096 commit d23c39a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

relay-server/src/services/server.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::middlewares::{
2424
RequestDecompressionLayer, SentryHttpLayer,
2525
};
2626
use crate::service::ServiceState;
27-
use crate::statsd::RelayCounters;
27+
use crate::statsd::{RelayCounters, RelayGauges};
2828

2929
/// Set the number of keep-alive retransmissions to be carried out before declaring that remote end
3030
/// is not available.
@@ -204,6 +204,11 @@ fn serve(listener: TcpListener, app: App, config: Arc<Config>) {
204204
let service = ServiceExt::<Request>::into_make_service_with_connect_info::<SocketAddr>(app);
205205
tokio::spawn(server.serve(service));
206206

207+
tokio::spawn(emit_active_connections_metric(
208+
config.metrics_periodic_interval(),
209+
handle.clone(),
210+
));
211+
207212
tokio::spawn(async move {
208213
let Shutdown { timeout } = Controller::shutdown_handle().notified().await;
209214
relay_log::info!("Shutting down HTTP server");
@@ -255,3 +260,16 @@ impl Service for HttpServer {
255260
serve(listener, app, config);
256261
}
257262
}
263+
264+
async fn emit_active_connections_metric(interval: Option<Duration>, handle: Handle) {
265+
let Some(mut ticker) = interval.map(tokio::time::interval) else {
266+
return;
267+
};
268+
269+
loop {
270+
ticker.tick().await;
271+
relay_statsd::metric!(
272+
gauge(RelayGauges::ServerActiveConnections) = handle.connection_count() as u64
273+
);
274+
}
275+
}

relay-server/src/statsd.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub enum RelayGauges {
4646
RedisPoolIdleConnections,
4747
/// The number of notifications in the broadcast channel of the project cache.
4848
ProjectCacheNotificationChannel,
49+
/// Exposes the amount of currently open and handled connections by the server.
50+
ServerActiveConnections,
4951
}
5052

5153
impl GaugeMetric for RelayGauges {
@@ -66,6 +68,7 @@ impl GaugeMetric for RelayGauges {
6668
RelayGauges::ProjectCacheNotificationChannel => {
6769
"project_cache.notification_channel.size"
6870
}
71+
RelayGauges::ServerActiveConnections => "server.http.connections",
6972
}
7073
}
7174
}

0 commit comments

Comments
 (0)