Class OtelTracerHook
- All Implemented Interfaces:
TracerHook
TracerHook bridge that opens an OpenTelemetry Span for every @RestOp call.
Span shape
Each request becomes a single span with kind SpanKind.SERVER, named after the HTTP method
(e.g. "GET", "POST") per the OpenTelemetry HTTP semantic conventions. Three OTel
HTTP semantic-convention attributes are populated:
http.request.method— uppercased HTTP method.http.response.status_code— HTTP response status (set right before the span is closed, so error paths get the framework's resolved status).http.route— the@RestOppath template (e.g."/users/{id}"), not the raw concrete URI. Keeps trace cardinality bounded for the same reason the Micrometer bridge uses the template as itsuritag.
On the exception path, the throwable is recorded via Span.recordException(Throwable), the
span status is set to StatusCode.ERROR, and an exception.type attribute carries the
exception's simple-name.
W3C trace context propagation
Incoming traceparent / tracestate request headers are extracted via the configured
TextMapPropagator (default: W3CTraceContextPropagator) so the server span continues
a caller-supplied distributed trace. Downstream HTTP calls made from inside the handler will pick
up the active span's context automatically when the consumer's outbound HTTP client honors the OTel
current context (the standard OTel client instrumentations do).
Wiring
See Also:
- Since:
- 10.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final io.opentelemetry.api.common.AttributeKey<String>OTel attribute keyexception.typeused when recording a thrown exception.static final io.opentelemetry.api.common.AttributeKey<String>OTel HTTP semantic-convention attribute keyhttp.request.method.static final io.opentelemetry.api.common.AttributeKey<Long>OTel HTTP semantic-convention attribute keyhttp.response.status_code.static final io.opentelemetry.api.common.AttributeKey<String>OTel HTTP semantic-convention attribute keyhttp.route.static final StringDefault tracer instrumentation scope name. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor usingGlobalOpenTelemetry.get()as theOpenTelemetrysource.OtelTracerHook(io.opentelemetry.api.OpenTelemetry openTelemetry) Constructor with an explicitOpenTelemetryinstance.OtelTracerHook(io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator) Constructor with an explicitTracerandTextMapPropagator. -
Method Summary
Modifier and TypeMethodDescriptionio.opentelemetry.context.propagation.TextMapPropagatorReturns theTextMapPropagatorused to extract W3C trace-context headers from incoming requests.io.opentelemetry.api.trace.TracerReturns theTracerthis hook publishes spans to.voidinject(TraceContextCarrier carrier) Renders this hook's active trace context (if any) intocarrier .Opens a new span for a custom (non-request) observation.startSpan(RestRequest request) Opens a new span for the in-flight request.startSpan(RestRequest request, TraceContextCarrier carrier, TraceOperation operation) Opens a new span for the in-flight request, given a pre-extracted non-HTTPTraceContextCarrierand derivedTraceOperation.
-
Field Details
-
DEFAULT_INSTRUMENTATION_NAME
Default tracer instrumentation scope name.- See Also:
-
ATTR_HTTP_REQUEST_METHOD
OTel HTTP semantic-convention attribute keyhttp.request.method. -
ATTR_HTTP_RESPONSE_STATUS_CODE
OTel HTTP semantic-convention attribute keyhttp.response.status_code. -
ATTR_HTTP_ROUTE
OTel HTTP semantic-convention attribute keyhttp.route. -
ATTR_EXCEPTION_TYPE
OTel attribute keyexception.typeused when recording a thrown exception.
-
-
Constructor Details
-
OtelTracerHook
public OtelTracerHook()Constructor usingGlobalOpenTelemetry.get()as theOpenTelemetrysource.Convenient when the deployment installs a single global
OpenTelemetry(the dominant pattern with the OTel auto-instrumentation agent). Equivalent tonew OtelTracerHook(GlobalOpenTelemetry.get()). -
OtelTracerHook
Constructor with an explicitOpenTelemetryinstance.The instance is used to obtain both the
Tracer(named "org.apache.juneau.rest.server") and theTextMapPropagatorused for incoming / outgoing W3C trace-context header transfer.- Parameters:
openTelemetry- TheOpenTelemetryinstance. Must not benull .
-
OtelTracerHook
public OtelTracerHook(io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator) Constructor with an explicitTracerandTextMapPropagator.Useful for tests where in-memory tracer / propagator instances are wired directly without going through a full
OpenTelemetrySDK assembly.- Parameters:
tracer- TheTracerused to build per-request spans. Must not benull .propagator- TheTextMapPropagatorused for incoming / outgoing W3C trace-context header transfer. Must not benull .
-
-
Method Details
-
startSpan
Description copied from interface:TracerHookOpens a new span for the in-flight request.- Specified by:
startSpanin interfaceTracerHook- Parameters:
request- The in-flightRestRequest. Nevernull . Bridges may read headers for distributed-tracing context propagation (e.g. W3Ctraceparent/tracestate), the HTTP method, the path, etc. Bridges must not mutate the request.- Returns:
- The opened
Scope. Nevernull . Returningnullis a contract violation — bridges that cannot open a span (e.g. because the trace context is invalid) should returnNoOpTracerHook.NoOpScope.INSTANCEso the framework's close-in-finally contract still holds.
-
startSpan
Description copied from interface:TracerHookOpens a new span for the in-flight request, given a pre-extracted non-HTTPTraceContextCarrierand derivedTraceOperation.Added as a source-compatible default overload so every existing
TracerHookimplementation — including a bare lambda implementing onlyTracerHook.startSpan(RestRequest)— keeps compiling unchanged. The default implementation simply delegates toTracerHook.startSpan(RestRequest), ignoringcarrier andoperation ; a bridge only needs to override this overload if it wants to honor a non-HTTP carrier (for example MCP'sparams._meta) or name the span afteroperation instead of its own HTTP-derived default.- Specified by:
startSpanin interfaceTracerHook- Parameters:
request- The in-flightRestRequest. Nevernull .carrier- TheTraceContextCarrieran activeTraceContextExtractorrecognized among the resolved operation arguments, ornull if none was recognized (the bridge should then fall back to HTTP headers only).operation- TheTraceOperationan activeTraceContextExtractorderived for this invocation. Nevernull ;TraceOperation.DEFAULTwhen the extractor supplied no override.- Returns:
- The opened
Scope. Nevernull .
-
startSpan
Description copied from interface:TracerHookOpens a new span for a custom (non-request) observation.Unlike
TracerHook.startSpan(RestRequest), this entry point is not tied to an in-flightRestRequest— it is the substrate for the explicit programmatic observation API (Observations) so application code can trace an arbitrary block of work that has no associated HTTP request. Bridge implementations typically open a span of kindINTERNAL(rather thanSERVER) namedspanName.The default implementation returns
NoOpTracerHook.NoOpScope.INSTANCE— a bridge that does not override this method simply does not trace custom observations, and the no-backend path stays zero-allocation. Both shipped bridges (the OpenTelemetryTracerHook) override it.- Specified by:
startSpanin interfaceTracerHook- Parameters:
spanName- The span name (e.g."loadOrder"). Nevernull ; never blank.- Returns:
- The opened
Scope. Nevernull — implementations that cannot open a span must returnNoOpTracerHook.NoOpScope.INSTANCEso the close-in-finally contract holds.
-
inject
Description copied from interface:TracerHookRenders this hook's active trace context (if any) intocarrier .The substrate for
TraceContexts.inject(TracerHook, TraceContextCarrier). The default implementation is a no-op —NoOpTracerHookand any bridge that does not override this method write nothing. A bridge with an actual trace context to render (for example the OpenTelemetry bridge injuneau-rest-server-tracing-otel) overrides this to writetraceparent/tracestate/baggagekeys viaTraceContextCarrier.set(String, String).- Specified by:
injectin interfaceTracerHook- Parameters:
carrier- TheTraceContextCarrierto write into. Nevernull .
-
getTracer
Returns theTracerthis hook publishes spans to.- Returns:
- The
Tracer.
-
getPropagator
Returns theTextMapPropagatorused to extract W3C trace-context headers from incoming requests.- Returns:
- The
TextMapPropagator.
-