Class Spans
java.lang.Object
codes.grow.packages.skywalking.tracing.Spans
Span helpers on top of the OpenTelemetry API. Without an explicit tracer or propagators they use
Tracing.openTelemetry().
Order order = Spans.withSpan("load-order", () -> repository.load(id));
Spans.addEvent("cache-miss");
Map<String, String> headers = new HashMap<>();
Spans.inject(headers);
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdds an event to the current span.static voidAdds an event with attributes to the current span.static io.opentelemetry.api.trace.SpanThe current span (an invalid no-op span when none is active).Span id of the current span.Trace id of the current span.static io.opentelemetry.context.ContextExtracts a context from a string map (keys match case-insensitively).static io.opentelemetry.context.Contextextract(Map<String, String> carrier, io.opentelemetry.context.propagation.ContextPropagators propagators) Extracts with explicit propagators.static io.opentelemetry.context.ContextextractHeaders(Map<String, List<String>> headers) Extracts a context from a multi-valued header map (names match case-insensitively).static io.opentelemetry.context.ContextextractHeaders(Map<String, List<String>> headers, io.opentelemetry.context.propagation.ContextPropagators propagators) Extracts from a multi-valued header map with explicit propagators.fields(io.opentelemetry.context.propagation.ContextPropagators propagators) Names of the propagation fields of the given propagators.headersFor(io.opentelemetry.context.propagation.ContextPropagators propagators) The headers a request should carry for the current context.static voidinject(io.opentelemetry.context.Context context, Map<String, String> carrier, io.opentelemetry.context.propagation.ContextPropagators propagators) Injects a context with explicit propagators.static voidInjects the current context into a string map (for example message headers).static voidinjectHeaders(io.opentelemetry.context.Context context, Map<String, List<String>> headers, io.opentelemetry.context.propagation.ContextPropagators propagators) Injects a context into a multi-valued header map with explicit propagators.static voidinjectHeaders(Map<String, List<String>> headers) Injects the current context into a multi-valued header map (as used byjava.net.http.HttpHeadersor servlet APIs); existing propagation headers are replaced.static voidrecordException(Throwable error) Records an exception event on the current span (status unchanged).static voidRecords the exception and sets status ERROR onspan.static voidSets status ERROR with a message on the current span.static voidRecords the exception and sets status ERROR on the current span.static io.opentelemetry.api.trace.Spanstart(String name, SpanOptions options) Starts (but does not end or activate) a span with options.Log correlation fields of the current span (trace_id,span_id; snake_case on purpose, the log field convention).static io.opentelemetry.api.trace.Tracertracer()The tracer the helpers use by default.static <E extends Throwable>
voidwithSpan(String name, SpanOptions options, ThrowingRunnable<E> fn) Runsfnin a new span with options.static <T,E extends Throwable>
TwithSpan(String name, SpanOptions options, ThrowingSupplier<T, E> fn) Runsfnin a new span with options.static <E extends Throwable>
voidwithSpan(String name, ThrowingRunnable<E> fn) Runsfnin a new internal span.static <T,E extends Throwable>
TwithSpan(String name, ThrowingSupplier<T, E> fn) Runsfnin a new internal span that is current while it runs.
-
Method Details
-
tracer
public static io.opentelemetry.api.trace.Tracer tracer()The tracer the helpers use by default.- Returns:
- tracer of
Tracing.openTelemetry()
-
withSpan
Runsfnin a new internal span that is current while it runs. The span ends whenfnreturns; an exception is recorded, sets status ERROR and is rethrown.- Type Parameters:
T- result typeE- exception type- Parameters:
name- span namefn- work- Returns:
- the result of
fn - Throws:
E- whatfnthrows
-
withSpan
public static <T,E extends Throwable> T withSpan(String name, SpanOptions options, ThrowingSupplier<T, E> fn) throws ERunsfnin a new span with options.- Type Parameters:
T- result typeE- exception type- Parameters:
name- span nameoptions- attributes, kind, links, tracer, parentfn- work- Returns:
- the result of
fn - Throws:
E- whatfnthrows
-
withSpan
Runsfnin a new internal span.- Type Parameters:
E- exception type- Parameters:
name- span namefn- work- Throws:
E- whatfnthrows
-
withSpan
public static <E extends Throwable> void withSpan(String name, SpanOptions options, ThrowingRunnable<E> fn) throws E Runsfnin a new span with options.- Type Parameters:
E- exception type- Parameters:
name- span nameoptions- optionsfn- work- Throws:
E- whatfnthrows
-
start
Starts (but does not end or activate) a span with options.- Parameters:
name- span nameoptions- options- Returns:
- started span
-
currentSpan
public static io.opentelemetry.api.trace.Span currentSpan()The current span (an invalid no-op span when none is active).- Returns:
- span
-
currentTraceId
Trace id of the current span.- Returns:
- 32 lower-hex characters, or empty when no valid span is active
-
currentSpanId
Span id of the current span.- Returns:
- 16 lower-hex characters, or empty when no valid span is active
-
addEvent
Adds an event to the current span.- Parameters:
name- event name
-
addEvent
Adds an event with attributes to the current span.- Parameters:
name- event nameattributes- attribute values
-
recordException
Records an exception event on the current span (status unchanged).- Parameters:
error- exception
-
setError
Records the exception and sets status ERROR on the current span.- Parameters:
error- exception
-
setError
Sets status ERROR with a message on the current span.- Parameters:
message- status description
-
setError
Records the exception and sets status ERROR onspan.- Parameters:
span- spanerror- exception
-
inject
Injects the current context into a string map (for example message headers).- Parameters:
carrier- mutable map
-
inject
public static void inject(io.opentelemetry.context.Context context, Map<String, String> carrier, io.opentelemetry.context.propagation.ContextPropagators propagators) Injects a context with explicit propagators.- Parameters:
context- contextcarrier- mutable mappropagators- propagators
-
extract
Extracts a context from a string map (keys match case-insensitively).- Parameters:
carrier- map- Returns:
- context (the current context plus the extracted span context and baggage)
-
extract
public static io.opentelemetry.context.Context extract(Map<String, String> carrier, io.opentelemetry.context.propagation.ContextPropagators propagators) Extracts with explicit propagators.- Parameters:
carrier- mappropagators- propagators- Returns:
- context
-
injectHeaders
Injects the current context into a multi-valued header map (as used byjava.net.http.HttpHeadersor servlet APIs); existing propagation headers are replaced.- Parameters:
headers- mutable header map
-
injectHeaders
public static void injectHeaders(io.opentelemetry.context.Context context, Map<String, List<String>> headers, io.opentelemetry.context.propagation.ContextPropagators propagators) Injects a context into a multi-valued header map with explicit propagators.- Parameters:
context- contextheaders- mutable header mappropagators- propagators
-
extractHeaders
Extracts a context from a multi-valued header map (names match case-insensitively).- Parameters:
headers- header map- Returns:
- context
-
extractHeaders
public static io.opentelemetry.context.Context extractHeaders(Map<String, List<String>> headers, io.opentelemetry.context.propagation.ContextPropagators propagators) Extracts from a multi-valued header map with explicit propagators.- Parameters:
headers- header mappropagators- propagators- Returns:
- context
-
headersFor
public static Map<String,String> headersFor(io.opentelemetry.context.propagation.ContextPropagators propagators) The headers a request should carry for the current context.- Parameters:
propagators- propagators- Returns:
- header name to value
-
traceFields
Log correlation fields of the current span (trace_id,span_id; snake_case on purpose, the log field convention).- Returns:
- fields (empty when no valid span is active)
-
fields
public static List<String> fields(io.opentelemetry.context.propagation.ContextPropagators propagators) Names of the propagation fields of the given propagators.- Parameters:
propagators- propagators- Returns:
- header names
-