diff --git a/src/api/metrics.rs b/src/api/metrics.rs index 224ab5c9..c9790f7c 100644 --- a/src/api/metrics.rs +++ b/src/api/metrics.rs @@ -32,11 +32,13 @@ async fn metrics_handler(reg: web::Data>) -> Result #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] pub struct PathLabel { pub path: String, + pub method: String, } #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] pub struct ResultLabel { pub path: String, + pub method: String, pub status_code: u16, } @@ -108,18 +110,35 @@ impl RequestMetrics { } // app services/middleware should call this method to increase the request count for the path - pub fn record_request(&self, trace_id: Option, path: String, ms: u64, status_code: u16) { + pub fn record_request( + &self, + trace_id: Option, + path: String, + method: String, + ms: u64, + status_code: u16, + ) { self .requests_count - .get_or_create(&PathLabel { path: path.clone() }) + .get_or_create(&PathLabel { + path: path.clone(), + method: method.clone(), + }) .inc(); self .requests_latency - .get_or_create(&PathLabel { path: path.clone() }) + .get_or_create(&PathLabel { + path: path.clone(), + method: method.clone(), + }) .inc_by(ms, trace_id.clone().map(|s| TraceLabel { trace_id: s })); self .requests_result - .get_or_create(&ResultLabel { path, status_code }) + .get_or_create(&ResultLabel { + path, + method, + status_code, + }) .inc_by(1, trace_id.clone().map(|s| TraceLabel { trace_id: s })); } } diff --git a/src/middleware/metrics_mw.rs b/src/middleware/metrics_mw.rs index 5e2089b8..8f15f495 100644 --- a/src/middleware/metrics_mw.rs +++ b/src/middleware/metrics_mw.rs @@ -56,6 +56,7 @@ where let request_id = get_request_id(&req); let endpoint = req.match_pattern(); + let method = req.method().to_string(); // Call the next service let res = self.service.call(req); @@ -69,6 +70,7 @@ where metrics.record_request( request_id, endpoint, + method, duration.as_millis() as u64, status.into(), );