Skip to content

Commit

Permalink
ref(server): Emit metric counting accepted connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Nov 7, 2024
1 parent d50cb99 commit eae8964
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion relay-server/src/services/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn build_keepalive(config: &Config) -> Option<TcpKeepalive> {
pub struct KeepAliveAcceptor(Option<TcpKeepalive>);

impl KeepAliveAcceptor {
/// Create a new acceptor that sets TCP_NODELAY and keep-alive.
/// Create a new acceptor that sets `TCP_NODELAY` and keep-alive.
pub fn new(config: &Config) -> Self {
Self(build_keepalive(config))
}
Expand All @@ -153,17 +153,28 @@ impl<S> Accept<TcpStream, S> for KeepAliveAcceptor {
type Future = std::future::Ready<io::Result<(Self::Stream, Self::Service)>>;

fn accept(&self, stream: TcpStream, service: S) -> Self::Future {
let mut keepalive = "ok";
let mut nodelay = "ok";

if let Self(Some(ref tcp_keepalive)) = self {
let sock_ref = socket2::SockRef::from(&stream);
if let Err(e) = sock_ref.set_tcp_keepalive(tcp_keepalive) {
relay_log::trace!("error trying to set TCP keepalive: {e}");
keepalive = "error";
}
}

if let Err(e) = stream.set_nodelay(true) {
relay_log::trace!("failed to set TCP_NODELAY: {e}");
nodelay = "error";
}

relay_statsd::metric!(
counter(RelayCounters::ServerSocketAccept) += 1,
keepalive = keepalive,
nodelay = nodelay
);

std::future::ready(Ok((stream, service)))
}
}
Expand All @@ -180,6 +191,7 @@ fn serve(listener: TcpListener, app: App, config: Arc<Config>) {
.http1()
.timer(TokioTimer::new())
.half_close(true)
.keep_alive(true)
.header_read_timeout(CLIENT_HEADER_TIMEOUT)
.writev(true);

Expand Down
3 changes: 3 additions & 0 deletions relay-server/src/statsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ pub enum RelayCounters {
BucketsDropped,
/// Incremented every time a segment exceeds the expected limit.
ReplayExceededSegmentLimit,
/// Incremented every time the server accepts a new connection.
ServerSocketAccept,
}

impl CounterMetric for RelayCounters {
Expand Down Expand Up @@ -904,6 +906,7 @@ impl CounterMetric for RelayCounters {
RelayCounters::ProjectStateFlushMetricsNoProject => "project_state.metrics.no_project",
RelayCounters::BucketsDropped => "metrics.buckets.dropped",
RelayCounters::ReplayExceededSegmentLimit => "replay.segment_limit_exceeded",
RelayCounters::ServerSocketAccept => "server.http.accepted",
}
}
}
Expand Down

0 comments on commit eae8964

Please sign in to comment.