Package io.opentelemetry.context
Interface ContextStorage
-
public interface ContextStorageThe storage for storing and retrieving the currentContext.If you want to implement your own storage or add some hooks when a
Contextis attached and restored, you should useContextStorageProvider. Here's an example that sets MDC beforeContextis 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 voidaddWrapper(Function<? super ContextStorage,? extends ContextStorage> wrapper)Adds thewrapper, which will be executed with theContextStorageis first used, i.e., by callingContext.makeCurrent().Scopeattach(Context toAttach)Contextcurrent()Returns the currentContext.static ContextStoragedefaultStorage()Returns the defaultContextStoragewhich storesContextusing a threadlocal.static ContextStorageget()Returns theContextStoragebeing used by this application.
-
-
-
Method Detail
-
get
static ContextStorage get()
Returns theContextStoragebeing 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 aContextin an application, useContext.makeCurrent()andScope.close().
-
defaultStorage
static ContextStorage defaultStorage()
Returns the defaultContextStoragewhich storesContextusing a threadlocal.
-
addWrapper
static void addWrapper(Function<? super ContextStorage,? extends ContextStorage> wrapper)
Adds thewrapper, which will be executed with theContextStorageis 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 specifiedContextas the currentContextand returns aScoperepresenting the scope of execution.Scope.close()must be called when the currentContextshould be restored to what it was before attachingtoAttach.
-
-