Interface Span
- All Known Implementing Classes:
NoOpSpanFactory.NoOpSpan,OpenTelemetrySpan
The Span is an abstraction for Axon Framework to have tracing capabilities without knowing the specific
tracing provider. Calling start() will start the span and make it active to the current thread. For
every start invocation, a respective end() should be called as well to prevent scope leaks.
Creating spans is the responsibility of the SpanFactory which should be implemented by the
tracing provider of choice.
Important! In order to make this span the parent for any new span created during its execution,
makeCurrent() should be called. This method will return a SpanScope, on which
SpanScope.close() should be invoked during the same code execution on the same thread. If not, this span will
become the unwanted parent of any children. You can make the same span the current for multiple threads at any point
in time, as long as you close them before calling end()
Each start() should eventually result in an end() being called, but this does not have to be done
on the same thread.
- Since:
- 4.6.0
- Author:
- Mitchell Herrijgers
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault SpanaddAttribute(String key, String value) Adds an attribute to the span.voidend()Ends the span.default SpanScopeSets the Span as the current for the current thread.Records an exception to the span.default voidRuns a piece of code which will be traced.default <T> TrunCallable(Callable<T> callable) Runs a piece of code which will be traced.default <T> voidrunConsumer(Consumer<T> supplier, T consumedObject) Runs a piece of code that returns a value and which will be traced.default <T> TrunSupplier(Supplier<T> supplier) Runs a piece of code that returns a value and which will be traced.default <T> CompletableFuture<T> runSupplierAsync(Supplier<CompletableFuture<T>> supplier) start()Starts the Span.default <T> Callable<T> wrapCallable(Callable<T> callable) default <T> Consumer<T> wrapConsumer(Consumer<T> supplier) Wraps aConsumer, tracing the invocation.default RunnablewrapRunnable(Runnable runnable) default <T> Supplier<T> wrapSupplier(Supplier<T> supplier) Wraps aSupplier, tracing the invocation.
-
Method Details
-
start
Span start()Starts the Span. However, does not set this span as the span of the current thread. SeemakeCurrent()in order to do so.- Returns:
- The span for fluent interfacing.
-
makeCurrent
Sets the Span as the current for the current thread. The returnedSpanScopemust be closed before ending the Span, on the same thread, or through a try-with-resources statement in the same thread as this method was called.You can make a span current on as many threads as you like, but you have to close every
SpanScope, or context will leak into the current thread. Note that if this is neglected, theend()method should warn the user in order to report this back to the framework.- Returns:
- The scope of the span that must be closed be
-
end
void end()Ends the span. All scopes should have been closed at this point. In addition, a span can only be ended once.If scopes are still open when this method is called, either an exception should be thrown or an error log should be produced to warn the user of the leak. This information can then be reported back to the developers of the framework for a fix.
-
recordException
Records an exception to the span. This will be reported to the APM tooling, which can show more information about the error in the trace. This method does not end the span.- Parameters:
t- The exception to record- Returns:
- The span for fluent interfacing.
-
run
Runs a piece of code which will be traced. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution. Note that theRunnablewill be invoked instantly and synchronously.- Parameters:
runnable- TheRunnableto execute.
-
wrapRunnable
Wraps aRunnable, propagating the current span context to the actual thread that runs theRunnable. If you don't wrap a runnable before passing it to anExecutorthe context will be lost and a new trace will be started.- Parameters:
runnable- TheRunnableto wrap- Returns:
- A wrapped runnable which propagates the span's context across threads.
-
runCallable
Runs a piece of code which will be traced. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution. Note that theCallablewill be invoked instantly and synchronously. -
wrapCallable
Wraps aCallable, propagating the current span context to the actual thread that runs theCallable. If you don't wrap a callable before passing it to anExecutorthe context will be lost and a new trace will be started.- Parameters:
callable- TheCallableto wrap- Returns:
- A wrapped callable which propagates the span's context across threads.
-
runSupplier
Runs a piece of code that returns a value and which will be traced. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution. Note that theSupplierwill be invoked instantly and synchronously.- Parameters:
supplier- TheSupplierto execute.
-
runSupplierAsync
-
wrapSupplier
Wraps aSupplier, tracing the invocation. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution.- Parameters:
supplier- TheSupplierto wrap- Returns:
- A wrapped Supplier
-
runConsumer
Runs a piece of code that returns a value and which will be traced. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution. Note that theConsumerwill be invoked instantly and synchronously.- Parameters:
supplier- TheConsumerto execute.
-
wrapConsumer
Wraps aConsumer, tracing the invocation. Exceptions will be caught automatically and added to the span, then rethrown. The span will be started before the execution, and ended after execution.- Parameters:
supplier- TheConsumerto wrap- Returns:
- A wrapped Consumer
-
addAttribute
Adds an attribute to the span. This can be used to add extra information to the span, which can be used by the APM tooling to provide more information about the span.- Parameters:
key- The key of the attribute.value- The value of the attribute.- Returns:
- The span for fluent interfacing.
-