Skip to main content

rskit_logging/
context.rs

1//! Span-level context helpers — component tagging, request enrichment.
2
3/// Returns a new [`tracing::Span`] with a `component` field set.
4///
5/// # Example
6///
7/// ```rust
8/// # use rskit_logging::context::component_span;
9/// let _span = component_span("auth-service").entered();
10/// tracing::info!("component log");
11/// ```
12pub fn component_span(name: &'static str) -> tracing::Span {
13    tracing::info_span!("component", component = name)
14}
15
16/// Returns a new [`tracing::Span`] enriched with HTTP request metadata.
17pub fn request_span(method: &str, path: &str, request_id: &str) -> tracing::Span {
18    tracing::info_span!(
19        "request",
20        http.method  = %method,
21        http.path    = %path,
22        request_id   = %request_id,
23    )
24}
25
26/// Injects a correlation ID into the current span.
27pub fn set_correlation_id(id: &str) {
28    tracing::Span::current().record("correlation_id", id);
29}
30
31/// Injects a user ID into the current span.
32pub fn set_user_id(id: &str) {
33    tracing::Span::current().record("user_id", id);
34}
35
36/// Injects a trace ID into the current span.
37pub fn set_trace_id(id: &str) {
38    tracing::Span::current().record("trace_id", id);
39}