17

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

Sigurd Garshol
  • 1,376
  • 3
  • 15
  • 36
  • 1
    Have you tried `HttpContext.TraceIdentifier` ? – itminus Feb 10 '20 at 08:21
  • 2
    Yes, that yields a different Id that appears to span the current request, but it does not appear to propagate into the next application. E.g.: Web application A: `0HLTE0F4RQOTU:00000001` WebApplication B: `0HLTE0F5N8LF8:00000002` In my log sink, this value is logged as the "RequestId", which is somewhat confusing considering that the logged "TraceId" appears to be derived from the `Request-Id` header. Naming things is hard, I guess. – Sigurd Garshol Feb 10 '20 at 08:41

2 Answers2

21

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.

Thomas
  • 2,368
  • 20
  • 22
-1

You can get tracid and spanid in dictionary.

using var subject = _tracer.BuildSpan($"Operation").StartActive();

var spanContext = subject.Span.Context;
var dictionary = new Dictionary<string, string>();
_tracer.Inject(spanContext, BuiltinFormats.TextMap, new TextMapInjectAdapter(dictionary));
sa-es-ir
  • 3,722
  • 2
  • 13
  • 31
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 15:45