[Security][Rust Frontend] Normalize HTTP method labels in metrics (#56058)
Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: OpenAI Codex <noreply@openai.com> Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
co-authored by
Bugen Zhao
OpenAI Codex
parent
41cffa9af0
commit
3735c2d5f5
@@ -4,6 +4,7 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use axum::extract::{MatchedPath, Request};
|
||||
use axum::http::Method;
|
||||
use axum::middleware::Next;
|
||||
use axum::response::Response;
|
||||
use vllm_metrics::{HttpHandlerLabels, HttpRequestLabels, METRICS};
|
||||
@@ -29,10 +30,29 @@ const EXCLUDED_HANDLERS: &[&str] = &[
|
||||
"/is_sleeping",
|
||||
];
|
||||
|
||||
/// Map the request method to a bounded Prometheus label.
|
||||
///
|
||||
/// HTTP parsers accept arbitrary method tokens. Recording the raw token as a
|
||||
/// label would let unique values create unbounded time series.
|
||||
pub(crate) fn http_metrics_method(method: &Method) -> &str {
|
||||
match *method {
|
||||
Method::GET
|
||||
| Method::POST
|
||||
| Method::PUT
|
||||
| Method::PATCH
|
||||
| Method::DELETE
|
||||
| Method::HEAD
|
||||
| Method::OPTIONS
|
||||
| Method::CONNECT
|
||||
| Method::TRACE => method.as_str(),
|
||||
_ => "other",
|
||||
}
|
||||
}
|
||||
|
||||
/// Record API-server HTTP metrics with Python-compatible
|
||||
/// (`PrometheusFastApiInstrumentator` style) family names and labels.
|
||||
pub async fn track_http_metrics(req: Request, next: Next) -> Response {
|
||||
let method = req.method().as_str().to_string();
|
||||
let method = http_metrics_method(req.method()).to_string();
|
||||
let handler = req
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
@@ -80,3 +100,33 @@ fn status_group(status: u16) -> &'static str {
|
||||
_ => "unknown",
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn known_methods_keep_their_names() {
|
||||
for (method, expected) in [
|
||||
(Method::GET, "GET"),
|
||||
(Method::POST, "POST"),
|
||||
(Method::PUT, "PUT"),
|
||||
(Method::PATCH, "PATCH"),
|
||||
(Method::DELETE, "DELETE"),
|
||||
(Method::HEAD, "HEAD"),
|
||||
(Method::OPTIONS, "OPTIONS"),
|
||||
(Method::CONNECT, "CONNECT"),
|
||||
(Method::TRACE, "TRACE"),
|
||||
] {
|
||||
assert_eq!(http_metrics_method(&method), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_methods_collapse_to_other() {
|
||||
let method = Method::from_bytes(b"XVULNCARD000001").expect("valid token");
|
||||
assert_eq!(http_metrics_method(&method), "other");
|
||||
let other = Method::from_bytes(b"XVULNCARD000002").expect("valid token");
|
||||
assert_eq!(http_metrics_method(&other), "other");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3115,6 +3115,64 @@ async fn http_metrics_group_error_statuses() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[serial]
|
||||
async fn http_metrics_collapse_unknown_methods() {
|
||||
let mut app = test_app().await;
|
||||
let before = METRICS.render().unwrap();
|
||||
|
||||
for (method, path) in [
|
||||
("XVULNCARD000001", "/tokenize"),
|
||||
("XVULNCARD000002", "/tokenize"),
|
||||
("XVULNCARD000003", "/detokenize"),
|
||||
] {
|
||||
let response = app
|
||||
.call(
|
||||
Request::builder()
|
||||
.method(method)
|
||||
.uri(path)
|
||||
.body(Body::empty())
|
||||
.expect("build request"),
|
||||
)
|
||||
.await
|
||||
.expect("call app");
|
||||
assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||
}
|
||||
|
||||
let after = METRICS.render().unwrap();
|
||||
assert!(
|
||||
!after.contains("XVULNCARD"),
|
||||
"raw method tokens must not appear in metrics: {after}"
|
||||
);
|
||||
assert_eq!(
|
||||
metric_delta(
|
||||
&before,
|
||||
&after,
|
||||
"http_requests_total",
|
||||
Some("method=\"other\",status=\"4xx\",handler=\"/tokenize\""),
|
||||
),
|
||||
2.0
|
||||
);
|
||||
assert_eq!(
|
||||
metric_delta(
|
||||
&before,
|
||||
&after,
|
||||
"http_requests_total",
|
||||
Some("method=\"other\",status=\"4xx\",handler=\"/detokenize\""),
|
||||
),
|
||||
1.0
|
||||
);
|
||||
assert_eq!(
|
||||
metric_delta(
|
||||
&before,
|
||||
&after,
|
||||
"http_request_duration_seconds_count",
|
||||
Some("method=\"other\",handler=\"/tokenize\""),
|
||||
),
|
||||
2.0
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[serial]
|
||||
async fn load_endpoint_tracks_chat_stream_lifecycle() {
|
||||
|
||||
Reference in New Issue
Block a user