Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe on how a logger can be customised in the Route-Chain (Java) / modify MDC #3378

Open
domdorn opened this issue Jul 20, 2020 · 2 comments
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on t:docs Issues related to the documentation

Comments

@domdorn
Copy link

domdorn commented Jul 20, 2020

I need to customise the logger that gets returned by extractLog( log -> () ) so I can attach the current correlationId / userId / other fields into the MDC. However, I'm unable to accomplish this with the documentation I found. This is what I got so far:

  public static RouteAdapter extractCorrelationId(Function<String, Route> inner) {
    return optionalHeaderValueByName("X-CorrelationId", (corr) -> extractLog(log -> {
      String correlationId = corr.orElseGet(() -> {
        String c = UUID.randomUUID().toString();
        log.debug("request does not have a upstream correlationId, assigning " + c);
        return c;
      });
      return provide(correlationId, inner);
    }));
  }

and I was planning to decorate my root-route as follows:

  public Route createRoute() {
    return extractCorrelationId(corr -> extractLog(log -> {
      LoggingAdapter newLog = log;
      var newMDC = newLog.mdc().$plus(Tuple2.apply("correlationId", corr)); // this is a MDC and not a LoggingAdapter :( 
      // how to bring the newMDC into the newLog ?  because AFAIK the MDC is a immutable list, thus I cannot simply change its values 
      return withLog(newLog, () -> allRoutes());
    }));
  }

Please provide a way on how a valid "LoggingAdapter" can be constructed from Java.

@jrudolph jrudolph added 1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on t:docs Issues related to the documentation labels Jul 30, 2020
@jrudolph
Copy link
Contributor

You will have to create a new LoggingAdapter in the first place because at least that part of LoggingAdapters is not thread-safe and use log.setMdc or log.mdc(map) to set the mdc.

The other option is to use the slf4j APIs directly but then you'll have to make to set the MDC whenever you run a chunk of code in a new thread (or use the automatic MDC propagation in commercial Lightbend Telemetry).

I keep this ticket open to create documentation around this particular pattern.

@alexklibisz
Copy link

I recently found a way to do this using the existing loggers. There are basically three directives:

  • a directive that takes the RouteContext and provides it with a new DiagnosticMarkerBusLoggingAdapter
  • a directive that extracts the DiagnosticMarkerBusLoggingAdapter from the RouteContext
  • a directive that takes a key/value pair and appends it to the MDC map on the DiagnosticMarkerBusLoggingAdapter.

As far as I can tell, this is enough to make it thread-safe: each request gets a new logger, and, AFAIK, each request's directives are executed serially, so the map updates must be serialized. In any case I also have tests that hit the routes in parallel to test the thread-safety.

We currently have this in our internal utilities. Would there be interest in making a PR with these directives? LMK what you think @jrudolph or anyone else in the akka team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on t:docs Issues related to the documentation
Projects
None yet
Development

No branches or pull requests

3 participants