Owen Hope
09/09/2024, 1:16 PMMDCContext.
The coroutine world is still very new to me
I have a CustomGraphQLContextFactory
class that I override generateContext
and am setting CoroutineContext::class to MDCContext()
then creating the GraphQLContext.
I have this test query I am using
suspend fun testQuerySuspendWithContextWrapPartLogic(
requestNumber: Int,
@GraphQLIgnore environment: DataFetchingEnvironment,
): Boolean {
MDC.put("MDC put outside of withContext", "request$requestNumber")
<http://logger.info|logger.info>("testQuerySuspendWithContextWrapPartLogic: 1st Log out of withContext")
withContext(<http://Dispatchers.IO|Dispatchers.IO> + MDCContext()) {
MDC.put("MDC put inside 1st withContext", "request$requestNumber")
<http://logger.info|logger.info>("testQuerySuspendWithContextWrapPartLogic: 1st Log in 1st withContext")
delay(10000) // Fake an API call
<http://logger.info|logger.info>("testQuerySuspendWithContextWrapPartLogic: 2nd Log in 1st withContext")
}
<http://logger.info|logger.info>("testQuerySuspendWithContextWrapPartLogic: 2nd Log out of withContext Expect to see mdc value of 'MDC put outside of withContext'")
I would have expected the MDC value that was put onto the MDC first before the withContext
to still exist when the log right outside of the withContext ran.
Do I need to restore the MDC after each withContext
call so any time I want to change the dispatcher to do an external API call I'll need to do val contextMap = MDC.getCopyOfContextMap()
then MDC.setContextMap(contextMap)
Thank you!