Search code examples
spring-cloudspring-cloud-sleuthbrave

Setting a Span's trace id in Brave vs. Sleuth


I'm looking for the proper way to set the trace-id for a Span in Brave. Pre-Brave we had:

Span span = Span.builder().traceId(someLong).build();

What's the brave equivalent? I have the following, but it's obviously not correct, as there's no way to set the Span's context explicitly.

Span span = tracer.nextSpan().start();
span.context().toBuilder().traceId(someLong).build();


Solution

  • You can do it like this brave.Span span = tracer.nextSpan().name("name").traceId(someLong).start();

    Or more advance

    brave.Span span = tracer.nextSpan().name("name").traceId(someLong);
    try (SpanInScope ws = tracer.withSpanInScope(span.start())) {
      // do sth
    } finally {
      span.finish();
    }
    

    Take look at spring cloud sleuth migration guide to catch all changes