How to set a custom Trace ID value in Micrometer-Tracing – Micrometer

by
Ali Hasan
micrometer micrometer-tracing spring-boot spring-boot-actuator

Quick Fix: Use a custom TextMapPropagator to override the default W3CTraceContextPropagator and set a custom Trace ID value. Add the custom propagator as a @Primary bean to override the default one. Extract the legacy Trace ID from the carrier and use it to construct a new traceparent header value. Inject the new traceparent header value along with other propagation fields.

The Problem:

How do I create and activate a Span with a given Trace ID value instead of an autogenerated one using the Micrometer-Tracing library with the OpenTelemetry bridge? I need to set a custom Trace ID value using a custom header, but the traceparent header should still work. Is it possible to do this in Micrometer-Tracing?

The Solutions:

Solution 1: Wrapper for W3C Trace Context Propagator

This solution creates a wrapper around the OpenTelemetry’s W3C Trace Context Propagator (`W3CTraceContextPropagator`). It extends the functionality of the propagator by checking for a custom header (in this case, `TraceId`) in addition to the standard `traceparent` header. If the `TraceId` header is present and `traceparent` is empty, it constructs a `traceparent` header using the value from `TraceId` and sets it as part of the context propagation. This allows for the creation of spans with a custom Trace ID value.

To use this solution:

  1. Create a class that implements the `TextMapPropagator` interface, like the one provided in the code snippet below.
  2. Annotate the class with `@Primary` to override the default W3CTraceContextPropagator bean.
  3. Configure the Spring application to use this custom propagator by setting the `management.tracing.propagation.type` property to `my-custom-w3c-extension`. This will enable the conditional bean registration based on the property value.

The provided Java code snippet demonstrates this approach. It extends the `W3CTraceContextPropagator` to handle the `TraceId` header and inject the custom `traceparent` header when necessary. This allows for the creation of spans with a custom Trace ID value while still supporting standard W3C trace context propagation.

Q&A

How to set a custom Trace ID value in Micrometer-Tracing?

You can set a custom Trace ID value in Micrometer-Tracing by injecting a custom TraceContext into the tracer.

Can I set a custom Trace ID value without autogeneration?

Yes, you can create and activate a Span with a given Trace ID value instead of an autogenerated one.

How to use a legacy Trace ID header with Micrometer Tracing?

You can use a custom TextMapPropagator to extract the Trace ID from a legacy header and start a span with it.

Video Explanation:

The following video, titled "Reactive and Imperative Context Propagation in Spring Applications ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... Micrometer, which provides new and essential capabilities for ... tracing, and logging and how library implementers can take advantage of these