简体   繁体   中英

Add trace.id and transaction.id Springboot

I have a Springboot micro-service. For logging I'm using Elastic common scheme, implemented using ecs-logging-java .

I want to set the trace.ID and a transaction.ID but I'm not sure how?

Bonus question, I'm I right in thinking trace.ID should be the ID to following the request through multiple system. transaction.ID is just for within the service?

Step 1: Add trace id in the thread context.

This can be done using MDC (manages contextual information on a per-thread basis). 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

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. This can be added to the application.properties I have added [%X{TRACE_ID}] in the default log console pattern.

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. . 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 . 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. 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.

All Leg Events will include the data in the ThreadContext. The ECSLayout automatically includes all the fields in the ThreadContextMap.

Lastly, you need to propagate the RequestContext to downstream services. You do that by creating a Spring Interceptor that gets wired into the RestTemplate which converts the RequestContext fields into HTTP headers. The downstream service then has a Filter or Spring Interceptor that converts the headers back into RequestContext attributes. Log4j Audit (referenced above) has examples and implementations of all these components.

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 . 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.

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. (MDC belongs to particular thread context)

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

So whenever your log will print a message, it will print trace id. Follow below artical. http://logback.qos.ch/manual/mdc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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