2

I've created a RestTemplate interceptor (ClientHttpRequestInterceptor) to log requests and responses and enabled Spring Cloud Sleuth with the goal of tracing transactions from when the interceptor receives a request to the point where it returns a result. However, Sleuth's trace and span ids do not appear in the interceptors log messages; it seems they are only at a later stage in processing the request. I would like to be able to trace into services that do not have explicit logging, so the interceptor seemed like a good approach, but if there is no way to add the trace and span ids at this stage then I may need to find another approach. Looking for any suggestion which would either add the trace and span to the interceptors log messages or another way to log requests and responses across services with trace and span ids. Thanks!

EDIT: Here's an example of the log output I'm currently getting from my RestTemplate interceptor with Spring Cloud Sleuth enabled:

2020-08-21 18:53:17.164 DEBUG [,,,] 14256 --- [           main] .c.o.l.RequestResponseLoggingInterceptor : ⇒ HTTP REQUEST: METHOD 'POST', URI 'http://localhost:59742/….

What I'd really like to get is the request (or repsonse) logged with the Sleuth trace and span ids:

2020-08-21 18:53:17.164 DEBUG [my-service-name,d3cb8a2fa7d1d9ac,80b023cb829bf254,false] 14256 --- [           main] .c.o.l.RequestResponseLoggingInterceptor : ⇒ HTTP REQUEST: METHOD 'POST', URI 'http://localhost:59742/….

I'm wondering whether this is possible (ie, adding logging to all service through a RestTemplate interceptor and having those log messages contain the Sleuth ids) or whether there is a different approach I should take to log all requests and responses with the Sleuth trace/span ids. I would appreciate any suggestions!

EDIT 2: A bit more information...My Sleuth config at this point is nothing more than enabling it in a bootstrap.yml file:

spring:
    cloud:
        sleuth:
            enabled: true

I also created / configured a custom CommonsRequestLoggingFilter, which executes just after the interceptor (it's the next line in the log) and does have the trace / span ids in its log message:

2020-08-21 18:53:17.164 DEBUG [my-app,d3cb8a2fa7d1d9ac,80b023cb829bf254,false] 3479 --- [c-auto-1-exec-2] c.t.c.o.l.CustomRequestLoggingFilter  : ⇒ POST ...

So Sleuth seems to be configured correctly (at least for my purposes now); it just isn't adding the trace ids to the interceptor's log messages. Thanks!

5
  • Theoretical questions are hard to answer. Share the code, your observations and your expectated output clearly to get better community support. Commented Aug 21, 2020 at 18:19
  • I've updated the question with examples of the current log output from the RestTemplate interceptor and what I'd like to see (same output-- log of the request or response-- with the Sleuth trace and span ids). Commented Aug 21, 2020 at 19:05
  • Have you added sleuth to your classpath or as dependency? Commented Aug 21, 2020 at 19:11
  • Yes. Sleuth is working, just not at the point of the interceptor-- log messages after the request is logged by the interceptor, and all of those before the response is logged all have the trace and span ids. They are just not appearing in the interceptor's log messages. Any help would be very much appreciated. Commented Aug 24, 2020 at 17:56
  • Hi @TimothyPerrigo, could you please help me to write the ClientHttpRequestInterceptor. I have similar activity. Could you please share some insights or code sample to write that piece of code Commented Aug 13, 2022 at 11:19

1 Answer 1

2

Sleuth documentation says

You have to register RestTemplate as a bean so that the interceptors will get injected. If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work.

I hope you are not using new keyword to instantiate RestTemplate.

Sign up to request clarification or add additional context in comments.

4 Comments

No, not using new; the template is registered as a bean and managed by the container.
Please share how you have setup sleuth in your project.
Additional info added in message. Thanks!
Update: I stand corrected, and I wanted to correct my responses. Upon further investigation, another library my app is using was creating a RestTemplate using new. After removing this, my interceptor's log messages contain the Sleuth trace and span ids, as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.