Class Slf4jThreadLocalAccessor
java.lang.Object
io.micrometer.context.integration.Slf4jThreadLocalAccessor
- All Implemented Interfaces:
ThreadLocalAccessor<Map<String,
String>>
public class Slf4jThreadLocalAccessor
extends Object
implements ThreadLocalAccessor<Map<String,String>>
ThreadLocalAccessor
for copying contents of the MDC
across
Thread
boundaries. It can work with selected keys only or copy the entire
contents of the MDC
. It is recommended to use only when no other
ThreadLocalAccessor
interacts with the MDC or the selected keys, for
instance tracing libraries.- Since:
- 1.1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Key under which this accessor is registered inContextRegistry
. -
Constructor Summary
ConstructorsConstructorDescriptionSlf4jThreadLocalAccessor
(String... keys) Create an instance ofSlf4jThreadLocalAccessor
. -
Method Summary
Modifier and TypeMethodDescriptiongetValue()
Return the currentThreadLocal
value.key()
The key to associate with the ThreadLocal value when saved within aContextSnapshot
.void
setValue()
Called instead ofThreadLocalAccessor.setValue(Object)
in order to remove the currentThreadLocal
value at the start of aContextSnapshot.Scope
.void
Set theThreadLocal
at the start of aContextSnapshot.Scope
to a value obtained from aContextSnapshot
or from a different type of context (through aContextAccessor
).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micrometer.context.ThreadLocalAccessor
reset, restore, restore
-
Field Details
-
KEY
Key under which this accessor is registered inContextRegistry
.- See Also:
-
-
Constructor Details
-
Slf4jThreadLocalAccessor
Create an instance ofSlf4jThreadLocalAccessor
.- Parameters:
keys
- selected keys for which values from theMDC
should be propagated acrossThread
boundaries. If none provided, the entire contents of theMDC
are propagated.
-
-
Method Details
-
key
Description copied from interface:ThreadLocalAccessor
The key to associate with the ThreadLocal value when saved within aContextSnapshot
.- Specified by:
key
in interfaceThreadLocalAccessor<Map<String,
String>>
-
getValue
Description copied from interface:ThreadLocalAccessor
Return the currentThreadLocal
value.This method is called in two scenarios:
- When capturing a
ContextSnapshot
. Anull
value is ignored and theThreadLocalAccessor.key()
will not be present in the snapshot. - When setting
ThreadLocal
values from aContextSnapshot
or from a Context object (through aContextAccessor
) to save existing values in order toThreadLocalAccessor.restore(Object)
them at the end of the scope. Anull
value means theThreadLocal
should not be set and upon closing aContextSnapshot.Scope
, theThreadLocalAccessor.restore()
variant is called.
- Specified by:
getValue
in interfaceThreadLocalAccessor<Map<String,
String>>
- When capturing a
-
setValue
Description copied from interface:ThreadLocalAccessor
Set theThreadLocal
at the start of aContextSnapshot.Scope
to a value obtained from aContextSnapshot
or from a different type of context (through aContextAccessor
).- Specified by:
setValue
in interfaceThreadLocalAccessor<Map<String,
String>> - Parameters:
value
- the value to set, nevernull
when called from aContextSnapshot
implementation, which is not allowed to store mappings tonull
.
-
setValue
public void setValue()Description copied from interface:ThreadLocalAccessor
Called instead ofThreadLocalAccessor.setValue(Object)
in order to remove the currentThreadLocal
value at the start of aContextSnapshot.Scope
.- Specified by:
setValue
in interfaceThreadLocalAccessor<Map<String,
String>>
-