Using a tracer, you can create a root span capturing the critical path of a request. Child spans can be created to allocate latency relating to outgoing requests.
When tracing single-threaded code, just run it inside a scoped span:
// Start a new trace or a span within an existing trace representing an operation
ScopedSpan span = tracer.startScopedSpan("encode");
try {
// The span is in "scope" so that downstream code such as loggers can see trace IDs
return encoder.encode();
} catch (RuntimeException | Error e) {
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally {
span.end();
}
When you need more features, or finer control, use the Span type:
// Start a new trace or a span within an existing trace representing an operation
Span span = tracer.nextSpan().name("encode").start();
// Put the span in "scope" so that downstream code such as loggers can see trace IDs
try (SpanInScope ws = tracer.withSpanInScope(span)) {
return encoder.encode();
} catch (RuntimeException | Error e) {
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally {
span.end(); // note the scope is independent of the span. Always finish a span.
}
Both of the above examples report the exact same span on finish!
Span, ScopedSpan, Propagator
| Modifier and Type | Class and Description |
|---|---|
| public static interface | Tracer.SpanInScope
Scope of a span. |
| Modifier and Type | Field and Description |
|---|---|
| public static final Tracer | NOOP
A noop implementation.
|
| Modifier and Type | Method and Description |
|---|---|
| public Span | Returns: current span in scopeRetrieves the current span in scope or |
| public SpanCustomizer | Returns: current span customizerAllows to customize the current span in scope. |
| public CurrentTraceContext | |
| public Span | Returns: a child span or a new trace if no span was presentThis creates a new span based on the current span in scope. |
| public Span | |
| public Span. | Returns: a span builderIn some cases (e.g. when dealing with
|
| public ScopedSpan | Returns: span in scopeof the span in scope name)Returns a new child span if there's a |
| public TraceContext. | |
| public Tracer. |
| NOOP | back to summary |
|---|---|
| public static final Tracer NOOP Hides io. A noop implementation. | |
| currentSpan | back to summary |
|---|---|
| public Span currentSpan() Retrieves the current span in scope or | |
| currentSpanCustomizer | back to summary |
|---|---|
| public SpanCustomizer currentSpanCustomizer() Allows to customize the current span in scope.
| |
| currentTraceContext | back to summary |
|---|---|
| public CurrentTraceContext currentTraceContext() Returns the
| |
| nextSpan | back to summary |
|---|---|
| public Span nextSpan() This creates a new span based on the current span in scope. If there's no such span a new trace will be created.
| |
| nextSpan | back to summary |
|---|---|
| public Span nextSpan(Span parent) This creates a new span whose parent is | |
| spanBuilder | back to summary |
|---|---|
| public Span. In some cases (e.g. when dealing with
| |
| startScopedSpan | back to summary |
|---|---|
| public ScopedSpan startScopedSpan(String name) Returns a new child span if there's a Here's an example:
| |
| traceContextBuilder | back to summary |
|---|---|
| public TraceContext. Builder for
| |
| withSpan | back to summary |
|---|---|
| public Tracer. Makes the given span the "current span" and returns an object that exits that scope
on close. Calls to The most convenient way to use this method is via the try-with-resources idiom.
When tracing in-process commands, prefer Note While downstream code might affect the span, calling this method, and calling close on the result have no effect on the input. For example, calling close on the result does not finish the span. Not only is it safe to call close, you must call close to end the scope, or risk leaking resources associated with the scope.
| |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Redeclares java. |
| close | back to summary |
|---|---|
| public void close() Redeclares java. Doc from java. Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. As noted in
| |