@@ -24,6 +24,8 @@ internal class HostingApplicationDiagnostics
24
24
25
25
private const string RequestIdHeaderName = "Request-Id" ;
26
26
private const string CorrelationContextHeaderName = "Correlation-Context" ;
27
+ private const string TraceParentHeaderName = "traceparent" ;
28
+ private const string TraceStateHeaderName = "tracestate" ;
27
29
28
30
private readonly DiagnosticListener _diagnosticListener ;
29
31
private readonly ILogger _logger ;
@@ -49,19 +51,12 @@ public void BeginRequest(HttpContext httpContext, ref HostingApplication.Context
49
51
var diagnosticListenerEnabled = _diagnosticListener . IsEnabled ( ) ;
50
52
var loggingEnabled = _logger . IsEnabled ( LogLevel . Critical ) ;
51
53
52
- // If logging is enabled or the diagnostic listener is enabled, try to get the correlation
53
- // id from the header
54
- StringValues correlationId = default ;
55
- if ( diagnosticListenerEnabled || loggingEnabled )
56
- {
57
- httpContext . Request . Headers . TryGetValue ( RequestIdHeaderName , out correlationId ) ;
58
- }
59
54
60
55
if ( diagnosticListenerEnabled )
61
56
{
62
57
if ( _diagnosticListener . IsEnabled ( ActivityName , httpContext ) )
63
58
{
64
- context . Activity = StartActivity ( httpContext , correlationId ) ;
59
+ context . Activity = StartActivity ( httpContext ) ;
65
60
}
66
61
if ( _diagnosticListener . IsEnabled ( DeprecatedDiagnosticsBeginRequestKey ) )
67
62
{
@@ -73,6 +68,12 @@ public void BeginRequest(HttpContext httpContext, ref HostingApplication.Context
73
68
// To avoid allocation, return a null scope if the logger is not on at least to some degree.
74
69
if ( loggingEnabled )
75
70
{
71
+ // Get the request ID (first try TraceParent header otherwise Request-ID header
72
+ if ( ! httpContext . Request . Headers . TryGetValue ( TraceParentHeaderName , out var correlationId ) )
73
+ {
74
+ httpContext . Request . Headers . TryGetValue ( RequestIdHeaderName , out correlationId ) ;
75
+ }
76
+
76
77
// Scope may be relevant for a different level of logging, so we always create it
77
78
// see: https://github.com/aspnet/Hosting/pull/944
78
79
// Scope can be null if logging is not on.
@@ -240,12 +241,22 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
240
241
}
241
242
242
243
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
243
- private Activity StartActivity ( HttpContext httpContext , StringValues requestId )
244
+ private Activity StartActivity ( HttpContext httpContext )
244
245
{
245
246
var activity = new Activity ( ActivityName ) ;
247
+
248
+ if ( ! httpContext . Request . Headers . TryGetValue ( TraceParentHeaderName , out var requestId ) )
249
+ {
250
+ httpContext . Request . Headers . TryGetValue ( RequestIdHeaderName , out requestId ) ;
251
+ }
252
+
246
253
if ( ! StringValues . IsNullOrEmpty ( requestId ) )
247
254
{
248
255
activity . SetParentId ( requestId ) ;
256
+ if ( httpContext . Request . Headers . TryGetValue ( TraceStateHeaderName , out var traceState ) )
257
+ {
258
+ activity . TraceStateString = traceState ;
259
+ }
249
260
250
261
// We expect baggage to be empty by default
251
262
// Only very advanced users will be using it in near future, we encourage them to keep baggage small (few items)
@@ -280,4 +291,4 @@ private void StopActivity(HttpContext httpContext, Activity activity)
280
291
_diagnosticListener . StopActivity ( activity , new { HttpContext = httpContext } ) ;
281
292
}
282
293
}
283
- }
294
+ }
0 commit comments