简体   繁体   English

添加 trace.id 和 transaction.id Springboot

[英]Add trace.id and transaction.id Springboot

I have a Springboot micro-service.我有一个 Springboot 微服务。 For logging I'm using Elastic common scheme, implemented using ecs-logging-java .对于日志记录,我使用 Elastic 通用方案,使用ecs-logging-java实现。

I want to set the trace.ID and a transaction.ID but I'm not sure how?我想设置 trace.ID 和 transaction.ID 但我不确定如何设置?

Bonus question, I'm I right in thinking trace.ID should be the ID to following the request through multiple system.奖金问题,我认为 trace.ID 应该是通过多个系统跟踪请求的 ID 是正确的。 transaction.ID is just for within the service? transaction.ID 仅用于服务内?

Step 1: Add trace id in the thread context.第 1 步:在线程上下文中添加跟踪 ID。

This can be done using MDC (manages contextual information on a per-thread basis).这可以使用MDC (在每个线程的基础上管理上下文信息)来完成。 Add the below line at the start of any method, from where you want to trace logs.在您要跟踪日志的任何方法的开头添加以下行。

MDC.put("TRACE_ID", UUID.randomUUID().toString());

Step 2: Add trace id in log format第 2 步:以日志格式添加跟踪 ID

Logs in java do not add trace id by default, so to make this possible we can add the trace id we previously added in the thread context to the log. java 中的日志默认不添加 trace id,因此为了实现这一点,我们可以将之前在线程上下文中添加的 trace id 添加到日志中。 This can be added to the application.properties I have added [%X{TRACE_ID}] in the default log console pattern.这可以添加到我在默认日志控制台模式中添加的[%X{TRACE_ID}]application.properties

logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} [%X{TRACE_ID}] %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}

I thought I had documented this but the closest I could come is in Log4j-Audit's RequestContext.我以为我已经记录了这一点,但我最接近的是 Log4j-Audit 的RequestContext。 . . I guess I need to add a new entry to my blog.我想我需要在我的博客中添加一个新条目。 The short answer to this is that you use Log4j 2's ThreadContextMap .对此的简短回答是您使用 Log4j 2 的ThreadContextMap First, when a user logs in create a session map that contains the data you want to capture in each request, such as the user's ip address and loginId.首先,当用户登录时,创建一个 session map,其中包含您要在每个请求中捕获的数据,例如用户的 ip 地址和 loginId。 Then create servlet Filter or Spring Interceptor to add that data as well as a unique request id to Log4j 2's Thread Context Map.然后创建 servlet 过滤器或 Spring 拦截器以将该数据以及唯一的请求 ID 添加到 Log4j 2 的线程上下文 Map。

All Leg Events will include the data in the ThreadContext.所有腿部事件都将包含 ThreadContext 中的数据。 The ECSLayout automatically includes all the fields in the ThreadContextMap. ECSLayout 自动包含 ThreadContextMap 中的所有字段。

Lastly, you need to propagate the RequestContext to downstream services.最后,您需要将 RequestContext 传播到下游服务。 You do that by creating a Spring Interceptor that gets wired into the RestTemplate which converts the RequestContext fields into HTTP headers.您可以通过创建一个 Spring 拦截器来实现这一点,该拦截器连接到 RestTemplate,它将 RequestContext 字段转换为 HTTP 标头。 The downstream service then has a Filter or Spring Interceptor that converts the headers back into RequestContext attributes.然后下游服务有一个过滤器或 Spring 拦截器,它将标头转换回 RequestContext 属性。 Log4j Audit (referenced above) has examples and implementations of all these components. Log4j 审计(上面引用)具有所有这些组件的示例和实现。

I should add that the method described above does not implement tracing as described by the WSC Trace Context spec so it is also not compatible with Elasticsearch's distributed tracing support .我应该补充一点,上述方法没有实现WSC Trace Context规范中描述的跟踪,因此它也与 Elasticsearch 的分布式跟踪支持不兼容。 It is worth noting however, that if one were to include Elasticsearch's distributed tracing support along with New Relic's distributed tracing support they would step on each other.然而值得注意的是,如果将 Elasticsearch 的分布式跟踪支持与 New Relic 的分布式跟踪支持一起包括在内,它们将相互影响。

Configure your logging patter as below如下配置您的日志记录模式

<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %thread [%X{trace-id}] [%-5level] %class{0} - %msg%n </pattern>

Put trace Id in MDC.将跟踪 ID 放入 MDC。 (MDC belongs to particular thread context) (MDC 属于特定的线程上下文)

  `MDC.put("trace-id", "traceid1");`

So whenever your log will print a message, it will print trace id.因此,每当您的日志将打印一条消息时,它都会打印跟踪 id。 Follow below artical.按照下面的文章。 http://logback.qos.ch/manual/mdc.html http://logback.qos.ch/manual/mdc.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM