micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Document how to add request data to the SLF4J MDC

Open ianfp opened this issue 2 years ago • 0 comments

Issue description

I'm trying to add per-request fields to the SLF4J MDC. For example:

{
    "requestName": "GET /chickens",
    "traceId": "some-trace-id",
    // etc
}

I've tried using an HttpServerFilter, but those run on the event loop thread, whereas my requests are handled by the I/O thread pool per this configuration:

micronaut:
  server:
    thread-selection: io

As a result, the Thread-local MDC is not propagated across to the request-handling thread. My application is not using reactive/flux stuff.

I cannot find any documentation about how to do this. I found this page: https://micronaut-projects.github.io/micronaut-tracing/4.2.1/guide/index.html but I can't see how it answers my question.

I found some seemingly-related issues, but none of them led me to a solution:

  • https://github.com/micronaut-projects/micronaut-core/issues/3106
  • https://github.com/micronaut-projects/micronaut-core/issues/2312
  • https://github.com/micronaut-projects/micronaut-core/issues/1669

There's even a StackOverflow question about this, but it does not have any answers:

  • https://stackoverflow.com/questions/68217927/add-a-request-id-to-slf4j-logging-in-micronaut The one comment is not sufficiently detailed to be helpful.

ianfp avatar Aug 23 '22 17:08 ianfp