What is the recommended way to use <MDCCOntext>? ...
# coroutines
j
What is the recommended way to use MDCCOntext? For example, in a Ktor route like:
Copy code
routing {
    get("/some-route") {
        MDC.put("foo", "bar") // Will it leak in successive requests for the same route?
        println(MDC.getCopyOfContextMap())
        delay(1)
        println(MDC.getCopyOfContextMap()) // Should I use `withContext(MDCContext()) {}` here?
    }
}
Should I do something like this?
Copy code
routing {
    get("/some-route") {
        // Use MDCContext before modifying the MDC to ensure the data is restored
        withContext(MDCContext()) {
            MDC.put("foo", "bar")
            println(MDC.getCopyOfContextMap())

            // Use MDCContext before suspending the coroutine to ensure
            // the data is still available when resuming
            withContext(MDCContext()) {
                delay(1)
                println(MDC.getCopyOfContextMap())
            }
        }
    }
}
As you can see, I’m a bit lost and I’m not sure when I need to use
MDCContext
😬