Operations

Context Propagation


Trace context is propagated across service boundaries using W3C traceparent headers. The tracing-export feature enables this.

Injecting Context

Add trace context to outgoing HTTP requests:

1
use orcher_monitoring::tracing::inject_context;
2
use reqwest::Client;
3
4
async fn call_external_service(data: &ServiceRequest) -> Result<ServiceResponse> {
5
let client = Client::new();
6
let mut headers = reqwest::header::HeaderMap::new();
7
8
// Inject W3C traceparent header
9
inject_context(&mut headers);
10
11
let response = client
12
.post("https://api.example.com/process")
13
.headers(headers)
14
.json(data)
15
.send()
16
.await?;
17
18
Ok(response.json().await?)
19
}

Extracting Context

Continue a trace from incoming HTTP requests:

1
use orcher_monitoring::tracing::extract_context;
2
3
async fn handle_request(req: Request) -> Response {
4
// Extract trace context from incoming headers
5
let parent_context = extract_context(req.headers());
6
7
let span = if let Some(ctx) = parent_context {
8
tracing::info_span!("handle_request", otel.parent = ctx)
9
} else {
10
tracing::info_span!("handle_request")
11
};
12
13
let _guard = span.enter();
14
// All child spans inherit this trace context
15
process_request(req).await
16
}

Trace and Span IDs

Access the current trace/span IDs for correlation with logs and external systems:

1
use orcher_monitoring::tracing::{current_trace_id, current_span_id};
2
3
let trace_id = current_trace_id(); // Option<String>
4
let span_id = current_span_id(); // Option<String>
5
6
tracing::info!(
7
trace_id = ?trace_id,
8
span_id = ?span_id,
9
"Processing with trace correlation"
10
);

Next Steps