feat: add method for tracking request metrics

This commit is contained in:
Zack Fu Zi Xiang 2024-10-08 18:59:45 +08:00
parent 33e63fae5c
commit 7e62e96d3b
No known key found for this signature in database
2 changed files with 25 additions and 4 deletions

View File

@ -32,11 +32,13 @@ async fn metrics_handler(reg: web::Data<Arc<Registry>>) -> Result<HttpResponse>
#[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<String>, path: String, ms: u64, status_code: u16) {
pub fn record_request(
&self,
trace_id: Option<String>,
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 }));
}
}

View File

@ -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(),
);