Top Description Fields Constructors Methods
io.micrometer.observation.contextpropagation

public Class ObservationThreadLocalAccessor

extends Object
implements ThreadLocalAccessor<Observation>
Class Inheritance
All Implemented Interfaces
io.micrometer.context.ThreadLocalAccessor
Imports
io.micrometer.common.util.internal.logging.InternalLogger, .InternalLoggerFactory, io.micrometer.context.ContextRegistry, .ThreadLocalAccessor, io.micrometer.observation.NullObservation, .Observation, .Observation.Scope, .ObservationRegistry

A ThreadLocalAccessor to put and restore current Observation.
Author
Marcin Grzejszczak
Since
1.10.0

Field Summary

Modifier and TypeField and Description
private static ObservationThreadLocalAccessor
public static final String
KEY

Key under which Micrometer Observation is being registered.

private static final InternalLogger
private ObservationRegistry
observationRegistry

The default implementation of ObservationRegistry that Micrometer provides comes with a static ThreadLocal instance for accessing the Observation.

Constructor Summary

AccessConstructor and Description
public
ObservationThreadLocalAccessor()

Creates a new instance of this class and stores a static handle to it.

public
ObservationThreadLocalAccessor(ObservationRegistry
observation registry
observationRegistry
)

Creates a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
private void
public static ObservationThreadLocalAccessor

Returns:

instance
getInstance
()

Return the singleton instance of this ObservationThreadLocalAccessor.

public ObservationRegistry

Returns:

observation registry
getObservationRegistry
()

Returns the provided ObservationRegistry.

public Observation
public Object
key()

Implements io.micrometer.context.ThreadLocalAccessor.key.

The key to associate with the ThreadLocal value when saved within a ContextSnapshot.
public void
reset()

Overrides default io.micrometer.context.ThreadLocalAccessor.reset.

Deprecated
Remove the ThreadLocal value when setting ThreadLocal values in case of missing mapping for a key() from a ContextSnapshot, or a Context object (operated upon by ContextAccessor).
public void
restore()

Overrides default io.micrometer.context.ThreadLocalAccessor.restore.

Called instead of restore(Object) when there was no ThreadLocal value existing at the start of the scope.
public void
restore(Observation
previous value to set, never null when called from a ContextSnapshot * implementation, which is not allowed to store mappings to null.
value
)

Overrides default io.micrometer.context.ThreadLocalAccessor.restore.

Restore the ThreadLocal at the end of a io.
public void
setObservationRegistry(ObservationRegistry
observation registry
observationRegistry
)

Provide an ObservationRegistry to be used by ObservationThreadLocalAccessor.

public void
setValue(Observation
the value to set, never null when called from a ContextSnapshot implementation, which is not allowed to store mappings to null.
value
)

Implements io.micrometer.context.ThreadLocalAccessor.setValue.

Set the ThreadLocal at the start of a io.
public void
setValue()

Overrides default io.micrometer.context.ThreadLocalAccessor.setValue.

Called instead of setValue(Object) in order to remove the current ThreadLocal value at the start of a io.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

instanceback to summary
private static ObservationThreadLocalAccessor instance
KEYback to summary
public static final String KEY

Key under which Micrometer Observation is being registered.

logback to summary
private static final InternalLogger log
observationRegistryback to summary
private ObservationRegistry observationRegistry

The default implementation of ObservationRegistry that Micrometer provides comes with a static ThreadLocal instance for accessing the Observation.Scope. That's why we can create an instance of the default implementation of the registry just to call its ObservationRegistry#getCurrentObservation() because that in turn will look at the static ThreadLocal and will always return the proper scope.

Constructor Detail

ObservationThreadLocalAccessorback to summary
public ObservationThreadLocalAccessor()

Creates a new instance of this class and stores a static handle to it. Remember to call ContextRegistry#getInstance() to load all accessors which will call this constructor.

ObservationThreadLocalAccessorback to summary
public ObservationThreadLocalAccessor(ObservationRegistry observationRegistry)

Creates a new instance of this class.

Parameters
observationRegistry:ObservationRegistry

observation registry

Since
1.10.8

Method Detail

assertFalseback to summary
pack-priv void assertFalse(String msg)
closeCurrentScopeback to summary
private void closeCurrentScope()
getInstanceback to summary
public static ObservationThreadLocalAccessor getInstance()

Return the singleton instance of this ObservationThreadLocalAccessor.

Returns:ObservationThreadLocalAccessor

instance

Since
1.10.8
getObservationRegistryback to summary
public ObservationRegistry getObservationRegistry()

Returns the provided ObservationRegistry.

Returns:ObservationRegistry

observation registry

Since
1.10.8
getValueback to summary
public Observation getValue()

Implements io.micrometer.context.ThreadLocalAccessor.getValue.

Doc from io.micrometer.context.ThreadLocalAccessor.getValue.

Return the current ThreadLocal value.

This method is called in two scenarios:

Annotations
@Override
keyback to summary
public Object key()

Implements io.micrometer.context.ThreadLocalAccessor.key.

Doc from io.micrometer.context.ThreadLocalAccessor.key.

The key to associate with the ThreadLocal value when saved within a ContextSnapshot.

Annotations
@Override
resetback to summary
public void reset()

Overrides default io.micrometer.context.ThreadLocalAccessor.reset.

Doc from io.micrometer.context.ThreadLocalAccessor.reset.

Deprecated

Remove the ThreadLocal value when setting ThreadLocal values in case of missing mapping for a key() from a ContextSnapshot, or a Context object (operated upon by ContextAccessor).

Annotations
@Override
@Deprecated
restoreback to summary
public void restore()

Overrides default io.micrometer.context.ThreadLocalAccessor.restore.

Doc from io.micrometer.context.ThreadLocalAccessor.restore.

Called instead of restore(Object) when there was no ThreadLocal value existing at the start of the scope.

Annotations
@Override
restoreback to summary
public void restore(Observation value)

Overrides default io.micrometer.context.ThreadLocalAccessor.restore.

Doc from io.micrometer.context.ThreadLocalAccessor.restore.

Restore the ThreadLocal at the end of a io.micrometer.context.ContextSnapshot.Scope to the previous value it had before the start of the scope.

Parameters
value:Observation

previous value to set, never null when called from a ContextSnapshot * implementation, which is not allowed to store mappings to null.

Annotations
@Override
setObservationRegistryback to summary
public void setObservationRegistry(ObservationRegistry observationRegistry)

Provide an ObservationRegistry to be used by ObservationThreadLocalAccessor.

Parameters
observationRegistry:ObservationRegistry

observation registry

Since
1.10.8
setValueback to summary
public void setValue(Observation value)

Implements io.micrometer.context.ThreadLocalAccessor.setValue.

Doc from io.micrometer.context.ThreadLocalAccessor.setValue.

Set the ThreadLocal at the start of a io.micrometer.context.ContextSnapshot.Scope to a value obtained from a ContextSnapshot or from a different type of context (through a ContextAccessor).

Parameters
value:Observation

the value to set, never null when called from a ContextSnapshot implementation, which is not allowed to store mappings to null.

Annotations
@Override
setValueback to summary
public void setValue()

Overrides default io.micrometer.context.ThreadLocalAccessor.setValue.

Doc from io.micrometer.context.ThreadLocalAccessor.setValue.

Called instead of setValue(Object) in order to remove the current ThreadLocal value at the start of a io.micrometer.context.ContextSnapshot.Scope.

Annotations
@Override