From 406f8e0f80cd201c76917895cfa2eb494a5ae042 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 4 Mar 2025 09:21:19 +0100 Subject: [PATCH] metrics: Add labels to `iocaine_garbage_served` Lifted out the label construction code into its dedicated function, so it can be reused to add labels to `iocaine_garbage_served` too. The required information is at hand at that point, so the cost of doing this is minimal. Changelog & Grafana dashboard adjusted accordingly. Signed-off-by: Gergely Nagy --- CHANGELOG.md | 4 ++- data/grafana-dashboard.json | 4 +-- src/app.rs | 6 ++++- src/tenx_programmer.rs | 52 +++++++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fae265..688e623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ## [Unreleased] -No changes yet. +### Added + +- The `iocaine_garbage_served` metric will have the same labels applied to it as `iocaine_requests_total`. ## [1.0.0] - 2025-03-01 diff --git a/data/grafana-dashboard.json b/data/grafana-dashboard.json index f59ee85..a22e4e4 100644 --- a/data/grafana-dashboard.json +++ b/data/grafana-dashboard.json @@ -355,7 +355,7 @@ "targets": [ { "editorMode": "code", - "expr": "iocaine_garbage_served", + "expr": "sum(iocaine_garbage_served)", "legendFormat": "__auto", "range": true, "refId": "A", @@ -613,6 +613,6 @@ "timezone": "browser", "title": "Iocaine", "uid": "aec38snrfs4cgf", - "version": 52, + "version": 54, "weekStart": "" } \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 8e40db9..c628a3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -119,7 +119,11 @@ async fn poison( let garbage = AssembledStatisticalSequences::generate(&iocaine, host, &path)?; if iocaine.config.metrics.enable { - metrics::counter!("iocaine_garbage_served").increment(garbage.len() as u64); + tenx_programmer::add_garbage_metrics( + &iocaine.config.metrics, + &headers, + garbage.len() as u64, + )?; } Ok(Html(garbage)) diff --git a/src/tenx_programmer.rs b/src/tenx_programmer.rs index 1692b7e..0a5c9fd 100644 --- a/src/tenx_programmer.rs +++ b/src/tenx_programmer.rs @@ -16,38 +16,33 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::{ app::{shutdown_signal, AppError, StatefulIocaine}, - config::MetricsLabel, + config::{MetricsConfig, MetricsLabel}, }; -pub async fn track_metrics( - State(iocaine): State, - req: Request, - next: Next, -) -> Result, AppError> { - let headers = req.headers().clone(); - let response = next.run(req).await; - let cfg = &iocaine.config.metrics; - +fn build_labels( + config: &MetricsConfig, + headers: &axum::http::HeaderMap, +) -> Result, AppError> { let mut labels = Vec::new(); - if cfg.labels.contains(&MetricsLabel::Host) { + if config.labels.contains(&MetricsLabel::Host) { if let Some(host) = headers.get("host") { let host = host.to_str()?.to_string(); labels.push(("host", host)); } } - if cfg.labels.contains(&MetricsLabel::UserAgent) - || cfg.labels.contains(&MetricsLabel::UserAgentGroup) + if config.labels.contains(&MetricsLabel::UserAgent) + || config.labels.contains(&MetricsLabel::UserAgentGroup) { if let Some(ua) = headers.get("user-agent") { let user_agent = ua.to_str()?.to_string(); - if cfg.labels.contains(&MetricsLabel::UserAgent) { + if config.labels.contains(&MetricsLabel::UserAgent) { labels.push(("user-agent", user_agent.clone())); } - if cfg.labels.contains(&MetricsLabel::UserAgentGroup) { - if let Some(group) = cfg + if config.labels.contains(&MetricsLabel::UserAgentGroup) { + if let Some(group) = config .agent_group .iter() .find(|agent_group_config| agent_group_config.agent.is_match(&user_agent)) @@ -58,6 +53,31 @@ pub async fn track_metrics( } } + Ok(labels) +} + +pub fn add_garbage_metrics( + config: &MetricsConfig, + headers: &axum::http::HeaderMap, + garbage: u64, +) -> Result<(), AppError> { + let labels = build_labels(config, headers)?; + + metrics::counter!("iocaine_garbage_served", &labels).increment(garbage); + + Ok(()) +} + +pub async fn track_metrics( + State(iocaine): State, + req: Request, + next: Next, +) -> Result, AppError> { + let headers = req.headers().clone(); + let response = next.run(req).await; + + let labels = build_labels(&iocaine.config.metrics, &headers)?; + metrics::counter!("iocaine_requests_total", &labels).increment(1); Ok(response)