Damian
Damian

Reputation: 3050

Generating micrometer tracer traceId programatically

I have an integration test when I want to validate traceId being propagated. But when I try to create an new context and span it turns out to be always empty. How can I create a proper context as it was generated in the controller?

@Autowired
Tracer tracer;

@Test
void shouldUseCreatedSpanTraceId() {

    TraceContext context = tracer.traceContextBuilder()
        .traceId("4bf92f3577b34da6a3ce929d0e0e4736")
        .spanId("00f067aa0ba902b7")
        .sampled(false)
        .build();

    Span span = null;
    try {
        span = tracer.spanBuilder().setParent(context).start();
        assertThat(span.context().traceId()).isNotBlank(); // Expecting not blank but was: ""
        feignClient.create(testRequest());
    } finally {
        span.end();
    }

}

Upvotes: 0

Views: 1283

Answers (1)

gtiwari333
gtiwari333

Reputation: 25174

Can you try with tracer.currentTraceContext().newScope(tc) ?

The following is working for me.


        log.info("Old trace id is logged");

        //set new trace id
        TraceContext tc = tracer.traceContextBuilder()
                .traceId("4bf92f3577b34da6a3ce929d0e0e4736")
                .spanId("00f067aa0ba902b7")
                .sampled(false)
                .build();

        try (var sc = tracer.currentTraceContext().newScope(tc)) {
            log.info("Now using custom trace id in log ");
        }

Upvotes: 2

Related Questions