blackcompe
blackcompe

Reputation: 3190

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();

Upvotes: 0

Views: 9872

Answers (3)

BishalG
BishalG

Reputation: 1424

We can build up TraceContext first and then we can use this to create TraceContextOrSamplingFlags which can be then used to create Span as follws:

TraceContext traceContext = TraceContext.newBuilder()
                            .traceId(someTraceId)
                            .spanId(someSpanId)
                            .build();

Span span = tracer.nextSpan(TraceContextOrSamplingFlags.create(traceContext))
                            .name(someName).start();

Upvotes: 3

Sahil
Sahil

Reputation: 888

You can use the TraceContext Builder to set your own TraceId.

    TraceContext traceContext = TraceContext.newBuilder().traceId(traceId).build();
    Span span =
            this.tracing.tracer()
                    .toSpan(traceContext)
                    .name("application.name"))
                    .start();

This is optional though; you could let the Tracer generate its own traceId when you create a new Span :-

this.tracing.tracer().nextSpan().name("application.name").start();

Upvotes: 3

yevtsy
yevtsy

Reputation: 574

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

Upvotes: 2

Related Questions