A Context object can be set to the ContextStorage, which
effectively forms a scope for the context. The scope is bound to the current thread.
Within a scope, its Context is accessible even across API boundaries, through current.
The scope is later exited by Scope#close() closing} the scope.
Context objects are immutable and inherit state from their parent. To add or overwrite the current state a new context object must be created and then attached, replacing the previously bound context. For example:
Context withCredential = Context.current().with(CRED_KEY, cred);
withCredential.wrap(new Runnable() {
public void run() {
readUserRecords(userId, CRED_KEY.get());
}
}).run();
Notes and cautions on use:
makeCurrent() must be followed by a Scope#close(). Breaking these
rules may lead to memory leaks and incorrect scoping.
Context propagation is not trivial, and when done incorrectly can lead to broken traces or
even mixed traces. We provide a debug mechanism for context propagation, which can be enabled by
setting -Dio.opentelemetry.context.enableStrictContext=true in your JVM args. This will
enable a strict checker that makes sure that Scopes are closed on the correct thread and
that they are not garbage collected before being closed. This is done with some relatively
expensive stack trace walking. It is highly recommended to enable this in unit tests and staging
environments, and you may consider enabling it in production if you have the CPU budget or have
very strict requirements on context being propagated correctly (i.e., because you use context in
a multi-tenant system). For kotlin coroutine users, this will also detect invalid usage of makeCurrent() from coroutines and suspending functions. This detection relies on internal APIs
of kotlin coroutines and may not function across all versions - let us know if you find a version
of kotlin coroutines where this mechanism does not function.
StrictContextStorage
| Modifier and Type | Method and Description |
|---|---|
| public static Context | |
| public <V> V | get(ContextKey<V> key)
Returns the value stored in this |
| public default Scope | makeCurrent()
Makes this the current context and returns a |
| public static Context | |
| public static Executor | taskWrapping(Executor executor)
Returns an |
| public static ExecutorService | taskWrapping(ExecutorService executorService)
Returns an |
| public static ScheduledExecutorService | taskWrapping(ScheduledExecutorService executorService)
Returns an |
| public <V> Context | |
| public default Context | |
| public default Runnable | wrap(Runnable runnable)
Returns a |
| public default <T> Callable | wrap(Callable<T> callable)
Returns a |
| public default Executor | wrap(Executor executor)
Returns an |
| public default ExecutorService | wrap(ExecutorService executor)
Returns an |
| public default ScheduledExecutorService | wrap(ScheduledExecutorService executor)
Returns an |
| public default <T> Consumer | wrapConsumer(Consumer<T> consumer)
Returns a |
| public default <T, U> BiConsumer | wrapConsumer(BiConsumer<T, U> consumer)
Returns a |
| public default <T, U> Function | wrapFunction(Function<T, U> function)
Returns a |
| public default <T, U, V> BiFunction | wrapFunction(BiFunction<T, U, V> function)
Returns a |
| public default <T> Supplier | wrapSupplier(Supplier<T> supplier)
Returns a |
| current | back to summary |
|---|---|
| public static Context current() Return the context associated with the current | |
| get | back to summary |
|---|---|
| public <V> V get(ContextKey<V> key) Returns the value stored in this
| |
| makeCurrent | back to summary |
|---|---|
| public default Scope makeCurrent() Makes this the current context and returns a The default implementation of this method will store the
| |
| root | back to summary |
|---|---|
| public static Context root() Returns the root It should generally not be required to use the root | |
| taskWrapping | back to summary |
|---|---|
| public static Executor taskWrapping(Executor executor) Returns an This is generally used to create an
| |
| taskWrapping | back to summary |
|---|---|
| public static ExecutorService taskWrapping(ExecutorService executorService) Returns an This is generally used to create an
| |
| taskWrapping | back to summary |
|---|---|
| public static ScheduledExecutorService taskWrapping(ScheduledExecutorService executorService) Returns an This is generally used to create an Note The context will not be propagated for
| |
| with | back to summary |
|---|---|
| public <V> Context with(ContextKey<V> k1, V v1) Returns a new context with the given key value set.
Note that multiple calls to
Nonetheless, | |
| with | back to summary |
|---|---|
| public default Context with(ImplicitContextKeyed value) Returns a new | |
| wrap | back to summary |
|---|---|
| public default Runnable wrap(Runnable runnable) Returns a | |
| wrap | back to summary |
|---|---|
| public default <T> Callable Returns a | |
| wrap | back to summary |
|---|---|
| public default Executor wrap(Executor executor) Returns an | |
| wrap | back to summary |
|---|---|
| public default ExecutorService wrap(ExecutorService executor) Returns an | |
| wrap | back to summary |
|---|---|
| public default ScheduledExecutorService wrap(ScheduledExecutorService executor) Returns an | |
| wrapConsumer | back to summary |
|---|---|
| public default <T> Consumer Returns a | |
| wrapConsumer | back to summary |
|---|---|
| public default <T, U> BiConsumer Returns a | |
| wrapFunction | back to summary |
|---|---|
| public default <T, U> Function Returns a | |
| wrapFunction | back to summary |
|---|---|
| public default <T, U, V> BiFunction Returns a | |
| wrapSupplier | back to summary |
|---|---|
| public default <T> Supplier Returns a | |