Hello Everyone, I have a question regarding MDC an...
# server
s
Hello Everyone, I have a question regarding MDC and Ktor. We are currently using the
CallLogging
plugin to inject MDC values in the pipeline like so:
Copy code
fun Application.module() {
  install(CallLogging) {
		mdc("clientIpAddress") { call ->
      call.request.header("X-Forwarded-For") ?: "UNDEFINED"
    }
  }
}
We are generating a
userId
within the route which needs to be added to MDC such that it is available outside the current co-routine context:
Copy code
fun Application.myRoutes() {
  routing {
    get("/testMDC") {
	 	MDC.putClosable("userId", "${UUID.randomUUID()}") // <-- This is thread local and does not carry onto other co-routines
    }
  }
}
The
clientIpAddress
works fine and is logged throughout but
userId
only logs in local context. Is there any work around for this?
s
Not sure if it will solve this exact problem, but it might provide a workable alternative
s
Is
MDC
from Slf4J?
Correct. Will check it out and revert. Thanks!
p
We've had to use, e.g. Dispatcher.IO + MDCContext() from that slf4j coroutine lib in every new coroutine context to ensure MDC flows through all coroutines.
249 Views