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 interfaceAn object to use to resetThreadLocalvalues at the end of a context scope. -
Method Summary
Modifier and TypeMethodDescriptionstatic ContextSnapshotcaptureAll(ContextRegistry registry, Object... contexts) Capture values fromThreadLocaland from other context objects using all accessors from theglobalContextRegistry instance.static ContextSnapshotcaptureAll(Object... contexts) Capture values fromThreadLocaland from other context objects using all accessors from theglobalContextRegistry instance.static ContextSnapshotcaptureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts) Variant ofcaptureAll(Object...)with a predicate to filter context keys and with a specificContextRegistryinstance.static ContextSnapshotcaptureFrom(Object context) Create aContextSnapshotby reading values from the given context object.static ContextSnapshotcaptureFrom(Object context, ContextRegistry registry) Create aContextSnapshotby reading values from the given context object.static ContextSnapshotcaptureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) Create aContextSnapshotby reading values from the given context object.static ContextSnapshot.ScopesetAllThreadLocalsFrom(Object sourceContext) Variant ofsetThreadLocalsFrom(Object, String...)that sets allThreadLocalvalues for which there is a value in the given source context.static ContextSnapshot.ScopesetAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry) Variant ofsetThreadLocalsFrom(Object, String...)that sets allThreadLocalvalues for which there is a value in the given source context.SetThreadLocalvalues from the snapshot.setThreadLocals(Predicate<Object> keyPredicate) Variant ofsetThreadLocals()with a predicate to select context values by key.static ContextSnapshot.ScopesetThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry, String... keys) Variant ofsetThreadLocalsFrom(Object, String...)with a specificContextRegistryinstead of the global instance.static ContextSnapshot.ScopesetThreadLocalsFrom(Object sourceContext, String... keys) Read the values specified by from the given source context, and if found, use them to setThreadLocalvalues.<C> CupdateContext(C context) Update the given context with all snapshot values.<C> CupdateContext(C context, Predicate<Object> keyPredicate) Variant ofupdateContext(Object)to update the given context with a subset of snapshot values.default RunnableReturn a newRunnablethat setsThreadLocalvalues from the snapshot around the invocation of the givenRunnable.default <T> Callable<T>Return a newCallablethat setsThreadLocalvalues from the snapshot around the invocation of the givenCallable.default <T> Consumer<T>Return a newConsumerthat setsThreadLocalvalues from the snapshot around the invocation of the givenConsumer.default ExecutorwrapExecutor(Executor executor) Return a newExecutorthat setsThreadLocalvalues 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()SetThreadLocalvalues from the snapshot.- Returns:
- an object that can be used to reset
ThreadLocalvalues 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
ThreadLocalvalues at the end of the context scope, either removing them or restoring their previous values, if any.
-
wrap
Return a newRunnablethat setsThreadLocalvalues from the snapshot around the invocation of the givenRunnable.- Parameters:
runnable- the runnable to instrument
-
wrap
Return a newCallablethat setsThreadLocalvalues 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 newConsumerthat setsThreadLocalvalues 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 newExecutorthat setsThreadLocalvalues from the snapshot around the invocation of any executed,Runnable.- Parameters:
executor- the executor to instrument- See Also:
-
captureAll
Capture values fromThreadLocaland from other context objects using all accessors from theglobalContextRegistry instance.- Parameters:
contexts- one more context objects to extract values from- Returns:
- a snapshot with saved context values
-
captureAll
Capture values fromThreadLocaland from other context objects using all accessors from theglobalContextRegistry 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 specificContextRegistryinstance.- 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 aContextSnapshotby reading values from the given context object.- Parameters:
context- the context to read values from- Returns:
- the created
ContextSnapshot
-
captureFrom
Create aContextSnapshotby 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 aContextSnapshotby 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 allThreadLocalvalues 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
ThreadLocalvalues 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 allThreadLocalvalues 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
ThreadLocalvalues 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 setThreadLocalvalues. Essentially, a shortcut that bypasses the need to create ofContextSnapshotfirst 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
ThreadLocalvalues 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 specificContextRegistryinstead 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
ThreadLocalvalues at the end of the context scope, either removing them or restoring their previous values, if any.
-