Skip to content

Commit fec9f69

Browse files
committed
feat(api): add tracing span with request_id for full request observability
Wrap each HTTP request in a tracing info_span so the request_id propagates to all log entries within the request lifecycle, not just the final "request handled" line.
1 parent 1631367 commit fec9f69

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

crates/ls-api/src/api/middleware.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,21 @@ async fn request_context_middleware(
280280

281281
let method = req.method().to_string();
282282
let path = req.path().to_string();
283+
284+
let span = tracing::info_span!(
285+
"http_request",
286+
request_id = %request_id,
287+
http.method = %method,
288+
http.route = %path,
289+
);
290+
283291
let started = Instant::now();
284-
let mut response = next.call(req).await?.map_into_boxed_body();
292+
let mut response = next.call(req).instrument(span.clone()).await?.map_into_boxed_body();
285293
let duration_ms = started.elapsed().as_millis() as u64;
286294

295+
// Enter the span for post-processing (no .await points after this)
296+
let _guard = span.enter();
297+
287298
if let Some(state) = &state {
288299
state.metrics.record_latency(duration_ms);
289300
}
@@ -310,9 +321,6 @@ async fn request_context_middleware(
310321
}
311322

312323
info!(
313-
request_id = %request_id,
314-
method = %method,
315-
path = %path,
316324
status,
317325
duration_ms,
318326
"request handled"

crates/ls-api/src/api/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use ls_logging::LogHandle;
4343
use rust_decimal::Decimal;
4444
use serde::{Deserialize, Deserializer, Serialize};
4545
use serde_json::{Value, json};
46-
use tracing::{error, info, warn};
46+
use tracing::{Instrument, error, info, warn};
4747
use uuid::Uuid;
4848

4949
struct ApiMetrics {

0 commit comments

Comments
 (0)