Package io.micrometer.context
Interface ContextSnapshot
public interface ContextSnapshot
Holds values extracted from
ThreadLocal
and other types of context and exposes
methods to propagate those values.
Use static factory methods on this interface to create a snapshot.
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
An object to use to resetThreadLocal
values at the end of a context scope. -
Method Summary
Modifier and TypeMethodDescriptionstatic ContextSnapshot
captureAll
(ContextRegistry registry, Object... contexts) Capture values fromThreadLocal
and from other context objects using all accessors from theglobal
ContextRegistry instance.static ContextSnapshot
captureAll
(Object... contexts) Capture values fromThreadLocal
and from other context objects using all accessors from theglobal
ContextRegistry instance.static ContextSnapshot
captureAllUsing
(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts) Variant ofcaptureAll(Object...)
with a predicate to filter context keys and with a specificContextRegistry
instance.static ContextSnapshot
captureFrom
(Object context) Create aContextSnapshot
by reading values from the given context object.static ContextSnapshot
captureFrom
(Object context, ContextRegistry registry) Create aContextSnapshot
by reading values from the given context object.static ContextSnapshot
captureFrom
(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) Create aContextSnapshot
by reading values from the given context object.static ContextSnapshot.Scope
setAllThreadLocalsFrom
(Object sourceContext) Variant ofsetThreadLocalsFrom(Object, String...)
that sets allThreadLocal
values for which there is a value in the given source context.static ContextSnapshot.Scope
setAllThreadLocalsFrom
(Object sourceContext, ContextRegistry contextRegistry) Variant ofsetThreadLocalsFrom(Object, String...)
that sets allThreadLocal
values for which there is a value in the given source context.SetThreadLocal
values from the snapshot.setThreadLocals
(Predicate<Object> keyPredicate) Variant ofsetThreadLocals()
with a predicate to select context values by key.static ContextSnapshot.Scope
setThreadLocalsFrom
(Object sourceContext, ContextRegistry contextRegistry, String... keys) Variant ofsetThreadLocalsFrom(Object, String...)
with a specificContextRegistry
instead of the global instance.static ContextSnapshot.Scope
setThreadLocalsFrom
(Object sourceContext, String... keys) Read the values specified by from the given source context, and if found, use them to setThreadLocal
values.<C> C
updateContext
(C context) Update the given context with all snapshot values.<C> C
updateContext
(C context, Predicate<Object> keyPredicate) Variant ofupdateContext(Object)
to update the given context with a subset of snapshot values.default Runnable
Return a newRunnable
that setsThreadLocal
values from the snapshot around the invocation of the givenRunnable
.default <T> Callable<T>
Return a newCallable
that setsThreadLocal
values from the snapshot around the invocation of the givenCallable
.default <T> Consumer<T>
Return a newConsumer
that setsThreadLocal
values from the snapshot around the invocation of the givenConsumer
.default Executor
wrapExecutor
(Executor executor) Return a newExecutor
that setsThreadLocal
values from the snapshot around the invocation of any executed,Runnable
.
-
Method Details
-
updateContext
<C> C updateContext(C context) Update the given context with all snapshot values.- Type Parameters:
C
- the type of the target context- Parameters:
context
- the context to write to- Returns:
- a context, possibly a new instance, with the written values
-
updateContext
Variant ofupdateContext(Object)
to update the given context with a subset of snapshot values.- Type Parameters:
C
- the type of the target context- Parameters:
context
- the context to write tokeyPredicate
- predicate for context value keys- Returns:
- a context, possibly a new instance, with the written values
-
setThreadLocals
ContextSnapshot.Scope setThreadLocals()SetThreadLocal
values from the snapshot.- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-
setThreadLocals
Variant ofsetThreadLocals()
with a predicate to select context values by key.- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-
wrap
Return a newRunnable
that setsThreadLocal
values from the snapshot around the invocation of the givenRunnable
.- Parameters:
runnable
- the runnable to instrument
-
wrap
Return a newCallable
that setsThreadLocal
values from the snapshot around the invocation of the givenCallable
.- Type Parameters:
T
- the type of value produced by theCallable
- Parameters:
callable
- the callable to instrument
-
wrap
Return a newConsumer
that setsThreadLocal
values from the snapshot around the invocation of the givenConsumer
.- Type Parameters:
T
- the type of value produced by theCallable
- Parameters:
consumer
- the callable to instrument
-
wrapExecutor
Return a newExecutor
that setsThreadLocal
values from the snapshot around the invocation of any executed,Runnable
.- Parameters:
executor
- the executor to instrument- See Also:
-
captureAll
Capture values fromThreadLocal
and from other context objects using all accessors from theglobal
ContextRegistry instance.- Parameters:
contexts
- one more context objects to extract values from- Returns:
- a snapshot with saved context values
-
captureAll
Capture values fromThreadLocal
and from other context objects using all accessors from theglobal
ContextRegistry instance.- Parameters:
registry
- the registry to usecontexts
- one more context objects to extract values from- Returns:
- a snapshot with saved context values
-
captureAllUsing
static ContextSnapshot captureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts) Variant ofcaptureAll(Object...)
with a predicate to filter context keys and with a specificContextRegistry
instance.- Parameters:
keyPredicate
- predicate for context value keysregistry
- the registry with the accessors to usecontexts
- one more context objects to extract values from- Returns:
- a snapshot with saved context values
-
captureFrom
Create aContextSnapshot
by reading values from the given context object.- Parameters:
context
- the context to read values from- Returns:
- the created
ContextSnapshot
-
captureFrom
Create aContextSnapshot
by reading values from the given context object.- Parameters:
context
- the context to read values fromregistry
- the registry to use- Returns:
- the created
ContextSnapshot
-
captureFrom
static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) Create aContextSnapshot
by reading values from the given context object.- Parameters:
context
- the context to read values from- Returns:
- the created
ContextSnapshot
-
setAllThreadLocalsFrom
Variant ofsetThreadLocalsFrom(Object, String...)
that sets allThreadLocal
values for which there is a value in the given source context.- Parameters:
sourceContext
- the source context to read values from- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-
setAllThreadLocalsFrom
static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry) Variant ofsetThreadLocalsFrom(Object, String...)
that sets allThreadLocal
values for which there is a value in the given source context.- Parameters:
sourceContext
- the source context to read values fromcontextRegistry
- the registry with the accessors to use- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-
setThreadLocalsFrom
Read the values specified by from the given source context, and if found, use them to setThreadLocal
values. Essentially, a shortcut that bypasses the need to create ofContextSnapshot
first viacaptureAll(Object...)
, followed bysetThreadLocals()
.- Parameters:
sourceContext
- the source context to read values fromkeys
- the keys of the values to read (at least one key must be passed)- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-
setThreadLocalsFrom
static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry, String... keys) Variant ofsetThreadLocalsFrom(Object, String...)
with a specificContextRegistry
instead of the global instance.- Parameters:
sourceContext
- the source context to read values fromcontextRegistry
- the registry with the accessors to usekeys
- the keys of the values to read (at least one key must be passed)- Returns:
- an object that can be used to reset
ThreadLocal
values at the end of the context scope, either removing them or restoring their previous values, if any.
-