open-telemetry

Can W3C traceparent be set by Java OpenTelemetry SDK


I am working to set the traceparent header in OpenTelemetry https://w3c.github.io/trace-context/#traceparent-header

I am using the JAVA OTEL SDK and wanted to know if there is a Java based solution to create the traceparent through a Java function.

Thanks, Prabal


Solution

  • opentelemetry-java maintainer here.

    Yes. opentelemetry-java has an interface called TextMapPropagator which extracts and injects context. There's a w3c trace context implementation called W3CTraceContextPropagator which injects and extracts the w3c trace context headers traceparent and tracestate.

    See ContextPropgators for a demonstration of extracting and injecting context.

    See TextMapPropagators for a discussion on the different implementations available.

    To manually put SpanContext into Context, do something like:

        Context context =
            Context.current()
                .with(
                    Span.wrap(
                        SpanContext.create(
                            "<traceId>",
                            "<spanId>",
                            TraceFlags.getDefault(),
                            TraceState.getDefault())));