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 Classes
    Modifier and Type
    Class
    Description
    static final class 
     

    Nested classes/interfaces inherited from interface kotlin.coroutines.CoroutineContext

    kotlin.coroutines.CoroutineContext.Element

    Nested classes/interfaces inherited from interface kotlin.coroutines.CoroutineContext.Element

    kotlin.coroutines.CoroutineContext.Element.DefaultImpls

    Nested classes/interfaces inherited from interface kotlinx.coroutines.ThreadContextElement

    kotlinx.coroutines.ThreadContextElement.DefaultImpls
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    PropagationContextElement key.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    restoreThreadContext(kotlin.coroutines.CoroutineContext context, io.micrometer.context.ContextSnapshot.Scope oldState)
     
    io.micrometer.context.ContextSnapshot.Scope
    updateThreadContext(kotlin.coroutines.CoroutineContext context)
     

    Methods inherited from class kotlin.coroutines.AbstractCoroutineContextElement

    fold, get, getKey, minusKey, plus

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface kotlin.coroutines.CoroutineContext

    plus

    Methods inherited from interface kotlin.coroutines.CoroutineContext.Element

    fold, get, getKey, minusKey
  • Field Details

  • Constructor Details

    • PropagationContextElement

      public PropagationContextElement()
  • Method Details

    • restoreThreadContext

      public void restoreThreadContext(kotlin.coroutines.CoroutineContext context, io.micrometer.context.ContextSnapshot.Scope oldState)
      Specified by:
      restoreThreadContext in interface kotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>
    • updateThreadContext

      public io.micrometer.context.ContextSnapshot.Scope updateThreadContext(kotlin.coroutines.CoroutineContext context)
      Specified by:
      updateThreadContext in interface kotlinx.coroutines.ThreadContextElement<io.micrometer.context.ContextSnapshot.Scope>