Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which to log X-B3-SpanId or SpanId? X-B3-TraceId or TraceId? (Spring sleuth)

Spring's sleuth adds to MDC X-B3-SpanId and SpanId. (same for TraceId)

{X-B3-SpanId=0000000000000001, X-B3-TraceId=0000000000000002, X-Span-Export=false, spanExportable=false, spanId=0000000000000001, traceId=0000000000000002}

"X-B3-" prefixed values are identical to non-prefixed.
If one of them is redundant, I prefer to remove it. (to reduce verboseness of my logs)

Is there any difference between X-B3-SpanId and SpanId? (X-B3-TraceId and Traceid)
Is there good reason to log both?

Diagram in spring docs https://cloud.spring.io/spring-cloud-sleuth/2.1.x/single/spring-cloud-sleuth.html#_propagation show that "X-B3-" prefixed thing lives only in request header for transport, but if I make request without any of those headers, my log's MDC still contains "X-B3-" prefixed values.


Used dependencies:

  • org.springframework.boot:spring-boot-starter-log4j2:2.0.3.RELEASE
  • org.springframework.cloud:spring-cloud-sleuth-core:2.0.3.RELEASE

Just plain addition of dependency to gradle.build - no customizations/configurations regarding sleuth.

*MDC = log4j2's Mapped Diagnostic Contex https://logging.apache.org/log4j/2.x/manual/thread-context.html

like image 268
IndustryUser1942 Avatar asked Jun 26 '19 07:06

IndustryUser1942


People also ask

What is TraceId and SpanId in sleuth?

TraceId – This is an id that is assigned to a single request, job, or action. Something like each unique user initiated web request will have its own traceId. SpanId – Tracks a unit of work. Think of a request that consists of multiple steps. Each step could have its own spanId and be tracked individually.

What is X b3 SpanId?

The x-b3-spanid HTTP header is used by the Zipkin tracer in Envoy. The SpanId is 64-bit in length and indicates the position of the current operation in the trace tree. The value should not be interpreted: it may or may not be derived from the value of the TraceId.

What is TraceId and SpanId in Microservices?

traceId: It is a unique Id assigned to the request and is the same across all the microservices. spanId: It is a unique Id assigned to each operation. exportable: It is a boolean value and represents whether the log should be exported to Zipkin or not.

How do you use a spring sleuth?

Let's take a look at a simple example which uses Spring Cloud Sleuth to trace a request. Start out by going to start.spring.io and create a new Spring Boot app that has a dependency on Sleuth ( spring-cloud-starter-slueth ). Generate the project to download the code.


1 Answers

Looking at the class Slf4jScopeDecorator is clear that the two values are exactly the same and they maintain the X-B3* prefixed values only for backward compatibility:

 * Adds {@linkplain MDC} properties "traceId", "parentId", "spanId" and "spanExportable"
 * when a {@link brave.Tracer#currentSpan() span is current}. These can be used in log
 * correlation. Supports backward compatibility of MDC entries by adding legacy "X-B3"
 * entries to MDC context "X-B3-TraceId", "X-B3-ParentSpanId", "X-B3-SpanId" and
 * "X-B3-Sampled"

https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java

So you can use whatever of the two you want but be aware that they consider X-B3* variables legacy

like image 135
david.delamo Avatar answered Oct 03 '22 15:10

david.delamo