Class PropagationContextElement
java.lang.Object
kotlin.coroutines.AbstractCoroutineContextElement
org.springframework.core.PropagationContextElement
- All Implemented Interfaces:
kotlin.coroutines.CoroutineContext, kotlin.coroutines.CoroutineContext.Element, kotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>
public final class PropagationContextElement
extends kotlin.coroutines.AbstractCoroutineContextElement
implements kotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>
ThreadContextElement that ensures that contexts registered with the
Micrometer Context Propagation library are captured and restored when
a coroutine is resumed on a thread. This is typically being used for
Micrometer Tracing support in Kotlin suspended functions.
It requires the io.micrometer:context-propagation library. If the
org.jetbrains.kotlinx:kotlinx-coroutines-reactor dependency is also
on the classpath, this element also supports Reactor Context.
PropagationContextElement can be used like this:
fun main() {
runBlocking(Dispatchers.IO + PropagationContextElement()) {
suspendingFunction()
}
}
suspend fun suspendingFunction() {
delay(1)
logger.info("Log statement with traceId")
}
- Since:
- 7.0
- Author:
- Brian Clozel, Sebastien Deleuze
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface kotlin.coroutines.CoroutineContext
kotlin.coroutines.CoroutineContext.ElementNested classes/interfaces inherited from interface kotlin.coroutines.CoroutineContext.Element
kotlin.coroutines.CoroutineContext.Element.DefaultImplsNested classes/interfaces inherited from interface kotlinx.coroutines.ThreadContextElement
kotlinx.coroutines.ThreadContextElement.DefaultImpls -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PropagationContextElement.KeyPropagationContextElementkey. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidrestoreThreadContext(kotlin.coroutines.CoroutineContext context, io.micrometer.context.ContextSnapshot.Scope oldState) io.micrometer.context.ContextSnapshot.ScopeupdateThreadContext(kotlin.coroutines.CoroutineContext context) Methods inherited from class kotlin.coroutines.AbstractCoroutineContextElement
fold, get, getKey, minusKey, plusMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface kotlin.coroutines.CoroutineContext
plusMethods inherited from interface kotlin.coroutines.CoroutineContext.Element
fold, get, getKey, minusKey
-
Field Details
-
Key
PropagationContextElementkey.
-
-
Constructor Details
-
PropagationContextElement
public PropagationContextElement()
-
-
Method Details
-
restoreThreadContext
public void restoreThreadContext(kotlin.coroutines.CoroutineContext context, io.micrometer.context.ContextSnapshot.Scope oldState) - Specified by:
restoreThreadContextin interfacekotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>
-
updateThreadContext
public io.micrometer.context.ContextSnapshot.Scope updateThreadContext(kotlin.coroutines.CoroutineContext context) - Specified by:
updateThreadContextin interfacekotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>
-