feat: freeze typed metrics registry and bounded exemplars
This commit is contained in:
1 parent
17c8e3a8f0
commit
8e709acea9
29 files changed
+1690
-103
No files matched your search
@@ -1,9 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
use std::{sync::Arc, time::Instant};
|
||||
|
||||
use axum::{http::header::CONTENT_TYPE, response::Response};
|
||||
use crank_metrics::{
|
||||
InFlightGuard, LimitStage, McpMethod, McpOutcome, McpResponseMode, record_limit_rejection,
|
||||
record_mcp_request,
|
||||
ExemplarTraceId, InFlightGuard, LimitStage, McpMethod, McpOutcome, McpResponseMode,
|
||||
record_limit_rejection, record_mcp_request_with_exemplar,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||
@@ -14,14 +14,18 @@ pub(super) struct McpRequestMetrics {
|
||||
method: McpMethod,
|
||||
response_mode: McpResponseMode,
|
||||
outcome: McpOutcome,
|
||||
started_at: Instant,
|
||||
exemplar: Option<ExemplarTraceId>,
|
||||
}
|
||||
|
||||
impl McpRequestMetrics {
|
||||
pub(super) const fn invalid() -> Self {
|
||||
pub(super) fn invalid() -> Self {
|
||||
Self {
|
||||
method: McpMethod::Invalid,
|
||||
response_mode: McpResponseMode::Unknown,
|
||||
outcome: McpOutcome::Aborted,
|
||||
started_at: Instant::now(),
|
||||
exemplar: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +34,17 @@ impl McpRequestMetrics {
|
||||
method: normalized_mcp_method(message),
|
||||
response_mode: McpResponseMode::Unknown,
|
||||
outcome: McpOutcome::Aborted,
|
||||
started_at: Instant::now(),
|
||||
exemplar: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn complete(&mut self, response: &Response) {
|
||||
self.exemplar = response
|
||||
.headers()
|
||||
.get("x-trace-id")
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(ExemplarTraceId::parse);
|
||||
self.response_mode = response
|
||||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
@@ -71,7 +82,13 @@ impl McpRequestMetrics {
|
||||
|
||||
impl Drop for McpRequestMetrics {
|
||||
fn drop(&mut self) {
|
||||
record_mcp_request(self.method, self.response_mode, self.outcome);
|
||||
record_mcp_request_with_exemplar(
|
||||
self.method,
|
||||
self.response_mode,
|
||||
self.outcome,
|
||||
self.started_at.elapsed(),
|
||||
self.exemplar,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user