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 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) Deprecated.static ContextSnapshot
captureAll
(Object... contexts) Deprecated.useContextSnapshotFactory.captureAll(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
.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)
.static ContextSnapshot
captureFrom
(Object context) Deprecated.useContextSnapshotFactory.captureFrom(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
.static ContextSnapshot
captureFrom
(Object context, ContextRegistry registry) Deprecated.useContextSnapshotFactory.captureFrom(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry)
.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)
.static ContextSnapshot.Scope
setAllThreadLocalsFrom
(Object sourceContext) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)
with no keys on a factory obtained via aContextSnapshotFactory.builder()
.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)
.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) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)
on a factory obtained via aContextSnapshotFactory.builder()
combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry)
.static ContextSnapshot.Scope
setThreadLocalsFrom
(Object sourceContext, String... keys) Deprecated.useContextSnapshotFactory.setThreadLocalsFrom(Object, String...)
on a factory obtained via aContextSnapshotFactory.builder()
.<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.- Parameters:
keyPredicate
- selects keys for use when settingThreadLocal
values- 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- Returns:
- wrapped instance
-
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- Returns:
- wrapped instance
-
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- Returns:
- wrapped instance
-
wrapExecutor
Return a newExecutor
that setsThreadLocal
values 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 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
Deprecated.useContextSnapshotFactory.captureAll(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry)
.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
@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 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
Deprecated.useContextSnapshotFactory.captureFrom(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
.Create aContextSnapshot
by 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 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
@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 aContextSnapshot
by 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 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
@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 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
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 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
@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 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.
-
ContextSnapshotFactory.captureAll(Object...)
on a factory obtained via aContextSnapshotFactory.builder()
combined withContextSnapshotFactory.Builder.contextRegistry(ContextRegistry)
.