Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(server): Emit metric counting accepted connections #4230

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading