Interface Tracer
-
public interface TracerContract that all tracers must implement to be pluggable into the SDK.- See Also:
TracerProxy
-
-
Field Summary
Fields Modifier and Type Field Description static StringAZ_TRACING_NAMESPACE_KEYKey forContextwhich indicates that the context contains the Azure resource provider namespace.static StringDIAGNOSTIC_ID_KEYKey forContextwhich indicates that the context contains a "Diagnostic Id" for the service call.static StringDISABLE_TRACING_KEYKey forContextwhich disables tracing for the request associated with the current context.static StringENTITY_PATH_KEYKey forContextwhich indicates that the context contains an entity path.static StringHOST_NAME_KEYKey forContextwhich indicates that the context contains the hostname.static StringMESSAGE_ENQUEUED_TIMEKey forContextwhich indicates the time of the last enqueued message in the partition's stream.static StringPARENT_SPAN_KEYDeprecated.Deprecated in favor of PARENT_TRACE_CONTEXT_KEY, use it to propagate full io.opentelemetry.Contextstatic StringPARENT_TRACE_CONTEXT_KEYContextkey to store trace context.static StringSCOPE_KEYKey forContextthe scope of code where the given Span is in the current Context.static StringSPAN_BUILDER_KEYKey forContextwhich indicates the shared span builder that is in the current Context.static StringSPAN_CONTEXT_KEYKey forContextwhich indicates that the context contains a message span context.static StringUSER_SPAN_NAME_KEYKey forContextwhich indicates that the context contains the name for the user spans that are created.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default voidaddEvent(String name, Map<String,Object> attributes, OffsetDateTime timestamp)Deprecated.default voidaddEvent(String name, Map<String,Object> attributes, OffsetDateTime timestamp, Context context)Adds an event to the span present in theContextwith the providedtimestampandattributes.voidaddLink(Context context)Provides a way to link multiple tracing spans.voidend(int responseCode, Throwable error, Context context)Completes the current tracing span.voidend(String statusMessage, Throwable error, Context context)Completes the current tracing span for AMQP calls.ContextextractContext(String diagnosticId, Context context)Extracts the span's context asContextfrom upstream.default ContextgetSharedSpanBuilder(String spanName, Context context)Returns a span builder with the provided name inContext.default AutoCloseablemakeSpanCurrent(Context context)Makes span current.voidsetAttribute(String key, String value, Context context)Adds metadata to the current span.ContextsetSpanName(String spanName, Context context)Sets the name for spans that are created.Contextstart(String methodName, Context context)Creates a new tracing span.Contextstart(String methodName, Context context, ProcessKind processKind)Creates a new tracing span for AMQP calls.default Contextstart(String methodName, StartSpanOptions options, Context context)Creates a new tracing span.
-
-
-
Field Detail
-
PARENT_SPAN_KEY
@Deprecated static final String PARENT_SPAN_KEY
Deprecated.Deprecated in favor of PARENT_TRACE_CONTEXT_KEY, use it to propagate full io.opentelemetry.ContextKey forContextwhich indicates that the context contains parent span data. This span will be used as the parent span for all spans the SDK creates.If no span data is listed when the span is created it will default to using this span key as the parent span.
- See Also:
- Constant Field Values
-
PARENT_TRACE_CONTEXT_KEY
static final String PARENT_TRACE_CONTEXT_KEY
Contextkey to store trace context. This context will be used as a parent context for new spans and propagated in outgoing HTTP calls.- See Also:
- Constant Field Values
-
USER_SPAN_NAME_KEY
static final String USER_SPAN_NAME_KEY
Key forContextwhich indicates that the context contains the name for the user spans that are created.If no span name is listed when the span is created it will default to using the calling method's name.
- See Also:
- Constant Field Values
-
ENTITY_PATH_KEY
static final String ENTITY_PATH_KEY
Key forContextwhich indicates that the context contains an entity path.- See Also:
- Constant Field Values
-
HOST_NAME_KEY
static final String HOST_NAME_KEY
Key forContextwhich indicates that the context contains the hostname.- See Also:
- Constant Field Values
-
SPAN_CONTEXT_KEY
static final String SPAN_CONTEXT_KEY
Key forContextwhich indicates that the context contains a message span context.- See Also:
- Constant Field Values
-
DIAGNOSTIC_ID_KEY
static final String DIAGNOSTIC_ID_KEY
Key forContextwhich indicates that the context contains a "Diagnostic Id" for the service call.- See Also:
- Constant Field Values
-
SCOPE_KEY
static final String SCOPE_KEY
Key forContextthe scope of code where the given Span is in the current Context.- See Also:
- Constant Field Values
-
AZ_TRACING_NAMESPACE_KEY
static final String AZ_TRACING_NAMESPACE_KEY
Key forContextwhich indicates that the context contains the Azure resource provider namespace.- See Also:
- Constant Field Values
-
SPAN_BUILDER_KEY
static final String SPAN_BUILDER_KEY
Key forContextwhich indicates the shared span builder that is in the current Context.- See Also:
- Constant Field Values
-
MESSAGE_ENQUEUED_TIME
static final String MESSAGE_ENQUEUED_TIME
Key forContextwhich indicates the time of the last enqueued message in the partition's stream.- See Also:
- Constant Field Values
-
DISABLE_TRACING_KEY
static final String DISABLE_TRACING_KEY
Key forContextwhich disables tracing for the request associated with the current context.- See Also:
- Constant Field Values
-
-
Method Detail
-
start
Context start(String methodName, Context context)
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() Context traceContext = tracer.start("keyvault.setsecret", Context.NONE); System.out.printf("OpenTelemetry Context with started `keyvault.setsecret` span: %s%n", traceContext.getData(PARENT_TRACE_CONTEXT_KEY).get());- Parameters:
methodName- Name of the method triggering the span creation.context- Additional metadata that is passed through the call stack.- Returns:
- The updated
Contextobject containing the returned span. - Throws:
NullPointerException- ifmethodNameorcontextisnull.
-
start
default Context start(String methodName, StartSpanOptions options, Context context)
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 updatedClientSpanContext = tracer.start("keyvault.setsecret", options, traceContext); System.out.printf("OpenTelemetry Context with started `keyvault.setsecret` span: %s%n", updatedClientSpanContext.getData(PARENT_TRACE_CONTEXT_KEY).get());- Parameters:
methodName- Name of the method triggering the span creation.options- span creation options.context- Additional metadata that is passed through the call stack.- Returns:
- The updated
Contextobject containing the returned span. - Throws:
NullPointerException- ifoptionsorcontextisnull.
-
start
Context start(String methodName, Context context, ProcessKind processKind)
Creates a new tracing span for AMQP calls.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.Sets additional request attributes on the created span when
processKindisProcessKind.SEND.Returns the diagnostic Id and span context of the returned span when
processKindisProcessKind.MESSAGE.Creates a new tracing span with remote parent and returns that scope when the given when
processKindisProcessKind.PROCESS.Code samples
Starts a tracing span with provided method name and AMQP operation SEND
// pass request metadata to the calling method Context sendContext = new Context(ENTITY_PATH_KEY, "entity-path").addData(HOST_NAME_KEY, "hostname"); // start a new tracing span with explicit parent, sets the request attributes on the span and sets the span // kind to client when process kind SEND Context updatedSendContext = tracer.start("eventhubs.send", sendContext, ProcessKind.SEND); System.out.printf("OpenTelemetry Context with started `eventhubs.send` span: %s%n", updatedSendContext.getData(PARENT_TRACE_CONTEXT_KEY).get());Starts a tracing span with provided method name and AMQP operation MESSAGE
String diagnosticIdKey = "Diagnostic-Id"; // start a new tracing span with explicit parent, sets the diagnostic Id (traceparent headers) on the current // context when process kind MESSAGE Context updatedReceiveContext = tracer.start("EventHubs.receive", traceContext, ProcessKind.MESSAGE); System.out.printf("Diagnostic Id: %s%n", updatedReceiveContext.getData(diagnosticIdKey).get());Starts a tracing span with provided method name and AMQP operation PROCESS
String spanImplContext = "span-context"; // start a new tracing span with remote parent and uses current context to return a scope // when process kind PROCESS Context processContext = new Context(PARENT_TRACE_CONTEXT_KEY, "<OpenTelemetry-context>") .addData(spanImplContext, "<user-current-span-context>"); Context updatedProcessContext = tracer.start("EventHubs.process", processContext, ProcessKind.PROCESS); System.out.printf("Scope: %s%n", updatedProcessContext.getData("scope").get());- Parameters:
methodName- Name of the method triggering the span creation.context- Additional metadata that is passed through the call stack.processKind- AMQP operation kind.- Returns:
- The updated
Contextobject containing the returned span. - Throws:
NullPointerException- ifmethodNameorcontextorprocessKindisnull.
-
end
void end(int responseCode, Throwable error, Context context)Completes the current tracing span.Code samples
Completes the tracing span present in the context, with the corresponding OpenTelemetry status for the given response status code
// context containing the span to end String openTelemetrySpanKey = "openTelemetry-span"; Context traceContext = new Context(PARENT_TRACE_CONTEXT_KEY, "<user-current-span>"); // completes the tracing span with the passed response status code tracer.end(200, null, traceContext);
- Parameters:
responseCode- Response status code if the span is in an HTTP call context.error-Throwablethat happened during the span ornullif no exception occurred.context- Additional metadata that is passed through the call stack.- Throws:
NullPointerException- ifcontextisnull.
-
end
void end(String statusMessage, Throwable error, Context context)
Completes the current tracing span for AMQP calls.Code samples
Completes the tracing span with the corresponding OpenTelemetry status for the given status message
// context containing the current trace context to end // completes the tracing span with the passed status message tracer.end("success", null, traceContext);- Parameters:
statusMessage- The error or success message that occurred during the call, ornullif no error occurred.error-Throwablethat happened during the span ornullif no exception occurred.context- Additional metadata that is passed through the call stack.- Throws:
NullPointerException- ifcontextisnull.
-
setAttribute
void setAttribute(String key, String value, Context context)
Adds metadata to the current span. If no span information is found in the context, then no metadata is added.- Parameters:
key- Name of the metadata.value- Value of the metadata.context- Additional metadata that is passed through the call stack.- Throws:
NullPointerException- ifkeyorvalueorcontextisnull.
-
setSpanName
Context setSpanName(String spanName, Context context)
Sets the name for spans that are created.Code samples
Retrieve the span name of the returned span
// Sets future span name - it will be used when span will be started on this context Context contextWithName = tracer.setSpanName("keyvault.setsecret", Context.NONE); Context traceContext = tracer.start("placeholder", contextWithName); System.out.printf("OpenTelemetry Context with started `keyvault.setsecret` span: %s%n", traceContext.getData(PARENT_TRACE_CONTEXT_KEY).get());- Parameters:
spanName- Name to give the next span.context- Additional metadata that is passed through the call stack.- Returns:
- The updated
Contextobject containing the name of the returned span. - Throws:
NullPointerException- ifspanNameorcontextisnull.
-
addLink
void addLink(Context context)
Provides a way to link multiple tracing spans. Used in batching operations to relate multiple requests under a single batch.Code samples
Link multiple spans using their span context information
// start a new tracing span with given name and parent context implicitly propagated // in io.opentelemetry.context.Context.current() Context spanContext = tracer.start("test.method", Context.NONE, ProcessKind.MESSAGE); // Adds a link between multiple span's using the span context information of the Span // For each event processed, add a link with the created spanContext tracer.addLink(spanContext);- Parameters:
context- Additional metadata that is passed through the call stack.- Throws:
NullPointerException- ifcontextisnull.
-
extractContext
Context extractContext(String diagnosticId, Context context)
Extracts the span's context asContextfrom upstream.Code samples
Extracts the corresponding span context information from a valid diagnostic id
// Extracts the span context information from the passed diagnostic Id that can be used for linking spans. String spanImplContext = "span-context"; Context spanContext = tracer.extractContext("valid-diagnostic-id", Context.NONE); System.out.printf("Span context of the current tracing span: %s%n", spanContext.getData(spanImplContext).get());- Parameters:
diagnosticId- Unique identifier for the trace information of the span.context- Additional metadata that is passed through the call stack.- Returns:
- The updated
Contextobject containing the span context. - Throws:
NullPointerException- ifdiagnosticIdorcontextisnull.
-
getSharedSpanBuilder
default Context getSharedSpanBuilder(String spanName, Context context)
Returns a span builder with the provided name inContext.Code samples
Returns a builder with the provided span name.
// Returns a span builder with the provided name Context spanContext = tracer.getSharedSpanBuilder("message-span", Context.NONE); System.out.printf("Builder of current span being built: %s%n", spanContext.getData(SPAN_BUILDER_KEY).get());- Parameters:
spanName- Name to give the span for the created builder.context- Additional metadata that is passed through the call stack.- Returns:
- The updated
Contextobject containing the span builder. - Throws:
NullPointerException- ifcontextorspanNameisnull.
-
addEvent
@Deprecated default void addEvent(String name, Map<String,Object> attributes, OffsetDateTime timestamp)
Deprecated.Adds an event to the current span with 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.
- Parameters:
name- the name of the event.attributes- the additional attributes to be set for the event.timestamp- The instant, in UTC, at which the event will be associated to the span.- Throws:
NullPointerException- ifeventNameisnull.
-
addEvent
default void addEvent(String name, Map<String,Object> attributes, 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.
- Parameters:
name- the name of the event.attributes- 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.- Throws:
NullPointerException- ifeventNameisnull.
-
makeSpanCurrent
default AutoCloseable makeSpanCurrent(Context context)
Makes span current. Implementations may put it on ThreadLocal. Make sure to always use try-with-resource statement with makeSpanCurrent- Parameters:
context- Context with span.Code samples
Starts a tracing span, makes it current and ends it
// Starts a span, makes it current and then stops it. Context traceContext = tracer.start("EventHubs.process", Context.NONE); // Make sure to always use try-with-resource statement with makeSpanCurrent try (AutoCloseable ignored = tracer.makeSpanCurrent(traceContext)) { System.out.println("doing some work..."); } catch (Throwable throwable) { tracer.end("Failure", throwable, traceContext); } finally { tracer.end("OK", null, traceContext); }- Returns:
- Closeable that should be closed in the same thread with try-with-resource statement.
-
-