Brave supports a "current span" concept which represents the in-flight
operation. Tracer.currentSpan() can be used to add custom tags to a
span and Tracer.nextSpan() can be used to create a child of whatever
is in-flight.
When writing new instrumentation, it is important to place a span you
created in scope as the current span. Not only does this allow users to
access it with Tracer.currentSpan(), but it also allows customizations
like SLF4J MDC to see the current trace IDs.
Tracer.withSpanInScope(Span) facilitates this and is most conveniently
employed via the try-with-resources idiom. Whenever external code might
be invoked (such as proceeding an interceptor or otherwise), place the
span in scope like this.
try (SpanInScope ws = tracer.withSpanInScope(span)) { return inboundRequest.invoke(); } finally { // note the scope is independent of the span span.finish(); }
In edge cases, you may need to clear the current span temporarily. For
example, launching a task that should not be associated with the current
request. To do this, simply pass null to withSpanInScope.
try (SpanInScope cleared = tracer.withSpanInScope(null)) {
startBackgroundThread();
}