Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access W3C TraceContext Headers in a .net core 3.1 application?

These sources (microsoft docs, microsoft dev blog) state that in order to use the new W3C Trace Context Headers in a .net 3.0+ core application no extra configuration is required. However, we are not receiving traceparent or tracestate from any request made via a ServiceClient.

What is the proper setup process? How did you guys get access to a distributed trace id? We prefer to expose those values automaticaly without adding a lot of code to all existing services, if that's possible.

Thank you very much in advance!

PS: this is my first question here; please let me know if you need further information

like image 419
Tobias Sigel Avatar asked Oct 24 '25 21:10

Tobias Sigel


1 Answers

The System.Diagnostics.Activity Id in .net 5 has already been configured as the w3c standard. It means that all Actions will be identifiable by a traceparent id format and both traceparent and tracestate will be sent to downstream dependencies in the http request header. As you said, no extra configuration is required, but in .net 3 is different, the default id format is the Hierarchical one and it's downstream propagated as a HeaderRequestId. To see the Activity Id format, import the System.Diagnostics to your class and type: Console.WriteLine(Activity.Current.Id), you'll see a format like this: |fab6082c-46326cca135ffe48.1.. To change it to the w3c format in .net 3 is required to force it in your main method:

public static void Main(string[] args)
{
    Activity.DefaultIdFormat = ActivityIdFormat.W3C;
    Activity.ForceDefaultIdFormat = true;

    CreateHostBuilder(args).Build().Run();
}

Then, you'll see w3c's format and also the traceparent and tracestate fields been propagated to the downstream dependencies in the header.

If you want to send messages throw a broker, use the application-properties section in case of AMQP calls. You'll find some examples of both cases here and here.

like image 115
Luiz Lelis Avatar answered Oct 26 '25 10:10

Luiz Lelis