RestTemplate日志拦截器 + Spring Cloud Sleuth

huangapple go评论94阅读模式
英文:

RestTemplate logging interceptor + Spring Cloud Sleuth

问题

我已经创建了一个RestTemplate拦截器(ClientHttpRequestInterceptor),用于记录请求和响应,并启用了Spring Cloud Sleuth,目标是从拦截器接收请求到返回结果的整个过程中跟踪事务。然而,Sleuth的跟踪和跨度ID似乎不会出现在拦截器的日志消息中;它们似乎只出现在稍后处理请求的阶段。我希望能够追踪到没有显式日志记录的服务,所以拦截器似乎是一个不错的方法,但是如果在这个阶段无法添加跟踪和跨度ID,那么我可能需要找到另一种方法。寻求任何建议,可以在拦截器的日志消息中添加跟踪和跨度,或者其他记录带有跟踪和跨度ID的服务请求和响应的方法。谢谢!

编辑:以下是启用了Spring Cloud Sleuth后,我从RestTemplate拦截器中获得的日志输出示例:

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

我真正想要的是使用Sleuth的tracespan ID记录请求(或响应):

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

我在思考是否有可能(即,通过RestTemplate拦截器向所有服务添加日志记录,并且使这些日志消息包含Sleuth ID),或者是否应该采取不同的方法来记录所有带有Sleuth跟踪/跨度ID的请求和响应。我将非常感谢任何建议!

编辑 2:更多信息...目前,我的Sleuth配置仅在bootstrap.yml文件中启用它:

spring:
    cloud:
        sleuth:
            enabled: true

我还创建/配置了一个自定义的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 ...

因此,Sleuth似乎已经正确配置(至少对于我现在的目的);它只是没有将跟踪ID添加到拦截器的日志消息中。谢谢!

英文:

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!

答案1

得分: 2

侦探文档表示:
> 您必须将 RestTemplate 注册为一个 bean,以便拦截器可以被注入。如果您使用 new 关键字创建 RestTemplate 实例,则不会进行仪器化。

我希望您不要使用 new 关键字来实例化 RestTemplate。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月21日 23:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63525459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定