another questions about MDC and coroutineContext..
can i update a ThreadContextElement from inside the coroutine ?
issue is this code seems natural
Copy code
suspend fun doStuff() = withContext(MDCContext() + exceptionHandler()) {
MDC.put("flow", "do-stuff")
<http://logger.info|logger.info> { "doing stuff" } // mdc is fine here
someSuspendFun()
<http://logger.info|logger.info> { "done doing stuff" } // mdc was reset to the state of MDCContext ie. is empty
}
ideally what i would like to be able to do is update the MDCContext or update the
val contextMap: MDCContextMap
after i modify the MDC
( i tried, it fails in different ways )
or use a mechanism where the MDC is stored before doing a suspend call, so any changes are actually picked up
TL;DR currently it leads to rather unnatural looking code and additional nesting or errors that are really hard to find
g
gildor
11/13/2020, 2:51 PM
You cannot use put, it will be rested after suspend
gildor
11/13/2020, 2:52 PM
It mentioned in doc
b
bezrukov
11/13/2020, 8:53 PM
Take the current MDCcontext, mutate and modify it, and wrap your suspend fun with one more withContext
n
Nikky
11/14/2020, 11:36 AM
exactly that extra withContext is what i was hoping to avoid, making code really a lot more annoying to write and read