Top Description Methods
io.opentelemetry.context

public Interface ContextStorage

Known Direct Implementers
io.opentelemetry.context.StrictContextStorage, io.opentelemetry.context.ThreadLocalContextStorage
Imports
java.util.function.Function, javax.annotation.Nullable

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 TypeMethod and Description
public static void
addWrapper(Function<? super ContextStorage, ? extends ContextStorage> wrapper)

Adds the wrapper, which will be executed with the ContextStorage is first used, i.e., by calling Context#makeCurrent().

public Scope
attach(Context toAttach)

Sets the specified Context as the current Context and returns a Scope representing the scope of execution.

public Context
current()

Returns the current Context.

public static ContextStorage
defaultStorage()

Returns the default ContextStorage which stores Context using a threadlocal.

public static ContextStorage
get()

Returns the ContextStorage being used by this application.

public default Context
root()

Returns the root Context which all other Context are derived from.

Method Detail

addWrapperback to summary
public static void addWrapper(Function<? super ContextStorage, ? extends ContextStorage> wrapper)

Adds the wrapper, which will be executed with the ContextStorage is first used, i.e., by calling Context#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.

attachback to summary
public Scope attach(Context toAttach)

Sets the specified Context as the current Context and returns a Scope representing the scope of execution. Scope#close() must be called when the current Context should be restored to what it was before attaching toAttach.

currentback to summary
public Context current()

Returns the current Context. If no Context has been attached yet, this will return null.

Annotations
@Nullable
defaultStorageback to summary
public static ContextStorage defaultStorage()

Returns the default ContextStorage which stores Context using a threadlocal.

getback to summary
public static ContextStorage get()

Returns the ContextStorage 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 a Context in an application, use Context#makeCurrent() and Scope#close().

rootback to summary
public default Context root()

Returns the root Context which all other Context are derived from.

The default implementation returns the root ArrayBasedContext, but subclasses can override this method to return a root instance of a different Context implementation.