Google Cloud:X-Cloud-Trace-Context 与应用程序日志traceId 不同

发布于 2025-01-16 00:43:31 字数 1872 浏览 0 评论 0原文

我有一个部署在谷歌云中的 Spring Boot 应用程序,我面临一个相当奇怪的问题。 X-Cloud-Trace-Context 由 google 设置(我想是负载均衡器),但应用程序级别日志的traceId具有不同的traceId(与我在邮递员的响应标头中看到的不同)。

,在这张照片中,您可以看到邮递员标头,包括以 54e11 结尾的 TraceId(其中 o=1)。

输入图片此处描述

在此一项中,您可以看到请求日志条目的 TraceId 以 54e11 结尾,但上面的条目(您看不到,但它是应用程序日志中的条目)具有 TraceId以 992da 结尾(与 POST 请求日志中的不同)。

为什么请求日志和应用程序日志不同?我是否需要编写一个自定义过滤器来从标头中提取traceId(如果找到)并对其执行某些操作(例如将其放入MDC 映射中)?

我的 pom 具有以下依赖项:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>

我的附加程序配置(logback-spring.xml)非常基本:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="projectId" value="${projectId:-${GOOGLE_CLOUD_PROJECT}}"/>

    <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.springframework.cloud.gcp.logging.StackdriverJsonLayout">
                <projectId>${projectId}</projectId>
                <includeException>true</includeException>
            </layout>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE_JSON" />
    </root>
</configuration>

提前感谢您的宝贵时间。

我还想补充一点,我遵循了这篇文章中的赞成解决方案,但我仍然遇到同样的问题 Spring Boot 日志记录和 Google Cloud Platform 日志查看器

I have a spring boot app that's deployed in google cloud and I face a rather odd problem.
The X-Cloud-Trace-Context is set by google (load balancer I suppose) but the traceId of the app level logs have a different traceId (different from the one I see in the response headers in postman).

enter image description here

So in this photo you can see the postman headers including the traceId ending in 54e11 (with o=1).

enter image description here

In this one you can that the request log entry has the traceId ending in 54e11 but the entry above (the one you can't see but it's the one from the app log) has a traceId ending in 992da (which is different from the one in the POST request log).

Why is it different between request log and app log ? Do I need to write a custom filter to extract the traceId from the headers (if found) and do something with it like put it in the MDC map ?

My pom has the following dependency :

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>

My appender configuration (logback-spring.xml) is pretty basic :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="projectId" value="${projectId:-${GOOGLE_CLOUD_PROJECT}}"/>

    <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.springframework.cloud.gcp.logging.StackdriverJsonLayout">
                <projectId>${projectId}</projectId>
                <includeException>true</includeException>
            </layout>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE_JSON" />
    </root>
</configuration>

Thanks in advance for your time.

I want to also add that I followed the upvoted solution from this post but I still have the same problem Spring Boot Logging and Google Cloud Platform Log Viewer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文