made query metrics better
Some checks failed
Push Dev / docker (push) Has been cancelled

This commit is contained in:
Shiroyasha 2025-03-01 14:41:14 +03:00
parent b02cca8608
commit 13e2a54eb2
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
3 changed files with 24 additions and 14 deletions

View file

@ -92,10 +92,10 @@ impl From<C2SMessage> for Vec<u8> {
impl C2SMessage { impl C2SMessage {
pub fn name(&self) -> &'static str { pub fn name(&self) -> &'static str {
match self { match self {
C2SMessage::Token(_) => "c2s>token", C2SMessage::Token(_) => "C2S;TOKEN",
C2SMessage::Ping(_, _, _) => "c2s>ping", C2SMessage::Ping(_, _, _) => "C2S;PING",
C2SMessage::Sub(_) => "c2s>sub", C2SMessage::Sub(_) => "C2S;SUB",
C2SMessage::Unsub(_) => "c2s>unsub", C2SMessage::Unsub(_) => "C2S;UNSUB",
} }
} }
} }

View file

@ -88,12 +88,12 @@ impl From<S2CMessage> for Vec<u8> {
impl S2CMessage { impl S2CMessage {
pub fn name(&self) -> &'static str { pub fn name(&self) -> &'static str {
match self { match self {
S2CMessage::Auth => "s2c>auth", S2CMessage::Auth => "S2C;AUTH",
S2CMessage::Ping(_, _, _, _) => "s2c>ping", S2CMessage::Ping(_, _, _, _) => "S2C;PING",
S2CMessage::Event(_) => "s2c>event", S2CMessage::Event(_) => "S2C;EVENT",
S2CMessage::Toast(_, _, _) => "s2c>toast", S2CMessage::Toast(_, _, _) => "S2C;TOAST",
S2CMessage::Chat(_) => "s2c>chat", S2CMessage::Chat(_) => "S2C;CHAT",
S2CMessage::Notice(_) => "s2c>notice", S2CMessage::Notice(_) => "S2C;NOTICE",
} }
} }
} }

View file

@ -43,8 +43,10 @@ fn create_mf(name: String, help: String, field_type: MetricType, metric: Metric)
} }
pub async fn track_metrics(req: Request<Body>, next: Next) -> Result<Response<Body>, StatusCode> { pub async fn track_metrics(req: Request<Body>, next: Next) -> Result<Response<Body>, StatusCode> {
let method = req.method().to_string();
let route = http_route(&req).to_string();
let start = Instant::now(); let start = Instant::now();
let uri = req.uri().path().to_string();
// Call the next middleware or handler // Call the next middleware or handler
let response = next.run(req).await; let response = next.run(req).await;
@ -52,18 +54,26 @@ pub async fn track_metrics(req: Request<Body>, next: Next) -> Result<Response<Bo
let latency = start.elapsed().as_secs_f64(); let latency = start.elapsed().as_secs_f64();
REQUESTS REQUESTS
.with_label_values(&[&uri, response.status().as_str()]) .with_label_values(&[&method, &route, response.status().as_str()])
.observe(latency); .observe(latency);
Ok(response) Ok(response)
} }
// https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/main/axum-tracing-opentelemetry/src/middleware/trace_extractor.rs#L177
#[inline]
fn http_route<B>(req: &Request<B>) -> &str {
req.extensions()
.get::<axum::extract::MatchedPath>()
.map_or_else(|| "", |mp| mp.as_str())
}
pub static REQUESTS: LazyLock<prometheus::HistogramVec> = LazyLock::new(|| { pub static REQUESTS: LazyLock<prometheus::HistogramVec> = LazyLock::new(|| {
register_histogram_vec!("sculptor_requests_count", "Number of requests", &["uri", "code"], vec![0.025, 0.250, 0.500]).unwrap() register_histogram_vec!("sculptor_requests_count", "Number of requests", &["method", "uri", "code"], vec![0.025, 0.250, 0.500]).unwrap()
}); });
pub static PINGS: LazyLock<prometheus::HistogramVec> = LazyLock::new(|| { pub static PINGS: LazyLock<prometheus::HistogramVec> = LazyLock::new(|| {
register_histogram_vec!("sculptor_pings_count", "Number of pings", &["type"], vec![0.000003, 0.00002, 0.0002]).unwrap() register_histogram_vec!("sculptor_pings_count", "Number of pings", &["type"], vec![0.000001, 0.00001, 0.0001]).unwrap()
}); });
pub static PINGS_ERROR: LazyLock<prometheus::IntCounter> = LazyLock::new(|| { pub static PINGS_ERROR: LazyLock<prometheus::IntCounter> = LazyLock::new(|| {