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 ContextSnapshotFactory.builder() to configure a factory to work with
snapshots.
Implementations are disallowed to store null values. If a ThreadLocal
is not set, or it's value is null, there is no way of distinguishing one from
the other. In such a case, the ContextSnapshot simply must not contain a
capture for the particular ThreadLocal. Implementations should filter out any
null values after reading into the storage also obtained by calling
ContextAccessor.readValues(Object, Predicate, Map), and should likewise ignore
null values from ContextAccessor.readValue(Object, Object).
- 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) Deprecated.static ContextSnapshotcaptureAll(Object... contexts) Deprecated.useContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder().static ContextSnapshotcaptureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts) Deprecated.useContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder()and configureContextSnapshotFactory.Builder.captureKeyPredicate(Predicate)andContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).static ContextSnapshotcaptureFrom(Object context) Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder().static ContextSnapshotcaptureFrom(Object context, ContextRegistry registry) Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).static ContextSnapshotcaptureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder()and configureContextSnapshotFactory.Builder.captureKeyPredicate(Predicate)andContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).static ContextSnapshot.ScopesetAllThreadLocalsFrom(Object sourceContext) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)with no keys on a factory obtained via aContextSnapshotFactory.builder().static ContextSnapshot.ScopesetAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)with no keys on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).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) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).static ContextSnapshot.ScopesetThreadLocalsFrom(Object sourceContext, String... keys) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)on a factory obtained via aContextSnapshotFactory.builder().<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.- Parameters:
keyPredicate- selects keys for use when settingThreadLocalvalues- 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- Returns:
- wrapped instance
-
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- Returns:
- wrapped instance
-
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- Returns:
- wrapped instance
-
wrapExecutor
Return a newExecutorthat setsThreadLocalvalues from the snapshot around the invocation of any executed,Runnable.- Parameters:
executor- the executor to instrument- Returns:
- wrapped instance
- See Also:
-
captureAll
Deprecated.useContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder().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
Deprecated.useContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).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
@Deprecated static ContextSnapshot captureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts) Deprecated.useContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder()and configureContextSnapshotFactory.Builder.captureKeyPredicate(Predicate)andContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).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
Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder().Create aContextSnapshotby reading values from the given context object.- Parameters:
context- the context to read values from- Returns:
- the created
ContextSnapshot
-
captureFrom
Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).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
@Deprecated static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) Deprecated.useContextSnapshotFactory.captureFrom(Object...)on a factory obtained via aContextSnapshotFactory.builder()and configureContextSnapshotFactory.Builder.captureKeyPredicate(Predicate)andContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).Create aContextSnapshotby reading values from the given context object.- Parameters:
context- the context to read values fromkeyPredicate- predicate for context value keysregistry- the registry to use- Returns:
- the created
ContextSnapshot
-
setAllThreadLocalsFrom
Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)with no keys on a factory obtained via aContextSnapshotFactory.builder().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
@Deprecated static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)with no keys on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.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
Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)on a factory obtained via aContextSnapshotFactory.builder().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
@Deprecated static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry, String... keys) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).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.
-
ContextSnapshotFactory.captureAll(Object...)on a factory obtained via aContextSnapshotFactory.builder()combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry).