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 voidaddWrapper(Function<? super ContextStorage, ? extends ContextStorage> wrapper) Adds thewrapper, which will be executed with theContextStorageis first used, i.e., by callingContext.makeCurrent().current()Returns the currentContext.static ContextStorageReturns the defaultContextStoragewhich storesContextusing a threadlocal.static ContextStorageget()Returns theContextStoragebeing used by this application.default Contextroot()
-
Method Details
-
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
Returns the defaultContextStoragewhich storesContextusing a threadlocal. -
addWrapper
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
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. -
current
-
root
-