Hi all, I posted this in <#CQLNT7B29|> since I'm d...
# coroutines
o
Hi all, I posted this in #CQLNT7B29 since I'm doing this in a query but since it also deals with coroutines I thought I'd post it here. I'm hoping to get some clarification about what I am doing wrong or if this is the expected behavior to see when using async calls and an
MDCContext.
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
Copy code
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!