Top Description Inners Methods
io.micrometer.context

public Interface ContextSnapshot

Known Direct Implementers
io.micrometer.context.DefaultContextSnapshot
Imports
java.util.Map, java.util.concurrent.Callable, .Executor, java.util.function.Consumer, .Predicate

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).

Authors
Rossen Stoyanchev, Brian Clozel, Dariusz Jędrzejczyk
Since
1.0.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ContextSnapshot.Scope

An object to use to reset ThreadLocal values at the end of a context scope.

Method Summary

Modifier and TypeMethod and Description
public static ContextSnapshot

Returns:

a snapshot with saved context values
captureAll
(Object...
one more context objects to extract values from
contexts
)

Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.
public static ContextSnapshot

Returns:

a snapshot with saved context values
captureAll
(ContextRegistry
the registry to use
registry
,
Object...
one more context objects to extract values from
contexts
)

Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.
public static ContextSnapshot

Returns:

a snapshot with saved context values
captureAllUsing
(Predicate<Object>
predicate for context value keys
keyPredicate
,
ContextRegistry
the registry with the accessors to use
registry
,
Object...
one more context objects to extract values from
contexts
)

Variant of captureAll(Object.
public static ContextSnapshot

Returns:

the created ContextSnapshot
captureFrom
(Object
the context to read values from
context
)

Create a ContextSnapshot by reading values from the given context object.
public static ContextSnapshot

Returns:

the created ContextSnapshot
captureFrom
(Object
the context to read values from
context
,
ContextRegistry
the registry to use
registry
)

Create a ContextSnapshot by reading values from the given context object.
public static ContextSnapshot

Returns:

the created ContextSnapshot
captureFrom
(Object
the context to read values from
context
,
Predicate<Object>
predicate for context value keys
keyPredicate
,
ContextRegistry
the registry to use
registry
)

Create a ContextSnapshot by reading values from the given context object.
public static ContextSnapshot.Scope

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
(Object
the source context to read values from
sourceContext
)

Variant of setThreadLocalsFrom(Object, String.
public static ContextSnapshot.Scope

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
(Object
the source context to read values from
sourceContext
,
ContextRegistry
the registry with the accessors to use
contextRegistry
)

Variant of setThreadLocalsFrom(Object, String.
public ContextSnapshot.Scope

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
()

Set ThreadLocal values from the snapshot.

public ContextSnapshot.Scope

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
(Predicate<Object>
selects keys for use when setting ThreadLocal values
keyPredicate
)

Variant of setThreadLocals() with a predicate to select context values by key.

public static ContextSnapshot.Scope

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
(Object
the source context to read values from
sourceContext
,
String...
the keys of the values to read (at least one key must be passed)
keys
)

Read the values specified by from the given source context, and if found, use them to set ThreadLocal values.
public static ContextSnapshot.Scope

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
(Object
the source context to read values from
sourceContext
,
ContextRegistry
the registry with the accessors to use
contextRegistry
,
String...
the keys of the values to read (at least one key must be passed)
keys
)

Variant of setThreadLocalsFrom(Object, String.
public <
the type of the target context
C
>
C

Returns:

a context, possibly a new instance, with the written values
updateContext
(C
the context to write to
context
)

Update the given context with all snapshot values.

public <
the type of the target context
C
>
C

Returns:

a context, possibly a new instance, with the written values
updateContext
(C
the context to write to
context
,
Predicate<Object>
predicate for context value keys
keyPredicate
)

Variant of updateContext(Object) to update the given context with a subset of snapshot values.

public default Runnable

Returns:

wrapped instance
wrap
(Runnable
the runnable to instrument
runnable
)

Return a new Runnable that sets ThreadLocal values from the snapshot around the invocation of the given Runnable.

public default <
the type of value produced by the Callable
T
>
Callable<T>

Returns:

wrapped instance
wrap
(Callable<T>
the callable to instrument
callable
)

Return a new Callable that sets ThreadLocal values from the snapshot around the invocation of the given Callable.

public default <
the type of value produced by the Callable
T
>
Consumer<T>

Returns:

wrapped instance
wrap
(Consumer<T>
the callable to instrument
consumer
)

Return a new Consumer that sets ThreadLocal values from the snapshot around the invocation of the given Consumer.

public default Executor

Returns:

wrapped instance
wrapExecutor
(Executor
the executor to instrument
executor
)

Return a new Executor that sets ThreadLocal values from the snapshot around the invocation of any executed, Runnable.

Method Detail

captureAllback to summary
public static ContextSnapshot captureAll(Object... contexts)

Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.

Parameters
contexts:Object[]

one more context objects to extract values from

Returns:ContextSnapshot

a snapshot with saved context values

Annotations
@Deprecated
captureAllback to summary
public static ContextSnapshot captureAll(ContextRegistry registry, Object... contexts)

Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.

Parameters
registry:ContextRegistry

the registry to use

contexts:Object[]

one more context objects to extract values from

Returns:ContextSnapshot

a snapshot with saved context values

Annotations
@Deprecated
captureAllUsingback to summary
public static ContextSnapshot captureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts)

Variant of captureAll(Object...) with a predicate to filter context keys and with a specific ContextRegistry instance.

Parameters
keyPredicate:Predicate<Object>

predicate for context value keys

registry:ContextRegistry

the registry with the accessors to use

contexts:Object[]

one more context objects to extract values from

Returns:ContextSnapshot

a snapshot with saved context values

Annotations
@Deprecated
captureFromback to summary
public static ContextSnapshot captureFrom(Object context)

Create a ContextSnapshot by reading values from the given context object.

Parameters
context:Object

the context to read values from

Returns:ContextSnapshot

the created ContextSnapshot

Annotations
@Deprecated
captureFromback to summary
public static ContextSnapshot captureFrom(Object context, ContextRegistry registry)

Create a ContextSnapshot by reading values from the given context object.

Parameters
context:Object

the context to read values from

registry:ContextRegistry

the registry to use

Returns:ContextSnapshot

the created ContextSnapshot

Annotations
@Deprecated
captureFromback to summary
public static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry)

Create a ContextSnapshot by reading values from the given context object.

Parameters
context:Object

the context to read values from

keyPredicate:Predicate<Object>

predicate for context value keys

registry:ContextRegistry

the registry to use

Returns:ContextSnapshot

the created ContextSnapshot

Annotations
@Deprecated
setAllThreadLocalsFromback to summary
public static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext)

Variant of setThreadLocalsFrom(Object, String...) that sets all ThreadLocal values for which there is a value in the given source context.

Parameters
sourceContext:Object

the source context to read values from

Returns:ContextSnapshot.Scope

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.

Annotations
@Deprecated
setAllThreadLocalsFromback to summary
public static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry)

Variant of setThreadLocalsFrom(Object, String...) that sets all ThreadLocal values for which there is a value in the given source context.

Parameters
sourceContext:Object

the source context to read values from

contextRegistry:ContextRegistry

the registry with the accessors to use

Returns:ContextSnapshot.Scope

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.

Annotations
@Deprecated
setThreadLocalsback to summary
public ContextSnapshot.Scope setThreadLocals()

Set ThreadLocal values from the snapshot.

Returns:ContextSnapshot.Scope

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.

setThreadLocalsback to summary
public ContextSnapshot.Scope setThreadLocals(Predicate<Object> keyPredicate)

Variant of setThreadLocals() with a predicate to select context values by key.

Parameters
keyPredicate:Predicate<Object>

selects keys for use when setting ThreadLocal values

Returns:ContextSnapshot.Scope

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.

setThreadLocalsFromback to summary
public static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, String... keys)

Read the values specified by from the given source context, and if found, use them to set ThreadLocal values. Essentially, a shortcut that bypasses the need to create of ContextSnapshot first via captureAll(Object...), followed by setThreadLocals().

Parameters
sourceContext:Object

the source context to read values from

keys:String[]

the keys of the values to read (at least one key must be passed)

Returns:ContextSnapshot.Scope

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.

Annotations
@Deprecated
setThreadLocalsFromback to summary
public static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry, String... keys)

Variant of setThreadLocalsFrom(Object, String...) with a specific ContextRegistry instead of the global instance.

Parameters
sourceContext:Object

the source context to read values from

contextRegistry:ContextRegistry

the registry with the accessors to use

keys:String[]

the keys of the values to read (at least one key must be passed)

Returns:ContextSnapshot.Scope

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.

Annotations
@Deprecated
updateContextback to summary
public <C> C updateContext(C context)

Update the given context with all snapshot values.

Parameters
<C>
the type of the target context
context:C

the context to write to

Returns:C

a context, possibly a new instance, with the written values

updateContextback to summary
public <C> C updateContext(C context, Predicate<Object> keyPredicate)

Variant of updateContext(Object) to update the given context with a subset of snapshot values.

Parameters
<C>
the type of the target context
context:C

the context to write to

keyPredicate:Predicate<Object>

predicate for context value keys

Returns:C

a context, possibly a new instance, with the written values

wrapback to summary
public default Runnable wrap(Runnable runnable)

Return a new Runnable that sets ThreadLocal values from the snapshot around the invocation of the given Runnable.

Parameters
runnable:Runnable

the runnable to instrument

Returns:Runnable

wrapped instance

wrapback to summary
public default <T> Callable<T> wrap(Callable<T> callable)

Return a new Callable that sets ThreadLocal values from the snapshot around the invocation of the given Callable.

Parameters
<T>
the type of value produced by the Callable
callable:Callable<T>

the callable to instrument

Returns:Callable<T>

wrapped instance

wrapback to summary
public default <T> Consumer<T> wrap(Consumer<T> consumer)

Return a new Consumer that sets ThreadLocal values from the snapshot around the invocation of the given Consumer.

Parameters
<T>
the type of value produced by the Callable
consumer:Consumer<T>

the callable to instrument

Returns:Consumer<T>

wrapped instance

wrapExecutorback to summary
public default Executor wrapExecutor(Executor executor)

Return a new Executor that sets ThreadLocal values from the snapshot around the invocation of any executed, Runnable.

Parameters
executor:Executor

the executor to instrument

Returns:Executor

wrapped instance

See Also
ContextExecutorService, ContextScheduledExecutorService
io.micrometer.context back to summary

public Interface ContextSnapshot.Scope

extends AutoCloseable
Known Direct Implementers
io.micrometer.context.DefaultContextSnapshot.DefaultScope

An object to use to reset ThreadLocal values at the end of a context scope.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares java.lang.AutoCloseable.close.

Reset ThreadLocal values, either removing them or restoring their previous values, if any.

Method Detail

closeback to summary
public void close()

Redeclares java.lang.AutoCloseable.close.

Reset ThreadLocal values, either removing them or restoring their previous values, if any.

Annotations
@Override