Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current TraceId and SpanId

This article, https://devblogs.microsoft.com/aspnet/improvements-in-net-core-3-0-for-troubleshooting-and-monitoring-distributed-apps/, tells me that the field TraceId is available as a correlation id, which is great!

info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
     => ConnectionId:0HLR1BR0PL1CH 
     => RequestPath:/weatherforecastproxy 
        RequestId:0HLR1BR0PL1CH:00000001, 
        SpanId:|363a800a-4cf070ad93fe3bd8., 
        TraceId:363a800a-4cf070ad93fe3bd8, 
        ParentId: Executed endpoint 'FrontEndApp.Controllers.WeatherForecastProxyController.Get
(FrontEndApp)'

In fact, I can see that in our log sink this works as advertised: When web application A serves a request and in doing so invokes web application B, both of them write the same TraceId value to the log.

As far as I understand, any ASP.NET Core application that receives an incoming Request-Id header will attach the same header to outgoing requests, but if the header does not exist on the incoming request, an new value will be generated for the outgoing request.

We have been asked to add that value to the response from web application A, but it is (not surprisingly) not available on the incoming request.

I have been looking at the System.Diagnostics.Activity class, but accessing Activity.Current isn't giving me an instance with anything useful - the TraceID is just {} - i.e. empty.

My question is this: How can I access the TraceId value in the context of a web application?

-S

like image 451
Sigurd Garshol Avatar asked Feb 10 '20 07:02

Sigurd Garshol


People also ask

What is TraceId and SpanId?

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 TraceId in logs?

Traceid = each request and response traceid is same when calling same service or one service to another service. Spanid = Span Id is printed along with Trace Id. Span Id is different every request and response calling one service to another service. Zipkin-export = By default it is false.

Can we use sleuth without zipkin?

If you want to use only Spring Cloud Sleuth without the Zipkin integration, add the spring-cloud-starter-sleuth module to your project. We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself. Add the dependency to spring-cloud-starter-sleuth .


1 Answers

I had the same problem when I tried to add a header with TraceId value.

Doing some tests with ModelValidation, I saw then in this kind of error response the "traceId" value was correct, but I couldn't obtain this value from http context variable in any way.

Then I went to net core source code to see DefaultProblemDetailsFactory implementation and surprise! The "traceId" value is obtained doing this:

var traceId = Activity.Current?.Id ?? httpContext?.TraceIdentifier;

Yes, you can get THE traceId using Activity static variable.

like image 105
Sebastian Sanchez Schemberger Avatar answered Nov 20 '22 07:11

Sebastian Sanchez Schemberger