When using the OTel Java API, manually instrumented code typically looks like the following:
@Inject
Tracer tracer;
public String instrumentedMethod() {
// ...
Span span = tracer.spanBuilder("interesting operation").startSpan();
span.setAttribute("custom.info", "some info");
try (Scope scope = span.makeCurrent()) {
// logic
}
finally {
span.end();
}
// ...
}
From my point of view, the lifespan of the span
is defined twice:
tracer.spanBuilder("...").startSpan()
andspan.end()
demarcate the beginning and the end of the span,- but also the
span.makeCurrent()
andscope.end()
does this in some way
What's the purpose of startSpan()
/span.end()
in contrast to span.makeCurrent()
/scope.end()
. Can scope
be omitted? Is span.end()
superfluous when scope
is used?