@ThreadSafe public final class DefaultTracer extends Object implements Tracer
Tracer.| Modifier and Type | Method and Description |
|---|---|
Span |
getCurrentSpan()
Gets the current Span from the current Context.
|
io.opentelemetry.context.propagation.HttpTextFormat<SpanContext> |
getHttpTextFormat()
Returns the
HttpTextFormat for this tracer implementation. |
static Tracer |
getInstance()
Returns a
Tracer singleton that is the default implementations for Tracer. |
Span.Builder |
spanBuilder(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()
TracerTo install a Span to the current Context use Tracer.withSpan(Span).
startSpan methods do NOT modify the current Context Span.
getCurrentSpan in interface TracerSpan 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)
TracerSpan 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(String spanName)
TracerspanBuilder in interface TracerspanName - The name of the returned Span.Span.Builder to create and start a new Span.public io.opentelemetry.context.propagation.HttpTextFormat<SpanContext> getHttpTextFormat()
TracerHttpTextFormat for this tracer implementation.
If no tracer implementation is provided, this defaults to the W3C Trace Context HTTP text
format (HttpTraceContext). For more details see W3C Trace Context.
Example of usage on the client:
private static final Tracer tracer = OpenTelemetry.getTracer();
private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
private static final HttpTextFormat.Setter setter =
new HttpTextFormat.Setter<HttpURLConnection>() {
public void put(HttpURLConnection carrier, String key, String value) {
carrier.setRequestProperty(field, value);
}
}
void makeHttpRequest() {
Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
try (Scope s = tracer.withSpan(span)) {
HttpURLConnection connection =
(HttpURLConnection) new URL("http://myserver").openConnection();
textFormat.inject(span.getContext(), connection, httpURLConnectionSetter);
// Send the request, wait for response and maybe set the status if not ok.
}
span.end(); // Can set a status.
}
Example of usage on the server:
private static final Tracer tracer = OpenTelemetry.getTracer();
private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
private static final HttpTextFormat.Getter<HttpRequest> getter = ...;
void onRequestReceived(HttpRequest request) {
SpanContext spanContext = textFormat.extract(request, getter);
Span span = tracer.spanBuilder("MyRequest")
.setParent(spanContext)
.setSpanKind(Span.Kind.SERVER).startSpan();
try (Scope s = tracer.withSpan(span)) {
// Handle request and send response back.
}
span.end()
}
getHttpTextFormat in interface TracerHttpTextFormat for this implementation.