Context propagation while logging in reactive and non-reactive Spring Boot application

127 Views Asked by At

I am working on a greenfield application with up to date versions of Spring Boot and Project Reactor.

My application uses JPA and JDBC (separate databases and datasources) to retrieve and create data, Spring MVC (for now, but it will move to Webflux at some point) to expose a REST API, and Project Reactor to build the actual backbone of the application as a pipeline of (potentially async) operations.

What I want is to be able to have as seamless a way as possible to tag pipelines with "correlation ids". I need to be able to ensure that such ids are generated when a given pipeline is created (e.g. as a result of a POST); they must appear in every log message of every step of a given pipeline, and they must also appear in the log messages of libraries used by the application (e.g. JDBC, JPA, etc...).

One further complication is that a given pipeline might fan out into several independent, asynchronous sub-flows and each of them would need to have an additional sub-correlation id (e.g. each log message in each sub-flow needs to have both the initial "wider" correlation id as well as the "narrower" sub-flow correlation id).

I am currently a bit stuck because I tried several things but I did not manage to get to where I want to.

I read the excellent series of posts about context-propagation library (here for the third and final post), and I read about micrometer. I watched the following very interesting videos:

  • Context propagation to tracing: here
  • Observability of your application: here

And checked several examples (micrometer's own examples and this from the blog post series).

What I would like, ideally, is:

  • instrument once, get everything out (e.g. tracing works and logging too, with tags coming from the current trace and span)
  • be able to add extra tags (I must use a specific format for my business-logic ids) to the spans
  • see the ids in every log message, both in the "reactive" code and in the non-reactive one (e.g. JDBC, etc...)
  • be able to create sub-spans manually, so that I can model my use-case of "one main flow -> many parallel independent sub-flows" as a "one span -> many sibling spans"

My my questions, at this stage are:

  • Am I thinking about this problem correctly? Is what I am trying to achieve possible?
  • Is Micrometer the right tool for the job? My feeling is that it should, but I could not get it to work.
  • Is there anything I am missing/misunderstanding?
0

There are 0 best solutions below