Google Cloud Trace 不会使用 Sleuth 关联来自 Spring Boot 应用程序的日志

问题描述

我最近将我的应用程序升级到 Spring Boot 2.4.2、Cloud 2020.0.0,按照迁移指南更改了 Spring Cloud GCP 依赖项:https://googlecloudplatform.github.io/spring-cloud-gcp/2.0.0/reference/html/index.html#migration-guide-from-spring-cloud-gcp-1-x-to-2-x

关于应用程序,一切似乎都运行良好。我可以看到传播 trace-id 标头的微服务之间的调用

网关:

2021-01-24 20:18:36.471 DEBUG [gateway,0bc6b9664e6604e2,eb9f834718fe33c9] 1 ---

服务 1:

2021-01-24 20:18:36.700 DEBUG [service1,570653ac93add270,true]

在 Google Cloud Trace 控制台中,我可以看到跟踪 ID (0bc6b9664e6604e2) 被捕获(以 16 个额外的 0 为前缀)并且它显示了两个微服务(第一篇文章对应网关,第三个调用对应到服务 1:

enter image description here

但是请注意消息“找不到此跟踪的日志”。

还有跟踪日志查看链接投诉:

enter image description here

如果我打开链接,它只会通过时间戳查看,而不使用相关跟踪 ID。

有趣的是,如果我直接在 GCP Logging 视图中查找日志语句,trace id 就在那里:

enter image description here

然后我可以运行 GCP 日志查询以正确查找所有日志:

enter image description here

显然一切正常。您能说出为什么 GCP Trace 无法与 GCP Logging 相关联吗?

解决方法

要使日志与跟踪相关联,您需要添加 stackdriver 日志依赖项(我认为它仅在您的日志实现为 logback 时才有效)。

检查https://cloud.spring.io/spring-cloud-gcp/reference/html/#integration-with-logging