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 Constructor Description BaseTracer()Deprecated.always pass an OpenTelemetry instance.BaseTracer(io.opentelemetry.api.OpenTelemetry openTelemetry)
-
Method Summary
All Methods Static Methods Instance 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.<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.static StringspanNameForClass(Class<?> clazz)This method is used to generate an acceptable span (operation) name based on a given class reference.static StringspanNameForMethod(Class<?> clazz, @Nullable Method method)This method is used to generate an acceptable span (operation) name based on a given method reference.static StringspanNameForMethod(Class<?> cl, String methodName)This method is used to generate an acceptable span (operation) name based on a given method reference.static StringspanNameForMethod(Method method)This method is used to generate an acceptable span (operation) name based on a given method reference.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.
-
-
-
Constructor Detail
-
BaseTracer
@Deprecated public 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
public BaseTracer(io.opentelemetry.api.OpenTelemetry openTelemetry)
-
-
Method Detail
-
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.
-
spanNameForMethod
public static String spanNameForMethod(Method method)
This method is used to generate an acceptable span (operation) name based on a given method reference. Anonymous classes are named based on their parent.
-
spanNameForMethod
public static String spanNameForMethod(Class<?> clazz, @Nullable Method method)
This method is used to generate an acceptable span (operation) name based on a given method reference. Anonymous classes are named based on their parent.
-
spanNameForMethod
public static String spanNameForMethod(Class<?> cl, String methodName)
This method is used to generate an acceptable span (operation) name based on a given method reference. Anonymous classes are named based on their parent.
-
spanNameForClass
public static String spanNameForClass(Class<?> clazz)
This method is used to generate an acceptable span (operation) name based on a given class reference. Anonymous classes are named based on their parent.
-
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.
-
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)
-
-