feat: add method for tracking request metrics
This commit is contained in:
parent
33e63fae5c
commit
7e62e96d3b
|
|
@ -32,11 +32,13 @@ async fn metrics_handler(reg: web::Data<Arc<Registry>>) -> Result<HttpResponse>
|
||||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
|
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
|
||||||
pub struct PathLabel {
|
pub struct PathLabel {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
pub method: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
|
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
|
||||||
pub struct ResultLabel {
|
pub struct ResultLabel {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
pub method: String,
|
||||||
pub status_code: u16,
|
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
|
// 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
|
self
|
||||||
.requests_count
|
.requests_count
|
||||||
.get_or_create(&PathLabel { path: path.clone() })
|
.get_or_create(&PathLabel {
|
||||||
|
path: path.clone(),
|
||||||
|
method: method.clone(),
|
||||||
|
})
|
||||||
.inc();
|
.inc();
|
||||||
self
|
self
|
||||||
.requests_latency
|
.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 }));
|
.inc_by(ms, trace_id.clone().map(|s| TraceLabel { trace_id: s }));
|
||||||
self
|
self
|
||||||
.requests_result
|
.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 }));
|
.inc_by(1, trace_id.clone().map(|s| TraceLabel { trace_id: s }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ where
|
||||||
|
|
||||||
let request_id = get_request_id(&req);
|
let request_id = get_request_id(&req);
|
||||||
let endpoint = req.match_pattern();
|
let endpoint = req.match_pattern();
|
||||||
|
let method = req.method().to_string();
|
||||||
|
|
||||||
// Call the next service
|
// Call the next service
|
||||||
let res = self.service.call(req);
|
let res = self.service.call(req);
|
||||||
|
|
@ -69,6 +70,7 @@ where
|
||||||
metrics.record_request(
|
metrics.record_request(
|
||||||
request_id,
|
request_id,
|
||||||
endpoint,
|
endpoint,
|
||||||
|
method,
|
||||||
duration.as_millis() as u64,
|
duration.as_millis() as u64,
|
||||||
status.into(),
|
status.into(),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue