Package io.opentelemetry.context
Interface ContextStorage
public interface ContextStorage
The storage for storing and retrieving the current
Context
.
If you want to implement your own storage or add some hooks when a Context
is attached
and restored, you should use ContextStorageProvider
. Here's an example that sets MDC
before Context
is attached:
> public class MyStorage implements ContextStorageProvider {
>
> @Override
> public ContextStorage get() {
> ContextStorage threadLocalStorage = ContextStorage.defaultStorage();
> return new RequestContextStorage() {
> @Override
> public Scope T attach(Context toAttach) {
> Context current = current();
> setMdc(toAttach);
> Scope scope = threadLocalStorage.attach(toAttach);
> return () -> {
> clearMdc();
> setMdc(current);
> scope.close();
> }
> }
>
> @Override
> public Context current() {
> return threadLocalStorage.current();
> }
> }
> }
> }
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addWrapper
(Function<? super ContextStorage, ? extends ContextStorage> wrapper) Adds thewrapper
, which will be executed with theContextStorage
is first used, i.e., by callingContext.makeCurrent()
.current()
Returns the currentContext
.static ContextStorage
Returns the defaultContextStorage
which storesContext
using a threadlocal.static ContextStorage
get()
Returns theContextStorage
being used by this application.default Context
root()
-
Method Details
-
get
Returns theContextStorage
being used by this application. This is only for use when integrating with other context propagation mechanisms and not meant for direct use. To attach or detach aContext
in an application, useContext.makeCurrent()
andScope.close()
. -
defaultStorage
Returns the defaultContextStorage
which storesContext
using a threadlocal. -
addWrapper
Adds thewrapper
, which will be executed with theContextStorage
is first used, i.e., by callingContext.makeCurrent()
. This must be called as early in your application as possible to have effect, often as part of a static initialization block in your main class. -
attach
Sets the specifiedContext
as the currentContext
and returns aScope
representing the scope of execution.Scope.close()
must be called when the currentContext
should be restored to what it was before attachingtoAttach
. -
current
-
root
-