[英]RestTemplate logging interceptor + Spring Cloud Sleuth
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.我创建了一个
RestTemplate
拦截器 ( ClientHttpRequestInterceptor
) 来记录请求和响应,并启用了 Spring Cloud Sleuth,目的是跟踪从拦截器收到请求到返回结果的事务。 However, Sleuth's trace and span ids do not appear in the interceptors log messages;但是,Sleuth 的 trace 和 span id 不会出现在拦截器日志消息中; 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.
我希望能够追踪到没有显式日志记录的服务,所以拦截器似乎是一个好方法,但如果在这个阶段没有办法添加跟踪和跨度 ID,那么我可能需要找到另一种方法. 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.
寻找任何建议,将跟踪和跨度添加到拦截器日志消息中,或者以其他方式记录跨服务的请求和响应,并使用跟踪和跨度 ID。 Thanks!
谢谢!
EDIT : Here's an example of the log output I'm currently getting from my RestTemplate
interceptor with Spring Cloud Sleuth enabled:编辑:这是日志 output 的示例,我目前从我的
RestTemplate
拦截器中获取 Spring Cloud Sleuth 启用:
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:我真正想得到的是使用 Sleuth
trace
和span
ID 记录的请求(或响应):
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.我想知道这是否可能(即,通过
RestTemplate
拦截器将日志记录添加到所有服务并让这些日志消息包含 Sleuth id)或者我是否应该采用不同的方法来使用 Sleuth 跟踪记录所有请求和响应/跨度ID。 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:编辑 2 :更多信息......此时我的 Sleuth 配置只不过是在
bootstrap.yml
文件中启用它:
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:我还创建/配置了一个自定义
CommonsRequestLoggingFilter
,它在拦截器之后执行(它是日志中的下一行)并且在其日志消息中确实有跟踪/跨度 ID:
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);所以 Sleuth 似乎配置正确(至少现在就我的目的而言); it just isn't adding the trace ids to the interceptor's log messages.
它只是没有将跟踪 ID 添加到拦截器的日志消息中。 Thanks!
谢谢!
Sleuth documentation says侦探文件说
You have to register RestTemplate as a bean so that the interceptors will get injected.
您必须将 RestTemplate 注册为 bean,以便注入拦截器。 If you create a RestTemplate instance with a new keyword then the instrumentation WILL NOT work.
如果您使用新关键字创建 RestTemplate 实例,则检测将不起作用。
I hope you are not using new
keyword to instantiate RestTemplate.我希望你没有使用
new
关键字来实例化 RestTemplate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.