Class Tracer
- Direct Known Subclasses:
Tracer.NoopTracer, TracerImpl
Span creation and in-process context interaction.
Users may choose to use manual or automatic Context propagation. Because of that this class offers APIs to facilitate both usages.
The automatic context propagation is done using Context which is a gRPC
independent implementation for in-process Context propagation mechanism which can carry
scoped-values across API boundaries and between threads. Users of the library must propagate the
Context between different threads.
Example usage with automatic context propagation:
class MyClass {
private static final Tracer tracer = Tracing.getTracer();
void doWork() {
try(Scope ss = tracer.spanBuilder("MyClass.DoWork").startScopedSpan()) {
tracer.getCurrentSpan().addAnnotation("Starting the work.");
doWorkInternal();
tracer.getCurrentSpan().addAnnotation("Finished working.");
}
}
}
Example usage with manual context propagation:
class MyClass {
private static final Tracer tracer = Tracing.getTracer();
void doWork(Span parent) {
Span childSpan = tracer.spanBuilderWithExplicitParent("MyChildSpan", parent).startSpan();
childSpan.addAnnotation("Starting the work.");
try {
doSomeWork(childSpan); // Manually propagate the new span down the stack.
} finally {
// To make sure we end the span even in case of an exception.
childSpan.end(); // Manually end the span.
}
}
}
- Since:
- 0.5
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal SpanGets the current Span from the current Context.(package private) static TracerReturns the no-op implementation of theTracer.final SpanBuilderspanBuilder(String spanName) Returns aSpanBuilderto create and start a new childSpanas a child of to the currentSpanif any, otherwise creates a rootSpan.abstract SpanBuilderspanBuilderWithExplicitParent(String spanName, Span parent) Returns aSpanBuilderto create and start a new childSpan(or root if parent isnullor has an invalidSpanContext), with parent being the designatedSpan.abstract SpanBuilderspanBuilderWithRemoteParent(String spanName, SpanContext remoteParentSpanContext) Returns aSpanBuilderto create and start a new childSpan(or root if parent isSpanContext.INVALIDornull), with parent being the remoteSpandesignated by theSpanContext.final ScopeEnters the scope of code where the givenSpanis in the current Context, and returns an object that represents that scope.final RunnableReturns aRunnablethat runs the given task with the givenSpanin the current context.final <C> Callable<C> Returns aCallablethat runs the given task with the givenSpanin the current context.
-
Field Details
-
noopTracer
-
-
Constructor Details
-
Tracer
protected Tracer()
-
-
Method Details
-
getNoopTracer
Returns the no-op implementation of theTracer.- Returns:
- the no-op implementation of the
Tracer.
-
getCurrentSpan
Gets the current Span from the current Context.To install a
Spanto the current Context usewithSpan(Span)OR useSpanBuilder.startScopedSpan()methods to start a newSpan.startSpan methods do NOT modify the current Context
Span.- Returns:
- a default
Spanthat does nothing and has an invalidSpanContextif noSpanis associated with the current Context, otherwise the currentSpanfrom the Context. - Since:
- 0.5
-
withSpan
Enters the scope of code where the givenSpanis 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
BlankSpanto enter a scope of code where tracing is stopped.Example of usage:
private static Tracer tracer = Tracing.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().addAnnotation("my annotation"); 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 = Tracing.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().addAnnotation("my annotation"); doSomeOtherWork(); // Here "span" is the current Span. } finally { ws.close(); } span.end(); }- Parameters:
span- TheSpanto be set to the current Context.- Returns:
- an object that defines a scope where the given
Spanwill be set to the current Context. - Throws:
NullPointerException- ifspanisnull.- Since:
- 0.5
-
withSpan
Returns aRunnablethat runs the given task with the givenSpanin the current context.Users may consider to use
SpanBuilder.startSpanAndRun(Runnable).Any error will end up as a
Status.UNKNOWN.IMPORTANT: Caller must manually propagate the entire
io.grpc.Contextwhen wraps aRunnable, see the examples.IMPORTANT: Caller must manually end the
Spanwithin theRunnable, or after theRunnableis executed.Example with Executor wrapped with
Context.currentContextExecutor(Executor):class MyClass { private static Tracer tracer = Tracing.getTracer(); void handleRequest(Executor executor) { Span span = tracer.spanBuilder("MyRunnableSpan").startSpan(); executor.execute(tracer.withSpan(span, new Runnable() { @Override public void run() { try { sendResult(); } finally { span.end(); } } })); } }Example without Executor wrapped with
Context.currentContextExecutor(Executor):class MyClass { private static Tracer tracer = Tracing.getTracer(); void handleRequest(Executor executor) { Span span = tracer.spanBuilder("MyRunnableSpan").startSpan(); executor.execute(Context.wrap(tracer.withSpan(span, new Runnable() { @Override public void run() { try { sendResult(); } finally { span.end(); } } }))); } }- Parameters:
span- theSpanto be set as current.runnable- theRunnableto withSpan in theSpan.- Returns:
- the
Runnable. - Since:
- 0.11.0
-
withSpan
Returns aCallablethat runs the given task with the givenSpanin the current context.Users may consider to use
SpanBuilder.startSpanAndCall(Callable).Any error will end up as a
Status.UNKNOWN.IMPORTANT: Caller must manually propagate the entire
io.grpc.Contextwhen wraps aCallable, see the examples.IMPORTANT: Caller must manually end the
Spanwithin theCallable, or after theCallableis executed.Example with Executor wrapped with
Context.currentContextExecutor(Executor):class MyClass { private static Tracer tracer = Tracing.getTracer(); void handleRequest(Executor executor) { Span span = tracer.spanBuilder("MyRunnableSpan").startSpan(); executor.execute(tracer.withSpan(span,new Callable<MyResult>(){ @Override public MyResult call() throws Exception { try { return sendResult(); } finally { span.end(); } } })); } }Example without Executor wrapped with
Context.currentContextExecutor(Executor):class MyClass { private static Tracer tracer = Tracing.getTracer(); void handleRequest(Executor executor) { Span span = tracer.spanBuilder("MyRunnableSpan").startSpan(); executor.execute(Context.wrap(tracer.withSpan(span,new Callable<MyResult>(){ @Override public MyResult call() throws Exception { try { return sendResult(); } finally { span.end(); } } }))); } }- Parameters:
span- theSpanto be set as current.callable- theCallableto run in theSpan.- Returns:
- the
Callable. - Since:
- 0.11.0
-
spanBuilder
Returns aSpanBuilderto create and start a new childSpanas a child of to the currentSpanif any, otherwise creates a rootSpan.See
SpanBuilderfor usage examples.This must be used to create a
Spanwhen automatic Context propagation is used.This is equivalent with:
tracer.spanBuilderWithExplicitParent("MySpanName",tracer.getCurrentSpan());- Parameters:
spanName- The name of the returned Span.- Returns:
- a
SpanBuilderto create and start a newSpan. - Throws:
NullPointerException- ifspanNameisnull.- Since:
- 0.5
-
spanBuilderWithExplicitParent
Returns aSpanBuilderto create and start a new childSpan(or root if parent isnullor has an invalidSpanContext), with parent being the designatedSpan.See
SpanBuilderfor usage examples.This must be used to create a
Spanwhen manual Context propagation is used OR when creating a rootSpanwith anullparent.- Parameters:
spanName- The name of the returned Span.parent- The parent of the returned Span. IfnulltheSpanBuilderwill build a rootSpan.- Returns:
- a
SpanBuilderto create and start a newSpan. - Throws:
NullPointerException- ifspanNameisnull.- Since:
- 0.5
-
spanBuilderWithRemoteParent
public abstract SpanBuilder spanBuilderWithRemoteParent(String spanName, @Nullable SpanContext remoteParentSpanContext) Returns aSpanBuilderto create and start a new childSpan(or root if parent isSpanContext.INVALIDornull), with parent being the remoteSpandesignated by theSpanContext.See
SpanBuilderfor usage examples.This must be used to create a
Spanwhen the parent is in a different process. This is only intended for use by RPC systems or similar.If no
SpanContextOR fail to parse theSpanContexton the server side, users must call this method with anullremote parentSpanContext.- Parameters:
spanName- The name of the returned Span.remoteParentSpanContext- The remote parent of the returned Span.- Returns:
- a
SpanBuilderto create and start a newSpan. - Throws:
NullPointerException- ifspanNameisnull.- Since:
- 0.5
-