Opentelemetry manual traceparent creation

21 Views Asked by At

I have a service with a rest controller that accepts a list of messages on the input, then the messages are published on kafka topic one by one. The messages have the same traceparent header (generated automatically, there is java agent running).

The issue is, I need to override the traceparent header for each message, because downstream systems rely on every message having separate trace id.

I have come up with something like the following:

public void processMessage(Msg msg) {
        
        // Set the trace ID, tracer bean already present
        Span span = tracer.spanBuilder("example-span").startSpan();
        span.setSpanContext(span.getSpanContext().toBuilder().setTraceId("traceId").build());
        
        try (Scope scope = span.makeCurrent()) {
            
            //here I need to pass the header downstream
            Map<String, String> headers = Map.of("traceparent", "concatenate values from span.getSpanContext()???");

            sendToKafka(msg, headers);

        } finally {
            span.end();
        }
    }

But the API seem to be very limited when it comes to manual operations. Does anyone have an idea on how to get the traceparent header in the nicer way than just concatenating the values from the context?

0

There are 0 best solutions below