W3C Context Header in Spring Cloud Sleuth
search cancel

W3C Context Header in Spring Cloud Sleuth

book

Article ID: 294521

calendar_today

Updated On:

Products

VMware Spring Runtime

Issue/Introduction

This article discuss how to enable W3C Context Header in Spring Cloud Sleuth as well as how to check for W3C context headers and how to break down traceparent header fields.

Environment

Product Version: 2.0

Resolution

You can enable W3C headers in Spring Cloud Sleuth by adding the following in your application properties:

spring.sleuth.propagation.type=W3C

 
For the demo purposes, the code below prints all request headers:

@GetMapping("/getHeaders")
public String getHeaders(@RequestHeader Map<String, String> headers){
    headers.forEach((key, value) -> {
      System.out.println(">>>> " + String.format("Header '%s' = %s", key, value));
    });
   ....
  }

 
According to W3C Specs, there are 2 headers for W3C, traceparent & trace Id. With the code above printing headers, you can see that traceparent is present.  

2021-03-17 16:10:54.445 DEBUG [customer-name-service,21f0f#######00110,567f6ec####555d6] 35622 --- [nio-8002-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.zipkin.ZipkinW3cDemoNameApplication#getHeaders(Map)
 
>>>> Header 'accept' = text/plain, application/json, application/*+json, */*
>>>> Header 'traceparent' = 00-0000000000000000####25f4-01
>>>> Header 'user-agent' = Java/15
>>>> Header 'host' = localhost:8002
>>>> Header 'connection' = keep-alive


From the sample above, the traceparent header is propagated and consists of the following:

traceid -21f0f####100110 

span id - 567f6ec####555d6 


If you are wondering where you can see the following fields which are described in W3C Specs, these fields will not be coming from different headers but they will be present in the traceparent header:
 

  • version
  • trace-id
  • parent-id
  • trace-flags


From the sample traceparent header above:

00-0000000000000000####25f4-01

 
We breakdown traceparent as follows:
 

  • version - 00
  • trace-id - 0000000000000000####0110
  • parent-id - 3922d8####c4725f4
  • trace-flags - 01