Class BaseTracer
- java.lang.Object
-
- io.opentelemetry.instrumentation.api.tracer.BaseTracer
-
- Direct Known Subclasses:
DatabaseClientTracer,HttpClientTracer,HttpServerTracer,RpcClientTracer,RpcServerTracer
public abstract class BaseTracer extends Object
Base class for all instrumentation specific tracer implementations.Tracers should not use
Spandirectly in their public APIs: ideally all lifecycle methods (ex. start/end methods) should return/acceptContext. By convention,Contextshould be passed to all methods as the first parameter.The
BaseTraceroffers severalstartSpan()utility methods for creating bare spans without any attributes. If you want to provide some additional attributes on span start please consider writing your own specificstartSpan()method in your tracer.A
Contextreturned by anystartSpan()method will always contain a new span. If there is a need to suppress span creationshouldStartSpan(Context, SpanKind)should be called beforestartSpan().When constructing
Spans tracers should set all attributes available during construction on aSpanBuilderinstead of aSpan. This waySpanProcessors are able to see those attributes in theonStart()method and can freely read/modify them.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseTracer()Deprecated.always pass an OpenTelemetry instance.protectedBaseTracer(io.opentelemetry.api.OpenTelemetry openTelemetry)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidend(io.opentelemetry.context.Context context)Ends the execution of a span stored in the passedcontext.voidend(io.opentelemetry.context.Context context, long endTimeNanos)Ends the execution of a span stored in the passedcontext.voidendExceptionally(io.opentelemetry.context.Context context, Throwable throwable)Records thethrowablein the span stored in the passedcontextand marks the end of the span's execution.voidendExceptionally(io.opentelemetry.context.Context context, Throwable throwable, long endTimeNanos)Records thethrowablein the span stored in the passedcontextand marks the end of the span's execution.<C> io.opentelemetry.context.Contextextract(C carrier, io.opentelemetry.context.propagation.TextMapGetter<C> getter)Extracts aContextfromcarrierusing the propagator embedded in this tracer.protected abstract StringgetInstrumentationName()The name of the instrumentation library, not the name of the instrument*ed* library.protected StringgetVersion()The version of the instrumentation library - defaults to the value of JAR manifest attributeImplementation-Version.<C> voidinject(io.opentelemetry.context.Context context, C carrier, io.opentelemetry.context.propagation.TextMapSetter<C> setter)Injectscontextdata intocarrierusing the propagator embedded in this tracer.voidonException(io.opentelemetry.context.Context context, Throwable throwable)Records thethrowablein the span stored in the passedcontextand sets the span's status toStatusCode.ERROR.booleanshouldStartSpan(io.opentelemetry.context.Context context, io.opentelemetry.api.trace.SpanKind proposedKind)Returns true if a new span of theproposedKindshould be suppressed.protected io.opentelemetry.api.trace.SpanBuilderspanBuilder(io.opentelemetry.context.Context parentContext, String spanName, io.opentelemetry.api.trace.SpanKind kind)Returns aSpanBuilderto create and start a newSpan.io.opentelemetry.context.ContextstartSpan(io.opentelemetry.context.Context parentContext, String spanName, io.opentelemetry.api.trace.SpanKind kind)Returns aContextinheriting fromparentContextthat contains a new span with namespanNameand kindkind.io.opentelemetry.context.ContextstartSpan(String spanName)Returns aContextinheriting fromContext.current()that contains a new span with namespanNameand kindSpanKind.INTERNAL.io.opentelemetry.context.ContextstartSpan(String spanName, io.opentelemetry.api.trace.SpanKind kind)Returns aContextinheriting fromContext.current()that contains a new span with namespanNameand kindkind.protected ThrowableunwrapThrowable(Throwable throwable)Extracts the actual cause by unwrapping passedthrowablefrom known wrapper exceptions, e.gExecutionException.protected io.opentelemetry.context.ContextwithClientSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.CLIENTspan.protected io.opentelemetry.context.ContextwithConsumerSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.CONSUMERspan.protected io.opentelemetry.context.ContextwithServerSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.SERVERspan.
-
-
-
Constructor Detail
-
BaseTracer
@Deprecated protected BaseTracer()
Deprecated.always pass an OpenTelemetry instance.Instead of using this always pass an OpenTelemetry instance; javaagent tracers should explicitly passGlobalOpenTelemetry.get()to the constructor.
-
BaseTracer
protected BaseTracer(io.opentelemetry.api.OpenTelemetry openTelemetry)
-
-
Method Detail
-
getInstrumentationName
protected abstract String getInstrumentationName()
The name of the instrumentation library, not the name of the instrument*ed* library. The value returned by this method should uniquely identify the instrumentation library so that during troubleshooting it's possible to pinpoint what tracer produced problematic telemetry.In this project we use a convention to encode the version of the instrument*ed* library into the instrumentation name, for example
io.opentelemetry.apache-httpclient-4.0. This way, if there are different instrumentations for different library versions it's easy to find out which instrumentations produced the telemetry data.- See Also:
TracerProvider.get(String, String)
-
getVersion
protected String getVersion()
The version of the instrumentation library - defaults to the value of JAR manifest attributeImplementation-Version.
-
shouldStartSpan
public final boolean shouldStartSpan(io.opentelemetry.context.Context context, io.opentelemetry.api.trace.SpanKind proposedKind)Returns true if a new span of theproposedKindshould be suppressed.If the passed
contextcontains aSpanKind.SERVERspan the instrumentation must not create anotherSERVERspan. The same is true for aSpanKind.CLIENTspan: if oneCLIENTspan is already present in the passedcontextthen another one must not be started.
-
startSpan
public io.opentelemetry.context.Context startSpan(String spanName)
Returns aContextinheriting fromContext.current()that contains a new span with namespanNameand kindSpanKind.INTERNAL.
-
startSpan
public io.opentelemetry.context.Context startSpan(String spanName, io.opentelemetry.api.trace.SpanKind kind)
Returns aContextinheriting fromContext.current()that contains a new span with namespanNameand kindkind.
-
startSpan
public io.opentelemetry.context.Context startSpan(io.opentelemetry.context.Context parentContext, String spanName, io.opentelemetry.api.trace.SpanKind kind)Returns aContextinheriting fromparentContextthat contains a new span with namespanNameand kindkind.
-
spanBuilder
protected final io.opentelemetry.api.trace.SpanBuilder spanBuilder(io.opentelemetry.context.Context parentContext, String spanName, io.opentelemetry.api.trace.SpanKind kind)Returns aSpanBuilderto create and start a newSpan.
-
withClientSpan
protected final io.opentelemetry.context.Context withClientSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.CLIENTspan.- See Also:
shouldStartSpan(Context, SpanKind)
-
withServerSpan
protected final io.opentelemetry.context.Context withServerSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.SERVERspan.- See Also:
shouldStartSpan(Context, SpanKind)
-
withConsumerSpan
protected final io.opentelemetry.context.Context withConsumerSpan(io.opentelemetry.context.Context parentContext, io.opentelemetry.api.trace.Span span)Returns aContextcontaining the passedspanmarked as the currentSpanKind.CONSUMERspan.- See Also:
shouldStartSpan(Context, SpanKind)
-
end
public void end(io.opentelemetry.context.Context context)
Ends the execution of a span stored in the passedcontext.
-
end
public void end(io.opentelemetry.context.Context context, long endTimeNanos)Ends the execution of a span stored in the passedcontext.- Parameters:
endTimeNanos- Explicit nanoseconds timestamp from the epoch.
-
endExceptionally
public void endExceptionally(io.opentelemetry.context.Context context, Throwable throwable)Records thethrowablein the span stored in the passedcontextand marks the end of the span's execution.- See Also:
onException(Context, Throwable),end(Context)
-
endExceptionally
public void endExceptionally(io.opentelemetry.context.Context context, Throwable throwable, long endTimeNanos)Records thethrowablein the span stored in the passedcontextand marks the end of the span's execution.- Parameters:
endTimeNanos- Explicit nanoseconds timestamp from the epoch.- See Also:
onException(Context, Throwable),end(Context, long)
-
onException
public void onException(io.opentelemetry.context.Context context, Throwable throwable)Records thethrowablein the span stored in the passedcontextand sets the span's status toStatusCode.ERROR. The throwable is unwrapped (unwrapThrowable(Throwable)) before being added to the span.
-
unwrapThrowable
protected Throwable unwrapThrowable(Throwable throwable)
Extracts the actual cause by unwrapping passedthrowablefrom known wrapper exceptions, e.gExecutionException.
-
extract
public <C> io.opentelemetry.context.Context extract(C carrier, io.opentelemetry.context.propagation.TextMapGetter<C> getter)Extracts aContextfromcarrierusing the propagator embedded in this tracer. This method can be used to propagateContextpassed from upstream services.- See Also:
TextMapPropagator.extract(Context, Object, TextMapGetter)
-
inject
public <C> void inject(io.opentelemetry.context.Context context, C carrier, io.opentelemetry.context.propagation.TextMapSetter<C> setter)Injectscontextdata intocarrierusing the propagator embedded in this tracer. This method can be used to propagate passedcontextto downstream services.- See Also:
TextMapPropagator.inject(Context, Object, TextMapSetter)
-
-