fix: harden typed metrics review findings

This commit is contained in:
2026-08-14 13:50:04 +03:00
parent 996a5461de
commit b7face0e94
30 changed files with 722 additions and 382 deletions
@@ -1,5 +1,8 @@
use std::time::Instant;
use axum::{extract::Request, http::HeaderValue, middleware::Next, response::Response};
use crank_core::{CorrelationContext, RequestId, TraceContext};
use crank_metrics::ExemplarTraceId;
use crank_observability::{set_remote_trace_parent, with_request_correlation};
use tracing::{Instrument, info_span};
@@ -10,12 +13,25 @@ const HEADER_X_TRACE_ID: axum::http::HeaderName = axum::http::HeaderName::from_s
#[derive(Clone, Debug)]
pub(super) struct RequestContext {
pub(super) correlation: CorrelationContext,
started_at: Instant,
}
impl RequestContext {
pub(super) fn request_id(&self) -> &str {
self.correlation.request_id().as_str()
}
pub(super) fn started_at(&self) -> Instant {
self.started_at
}
pub(super) fn exemplar(&self) -> Option<ExemplarTraceId> {
self.correlation
.trace_context()
.is_sampled()
.then(|| ExemplarTraceId::parse(self.correlation.trace_id().as_str()))
.flatten()
}
}
pub(super) async fn apply_request_context(mut request: Request, next: Next) -> Response {
@@ -37,6 +53,7 @@ pub(super) async fn apply_request_context(mut request: Request, next: Next) -> R
span.record("trace_id", trace_context.trace_id().as_str());
let context = RequestContext {
correlation: CorrelationContext::new(request_id, trace_context),
started_at: Instant::now(),
};
request.extensions_mut().insert(context.clone());
@@ -51,6 +68,9 @@ pub(super) async fn apply_request_context(mut request: Request, next: Next) -> R
if let Ok(value) = HeaderValue::from_str(context.correlation.trace_id().as_str()) {
response.headers_mut().insert(HEADER_X_TRACE_ID, value);
}
if let Some(exemplar) = context.exemplar() {
response.extensions_mut().insert(exemplar);
}
response
},
)