Fix smg_http_requests_total semantics (#15655)
This commit is contained in:
@@ -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",
|
||||
@@ -663,9 +667,6 @@ 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)
|
||||
@@ -673,18 +674,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[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.
|
||||
|
||||
@@ -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",
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user