Class OpenTelemetryTracer
- All Implemented Interfaces:
Tracer
Span and in-process
context propagation. Singleton OpenTelemetry tracer capable of starting and exporting spans.
This helper class supports W3C distributed tracing protocol and injects SpanContext into the outgoing HTTP and AMQP requests.
-
Field Summary
Fields inherited from interface com.azure.core.util.tracing.Tracer
DIAGNOSTIC_ID_KEY, DISABLE_TRACING_KEY, ENTITY_PATH_KEY, HOST_NAME_KEY, MESSAGE_ENQUEUED_TIME, PARENT_SPAN_KEY, PARENT_TRACE_CONTEXT_KEY, SCOPE_KEY, SPAN_BUILDER_KEY, SPAN_CONTEXT_KEY, USER_SPAN_NAME_KEY -
Constructor Summary
ConstructorsConstructorDescriptionCreates newOpenTelemetryTracerusing default global tracer -GlobalOpenTelemetry.getTracer(String) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEvent(String eventName, Map<String, Object> traceEventAttributes, OffsetDateTime timestamp, Context context) Adds an event to the span present in theContextwith the providedtimestampandattributes.voidCompletes span on the context.extractContext(Function<String, String> headerGetter) Extracts the span's context asContextfrom upstream.voidinjectContext(BiConsumer<String, String> headerSetter, Context context) Injects tracing context.booleanChecks if tracer is enabled.booleanisRecording(Context context) Checks if span is sampled in.makeSpanCurrent(Context context) Makes span current.voidsetAttribute(String key, long value, Context context) Sets long attribute.voidsetAttribute(String key, Object value, Context context) Sets an attribute on span.voidsetAttribute(String key, String value, Context context) Adds metadata to the current span.Creates a new tracing span.start(String spanName, StartSpanOptions options, Context context) Creates a new tracing span.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.azure.core.util.tracing.Tracer
addEvent, addLink, end, extractContext, getSharedSpanBuilder, setSpanName, start
-
Constructor Details
-
OpenTelemetryTracer
public OpenTelemetryTracer()Creates newOpenTelemetryTracerusing default global tracer -GlobalOpenTelemetry.getTracer(String)
-
-
Method Details
-
start
Creates a new tracing span.The
contextwill be checked for information about a parent span. If a parent span is found, the new span will be added as a child. Otherwise, the parent span will be created and added to thecontextand any downstreamstart()calls will use the created span as the parent.Code samples
Starts a tracing span with provided method name and explicit parent span
// start a new tracing span with given name and parent context implicitly propagated // in io.opentelemetry.context.Context.current() Throwable throwable = null; Context span = tracer.start("keyvault.setsecret", Context.NONE); try { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, span); } -
start
Creates a new tracing span.The
contextwill be checked for information about a parent span. If a parent span is found, the new span will be added as a child. Otherwise, the parent span will be created and added to thecontextand any downstreamstart()calls will use the created span as the parent.Code samples
Starts a tracing span with provided method name and explicit parent span
// start a new CLIENT tracing span with the given start options and explicit parent context StartSpanOptions options = new StartSpanOptions(SpanKind.CLIENT) .setAttribute("key", "value"); Context spanFromOptions = tracer.start("keyvault.setsecret", options, Context.NONE); try { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, spanFromOptions); } -
injectContext
Injects tracing context.Context httpSpan = tracer.start("HTTP GET", new StartSpanOptions(SpanKind.CLIENT), methodSpan); tracer.injectContext((headerName, headerValue) -> request.setHeader(headerName, headerValue), httpSpan); try (AutoCloseable scope = tracer.makeSpanCurrent(httpSpan)) { HttpResponse response = getResponse(request); httpResponseCode = response.getStatusCode(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(httpResponseCode, throwable, httpSpan); }- Specified by:
injectContextin interfaceTracer- Parameters:
headerSetter- callback to set context with.context- trace context instance
-
setAttribute
Sets long attribute.Context span = tracer.start("EventHubs.process", Context.NONE); tracer.setAttribute("foo", 42, span);- Specified by:
setAttributein interfaceTracer- Parameters:
key- attribute namevalue- atteribute valuecontext- tracing context
-
setAttribute
Adds metadata to the current span. If no span information is found in the context, then no metadata is added.span = tracer.start("EventHubs.process", Context.NONE); tracer.setAttribute("bar", "baz", span);- Specified by:
setAttributein interfaceTracer- Parameters:
key- Name of the metadata.value- Value of the metadata.context- Additional metadata that is passed through the call stack.
-
setAttribute
Sets an attribute on span. Adding duplicate attributes, update, or removal is discouraged, since underlying implementations behavior can vary.- Specified by:
setAttributein interfaceTracer- Parameters:
key- attribute key.value- attribute value. Note that underlying tracer implementations limit supported value types. OpenTelemetry implementation supports following types:Stringintdoublebooleanlong
context- context containing span to which attribute is added.
-
end
Completes span on the context.Code samples
Completes the tracing span with unset status
Context messageSpan = tracer.start("ServiceBus.message", new StartSpanOptions(SpanKind.PRODUCER), Context.NONE); tracer.end(null, null, messageSpan);Completes the tracing span with provided error message
Context span = tracer.start("ServiceBus.send", new StartSpanOptions(SpanKind.CLIENT), Context.NONE); tracer.end("amqp:not-found", null, span);Completes the tracing span with provided exception
Context sendSpan = tracer.start("ServiceBus.send", new StartSpanOptions(SpanKind.CLIENT), Context.NONE); try (AutoCloseable scope = tracer.makeSpanCurrent(sendSpan)) { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, sendSpan); }- Specified by:
endin interfaceTracer- Parameters:
errorMessage- The error message that occurred during the call, ornullif no error. occurred. Any other non-null string indicates an error with description provided inerrorMessage.throwable-Throwablethat happened during the span ornullif no exception occurred.context- Additional metadata that is passed through the call stack.
-
extractContext
Extracts the span's context asContextfrom upstream.Code samples
Extracts the corresponding span context information from a valid diagnostic id
Context parentContext = tracer.extractContext(name -> { Object value = messageProperties.get(name); return value instanceof String ? (String) value : null; }); StartSpanOptions remoteParentOptions = new StartSpanOptions(SpanKind.CONSUMER) .setRemoteParent(parentContext); Context spanWithRemoteParent = tracer.start("EventHubs.process", remoteParentOptions, Context.NONE); try (AutoCloseable scope = tracer.makeSpanCurrent(spanWithRemoteParent)) { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, spanWithRemoteParent); }- Specified by:
extractContextin interfaceTracer- Parameters:
headerGetter- Unique identifier for the trace information of the span and todo.- Returns:
- The updated
Contextobject containing the span context.
-
isRecording
Checks if span is sampled in.- Specified by:
isRecordingin interfaceTracer- Parameters:
context- Span to check.- Returns:
- true if span is recording, false otherwise.
-
makeSpanCurrent
Makes span current. Implementations may put it on ThreadLocal. Make sure to always use try-with-resource statement with makeSpanCurrent- Specified by:
makeSpanCurrentin interfaceTracer- Parameters:
context- Context with span.Context span = tracer.start("EventHubs.process", new StartSpanOptions(SpanKind.CONSUMER), Context.NONE); try (AutoCloseable scope = tracer.makeSpanCurrent(span)) { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, span); }- Returns:
- Closeable that should be closed in the same thread with try-with-resource statement.
-
addEvent
public void addEvent(String eventName, Map<String, Object> traceEventAttributes, OffsetDateTime timestamp, Context context) Adds an event to the span present in theContextwith the providedtimestampandattributes.This API does not provide any normalization if provided timestamps are out of range of the current span timeline
Supported attribute values include String, double, boolean, long, String [], double [], long []. Any other Object value type and null values will be silently ignored.
Context span = tracer.start("Cosmos.getItem", Context.NONE); tracer.addEvent("trying another endpoint", Collections.singletonMap("endpoint", "westus3"), OffsetDateTime.now(), span);- Specified by:
addEventin interfaceTracer- Parameters:
eventName- the name of the event.traceEventAttributes- the additional attributes to be set for the event.timestamp- The instant, in UTC, at which the event will be associated to the span.context- the call metadata containing information of the span to which the event should be associated with.
-
isEnabled
public boolean isEnabled()Description copied from interface:com.azure.core.util.tracing.TracerChecks if tracer is enabled.if (!tracer.isEnabled()) { doWork(); } else { Context span = tracer.start("span", Context.NONE); try { doWork(); } catch (Throwable ex) { throwable = ex; } finally { tracer.end(null, throwable, span); } }
-