Search code examples
kotlinkotlin-coroutinesspring-cloud-sleuth

How to pass traceId when creating new coroutine in sleuth?


How to correctly pass the traceId when creating a new coroutine context? Currently the traceId and spanId is zero when launching a new coroutine.

    suspend fun test(event: TestEvent) {
        CoroutineScope(Dispatchers.IO).launch {
            (if anything logged here, it should have the same trace id)
        }
    }

Solution

  • Passing mdccontext helped. Now all the logging has the same traceId as the context. That spawned the coroutine.

    suspend fun test(event: TestEvent) {
            CoroutineScope(Dispatchers.IO+MDCContext()).launch {
                (if anything logged here, it should have the same trace id)
            }
        }