question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[enhancement] Extract baggage entries from HTTP headers and manage baggage entries as SLF4J MDC entries

See original GitHub issue

Scenario

Suppose we have a legacy service-x (out of our jurisdiction) which need to call a service-a (in our jurisdiction) which, in turn, will need to call other downstream services (also in our jurisdiction). Every request that service-x sends to service-a contains a custom HTTP header (e.g. X-Tenant-Id).

We would like such header to be:

  1. extracted and propagated from service-a to the downstream services;
  2. included in the log traces, together with the default distributed tracing info (e.g. trace id, span id, etc).

Current Approach

For the goal 1, I’ve implemented a custom GenericFilterBean in the service-a to extract the header and add its value to the span context as a baggage key-value pair. This is sufficient to propagate the tenant identifier to the downstream services because the span context travels together with the trace and is attached to every span.

For the goal 2, I’ve implemented a custom SpanLogger which adds to and removes from the MDC the tenant identifier (as it’s currently done for trace id, span id, etc). And I’ve also updated the logging pattern accordingly.

First things first, is this the correct approach (in the context of the 1.3.3.RELEASE version)?

Enhancement Requests

  1. Add a property to automatically extract HTTP headers and put them in the baggage, e.g.
spring.sleuth.http.baggage.fromHeaders:
  - X-Tenant-Id

With the above configuration, if the application receives the HTTP header X-Tenant-Id, it will add the same to the baggage of the span context.

  1. Add a property to automatically manage key-value pairs in the baggage as MDC key-value pairs, e.g.
spring.sleuth.log.slf4j.mdc.fromBaggage:
  - X-Tenant-Id

With the above configuration, MDC will be updated properly by adding and removing the key-value pair related to the X-Tenant-Id when necessary (as it’s currently done for trace id, span id, etc).

Sample Code

Notes

  • I’d be happy to contribute the enhancements if you feel like they are general enough and they make sense in the context of Sleuth.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
fabriziocuccicommented, Jun 7, 2018

Currently it’s basically enough to copy the class and register your own bean that does exactly what you need.

You mean copying the whole Slf4jCurrentTraceContext, adding a couple MDC.put(...) and MDC.remove(...) and registering the bean so that the default implementation doesn’t kick in?

Yes, that is totally doable but maybe the configuration option would be a bit nicer? 😅

Thanks for adding this to the backlog and, again, I’d be happy to contribute with a PR.

3reactions
marcingrzejszczakcommented, Sep 24, 2018

It’s done and will be available with 2.1.0.M1 - https://github.com/spring-cloud/spring-cloud-sleuth/issues/1071

Read more comments on GitHub >

github_iconTop Results From Across the Web

5. Propagation - Spring Cloud
If you want to add the baggage entries as tags, to make it possible to search for spans via the baggage entries, you...
Read more >
Spring Boot and Tracing Calls - The Blog of Ivan Krizsan
With Spring Cloud Sleuth this type of information can be passed as what is called baggage. Both trance id, span id and baggage...
Read more >
Passing HTTP headers to another microservice and put them ...
(1) Tell Sleuth to get the value from this header name(s) and propagate to remote services (2) Set the baggage value(s) to Slf4j's...
Read more >
Spring Cloud Sleuth - Single Application - Baeldung
First, let's create a controller class to be an entry point to work ... Let's run our application and navigate to “http://localhost:8080”.
Read more >
How to get request headers to be saved to MDC
You have your custom tracing mechanism, that's why you need to take care of all the baggage- prefixing etc. If you check out...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found