Modifier and Type | Method and Description |
---|---|
Span |
getCurrentSpan()
Gets the current Span from the current Context.
|
static Tracer |
getInstance()
Returns a
Tracer singleton that is the default implementations for Tracer . |
Span.Builder |
spanBuilder(java.lang.String spanName)
Returns a
Span.Builder to create and start a new Span . |
io.opentelemetry.context.Scope |
withSpan(Span span)
Enters the scope of code where the given
Span is in the current Context, and returns an
object that represents that scope. |
public static Tracer getInstance()
Tracer
singleton that is the default implementations for Tracer
.Tracer
singleton that is the default implementations for Tracer
.public Span getCurrentSpan()
Tracer
To install a Span
to the current Context use Tracer.withSpan(Span)
.
startSpan methods do NOT modify the current Context Span
.
getCurrentSpan
in interface Tracer
Span
that does nothing and has an invalid SpanContext
if no
Span
is associated with the current Context, otherwise the current Span
from the Context.public io.opentelemetry.context.Scope withSpan(Span span)
Tracer
Span
is in the current Context, and returns an
object that represents that scope. The scope is exited when the returned object is closed.
Supports try-with-resource idiom.
Can be called with DefaultSpan
to enter a scope of code where tracing is stopped.
Example of usage:
private static Tracer tracer = OpenTelemetry.getTracer();
void doWork() {
// Create a Span as a child of the current Span.
Span span = tracer.spanBuilder("my span").startSpan();
try (Scope ws = tracer.withSpan(span)) {
tracer.getCurrentSpan().addEvent("my event");
doSomeOtherWork(); // Here "span" is the current Span.
}
span.end();
}
Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.
Example of usage prior to Java SE7:
private static Tracer tracer = OpenTelemetry.getTracer();
void doWork() {
// Create a Span as a child of the current Span.
Span span = tracer.spanBuilder("my span").startSpan();
Scope ws = tracer.withSpan(span);
try {
tracer.getCurrentSpan().addEvent("my event");
doSomeOtherWork(); // Here "span" is the current Span.
} finally {
ws.close();
}
span.end();
}
public Span.Builder spanBuilder(java.lang.String spanName)
Tracer
spanBuilder
in interface Tracer
spanName
- The name of the returned Span.Span.Builder
to create and start a new Span
.