PropagationContextElement

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")
}

Author

Brian Clozel

Sebastien Deleuze

Since

7.0

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard

Properties

Link copied to clipboard

Functions

Link copied to clipboard
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
Link copied to clipboard
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
Link copied to clipboard
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
Link copied to clipboard
open operator fun plus(context: CoroutineContext): CoroutineContext
Link copied to clipboard
open override fun restoreThreadContext(context: CoroutineContext, oldState: ContextSnapshot.Scope)
Link copied to clipboard
open override fun updateThreadContext(context: CoroutineContext): ContextSnapshot.Scope