Skip to content
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
19 changes: 4 additions & 15 deletions sgl-model-gateway/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ impl<B> OnRequest<B> for RequestLogger {
span.record("request_id", request_id.0.as_str());
}

let method = method_to_static_str(request.method().as_str());
let path = normalize_path_for_metrics(request.uri().path());
Metrics::record_http_request(method, &path);

// Log the request start
info!(
target: "sgl_model_gateway::request",
Expand Down Expand Up @@ -663,28 +667,13 @@ where
let response = result?;

let duration = start.elapsed();
let status_class = status_to_class(response.status().as_u16());

Metrics::record_http_request(method, &path, status_class);
Metrics::record_http_duration(method, &path, duration);

Ok(response)
})
}
}

#[inline]
fn status_to_class(status: u16) -> &'static str {
match status {
100..=199 => "1xx",
200..=299 => "2xx",
300..=399 => "3xx",
400..=499 => "4xx",
500..=599 => "5xx",
_ => "unknown",
}
}

/// Normalize path for metrics to avoid high cardinality.
/// Replaces dynamic segments (IDs, UUIDs) with `{id}` placeholder.
/// Only allocates when normalization is needed; uses single-pass with byte offsets.
Expand Down
9 changes: 4 additions & 5 deletions sgl-model-gateway/src/observability/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn init_metrics() {
// Layer 1: HTTP metrics
describe_counter!(
"smg_http_requests_total",
"Total HTTP requests by method, path, and status"
"Total HTTP requests by method and path"
);
describe_histogram!(
"smg_http_request_duration_seconds",
Expand Down Expand Up @@ -413,14 +413,13 @@ pub struct StreamingMetricsParams<'a> {

impl Metrics {
/// Record an HTTP request.
/// For best performance, pass static strings (use `method_to_static_str` for method,
/// `status_to_class` for status_class returns static, and cache normalized paths).
pub fn record_http_request(method: &'static str, path: &str, status_class: &'static str) {
/// Here we want a metric to directly reflect user's experience ("I am sending a request")
/// when viewing the router as a blackbox, and is bumped immediately when the request arrives.
pub fn record_http_request(method: &'static str, path: &str) {
counter!(
"smg_http_requests_total",
"method" => method,
"path" => path.to_string(),
"status" => status_class
)
.increment(1);
}
Expand Down
Loading