You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I have been trying to make use of tracing + opentelemetry crates to instrument my code for distributed tracing purposes. I intend to have an http request pass through my service, there will be calls to subsequent services that I am addding traceparent headers to, according to the W3C trace-context spec:
/// Initializes the OpenTelemetry tracer and sets it as the global tracer./// Setup exporter to push traces to the collector over GRPC.pubfninit_tracer() -> SdkTracerProvider{
global::set_text_map_propagator(TraceContextPropagator::new());// Initialize OTLP exporter using GRPC binary protocollet otlp_exporter = SpanExporter::builder().with_tonic().build().unwrap();// Create a tracer provider with the exporterlet tracer_provider = SdkTracerProvider::builder().with_batch_exporter(otlp_exporter).build();// Set it as the global provider
global::set_tracer_provider(tracer_provider.clone());
tracer_provider
}/// Initializes the OpenTelemetry logger and sets up the tracing subscriber.pubfninit_logger() -> SdkLoggerProvider{let log_exporter = LogExporter::builder().with_tonic().build().unwrap();let logger_provider = SdkLoggerProvider::builder().with_simple_exporter(log_exporter).build();let otel_layer = OpenTelemetryTracingBridge::new(&logger_provider);
tracing_subscriber::registry().with(otel_layer).with(EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into())).with(fmt::layer()).init();
logger_provider
}#[instrument]pubfnrequest(){
...// Propagate OpenTelemetry context to the downstream service.
let mut headers = HeaderMap::new();let tracer = global::tracer("my_tracer");let span = tracer
.span_builder("execute").with_kind(SpanKind::Client).start(&tracer);let context = Context::current_with_span(span);
global::get_text_map_propagator(|propagator| {
propagator.inject_context(&context,&mutHeaderInjector(&mut headers))});let response = client
.post(&url).headers(headers).json(&body).send().await?;
...}pubasyncfnadd_response_header(request:Request<Body>,next:Next) -> Response<Body>{let tracer = global::tracer("my_tracer");let span = tracer.start("request");let ctx = Context::current_with_span(span);letmut response = next.run(request).with_context(ctx.clone()).await;let span = ctx.span();let span_context = span.span_context();if span_context.is_valid(){let trace_id = span_context.trace_id().to_string();
response.headers_mut().insert(HeaderName::from_static("x-trace-id"),HeaderValue::from_str(&trace_id).unwrap(),);}
response
}
I am seeing different traceid when comparing traceparent sent to the downstream service and the x-trace-id response header, what am I doing wrong?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I have been trying to make use of tracing + opentelemetry crates to instrument my code for distributed tracing purposes. I intend to have an http request pass through my service, there will be calls to subsequent services that I am addding
traceparentheaders to, according to the W3C trace-context spec:I am seeing different traceid when comparing traceparent sent to the downstream service and the x-trace-id response header, what am I doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions