Package io.opentelemetry.context
Interface ContextStorage
-
public interface ContextStorage
The storage for storing and retrieving the currentContext
.If you want to implement your own storage or add some hooks when a
Context
is attached and restored, you should useContextStorageProvider
. Here's an example that sets MDC beforeContext
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
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static void
addWrapper(Function<? super ContextStorage,? extends ContextStorage> wrapper)
Adds thewrapper
, which will be executed with theContextStorage
is first used, i.e., by callingContext.makeCurrent()
.Scope
attach(Context toAttach)
Context
current()
Returns the currentContext
.static ContextStorage
defaultStorage()
Returns the defaultContextStorage
which storesContext
using a threadlocal.static ContextStorage
get()
Returns theContextStorage
being used by this application.
-
-
-
Method Detail
-
get
static ContextStorage 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
static ContextStorage defaultStorage()
Returns the defaultContextStorage
which storesContext
using a threadlocal.
-
addWrapper
static void addWrapper(Function<? super ContextStorage,? extends ContextStorage> wrapper)
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
Scope attach(Context toAttach)
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
.
-
-