Class Instrumenter<REQUEST,RESPONSE>
- java.lang.Object
-
- io.opentelemetry.instrumentation.api.instrumenter.Instrumenter<REQUEST,RESPONSE>
-
public class Instrumenter<REQUEST,RESPONSE> extends Object
An instrumenter of the start and end of a request/response lifecycle. Almost all instrumentation of libraries falls into modeling start and end, generating observability signals from these such as a tracingSpan, or metrics such as the duration taken, active requests, etc. When instrumenting a library, there will generally be four steps.- Create an
InstrumenterusingInstrumenterBuilder. Use the builder to configure any library-specific customizations, and also expose useful knobs to your user. - Call
shouldStart(Context, Object)and do not proceed iffalse. - Call
start(Context, Object)at the beginning of a request. - Call
end(Context, Object, Object, Throwable)at the end of a request.
- Create an
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidend(io.opentelemetry.context.Context context, REQUEST request, @Nullable RESPONSE response, @Nullable Throwable error)Ends an instrumented operation.static <REQUEST,RESPONSE>
InstrumenterBuilder<REQUEST,RESPONSE>newBuilder(io.opentelemetry.api.OpenTelemetry openTelemetry, String instrumentationName, SpanNameExtractor<? super REQUEST> spanNameExtractor)Returns a newInstrumenterBuilder.booleanshouldStart(io.opentelemetry.context.Context parentContext, REQUEST request)Returns whether instrumentation should be applied for theInstrumenter.io.opentelemetry.context.Contextstart(io.opentelemetry.context.Context parentContext, REQUEST request)Starts a new operation to be instrumented.
-
-
-
Method Detail
-
newBuilder
public static <REQUEST,RESPONSE> InstrumenterBuilder<REQUEST,RESPONSE> newBuilder(io.opentelemetry.api.OpenTelemetry openTelemetry, String instrumentationName, SpanNameExtractor<? super REQUEST> spanNameExtractor)
Returns a newInstrumenterBuilder.The
instrumentationNameis the name of the instrumentation library, not the name of the instrument*ed* library. The value passed in this parameter 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 minimum supported 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.
-
shouldStart
public boolean shouldStart(io.opentelemetry.context.Context parentContext, REQUEST request)Returns whether instrumentation should be applied for theInstrumenter. Iftrue, callstart(Context, Object)andend(Context, Object, Object, Throwable)around the operation being instrumented, or iffalseexecute the operation directly without calling those methods.
-
start
public io.opentelemetry.context.Context start(io.opentelemetry.context.Context parentContext, REQUEST request)Starts a new operation to be instrumented. TheparentContextis the parent of the resulting instrumented operation and should usually beContext.current(). Therequestis the request object of this operation. The returnedContextshould be propagated along with the operation and passed toend(Context, Object, Object, Throwable)when it is finished.
-
end
public void end(io.opentelemetry.context.Context context, REQUEST request, @Nullable RESPONSE response, @Nullable Throwable error)Ends an instrumented operation. TheContextmust be what was returned fromstart(Context, Object).requestis the request object of the operation,responseis the response object of the operation, anderroris an exception that was thrown by the operation, ornullif none was thrown.
-
-