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
ConstructorDescriptionCreates newOpenTelemetryTracer
using default global tracer -GlobalOpenTelemetry.getTracer(String)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEvent
(String eventName, Map<String, Object> traceEventAttributes, OffsetDateTime timestamp, Context context) Adds an event to the span present in theContext
with the providedtimestamp
andattributes
.void
Completes the current tracing span for AMQP calls.extractContext
(Function<String, String> headerGetter) Extracts the span's context asContext
from upstream.void
injectContext
(BiConsumer<String, String> headerSetter, Context context) Injects tracing context.boolean
Checks if tracer is enabled.makeSpanCurrent
(Context context) Makes span current.void
setAttribute
(String key, long value, Context context) Sets long attribute.void
setAttribute
(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, wait
Methods inherited from interface com.azure.core.util.tracing.Tracer
addEvent, addLink, end, extractContext, getSharedSpanBuilder, setSpanName, start
-
Constructor Details
-
OpenTelemetryTracer
public OpenTelemetryTracer()Creates newOpenTelemetryTracer
using default global tracer -GlobalOpenTelemetry.getTracer(String)
-
-
Method Details
-
start
Creates a new tracing span.The
context
will 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 thecontext
and 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
context
will 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 thecontext
and 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
Description copied from interface:Tracer
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:
injectContext
in interfaceTracer
- Parameters:
headerSetter
- callback to set context with.context
- trace context instance
-
setAttribute
Description copied from interface:Tracer
Sets long attribute.Context span = tracer.start("EventHubs.process", Context.NONE); tracer.setAttribute("foo", 42, span);
- Specified by:
setAttribute
in 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:
setAttribute
in interfaceTracer
- Parameters:
key
- Name of the metadata.value
- Value of the metadata.context
- Additional metadata that is passed through the call stack.
-
end
Completes the current tracing span for AMQP calls.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); }
-
extractContext
Extracts the span's context asContext
from 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:
extractContext
in interfaceTracer
- Parameters:
headerGetter
- Unique identifier for the trace information of the span and todo.- Returns:
- The updated
Context
object containing the span context.
-
makeSpanCurrent
Makes span current. Implementations may put it on ThreadLocal. Make sure to always use try-with-resource statement with makeSpanCurrent- Specified by:
makeSpanCurrent
in 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 theContext
with the providedtimestamp
andattributes
.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:
addEvent
in 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:Tracer
Checks 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); } }
-