Skip to main content

cratestack_axum/headers/
enrich.rs

1use axum::http::HeaderMap;
2
3use super::forwarded::parse_client_ip;
4use super::traceparent::parse_traceparent;
5
6/// Enrich a `CoolContext` with the request id (from `traceparent`) and the
7/// client IP (from `Forwarded`/`X-Forwarded-For`). Malformed `traceparent`
8/// headers are silently ignored here — the auth/header-validation layer is
9/// the right place to reject them, not the enrichment seam.
10pub fn enrich_context_from_headers(
11    ctx: cratestack_core::CoolContext,
12    headers: &HeaderMap,
13) -> cratestack_core::CoolContext {
14    let mut ctx = ctx;
15    if let Ok(Some(trace_id)) = parse_traceparent(headers) {
16        ctx = ctx.with_request_id(trace_id);
17    }
18    if let Some(ip) = parse_client_ip(headers) {
19        ctx = ctx.with_client_ip(ip);
20    }
21    ctx
22}