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:
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:
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?