@@ -27,7 +27,7 @@ internal static bool IsEnabled()
27
27
{
28
28
// check if there is a parent Activity (and propagation is not suppressed)
29
29
// or if someone listens to HttpHandlerDiagnosticListener
30
- return s_enableActivityPropagation && ( Activity . Current != null || s_diagnosticListener . IsEnabled ( ) ) ;
30
+ return Settings . s_activityPropagationEnabled && ( Activity . Current != null || Settings . s_diagnosticListener . IsEnabled ( ) ) ;
31
31
}
32
32
33
33
// SendAsyncCore returns already completed ValueTask for when async: false is passed.
@@ -58,10 +58,11 @@ private async ValueTask<HttpResponseMessage> SendAsyncCore(HttpRequestMessage re
58
58
}
59
59
60
60
Activity ? activity = null ;
61
+ DiagnosticListener diagnosticListener = Settings . s_diagnosticListener ;
61
62
62
63
// if there is no listener, but propagation is enabled (with previous IsEnabled() check)
63
64
// do not write any events just start/stop Activity and propagate Ids
64
- if ( ! s_diagnosticListener . IsEnabled ( ) )
65
+ if ( ! diagnosticListener . IsEnabled ( ) )
65
66
{
66
67
activity = new Activity ( DiagnosticsHandlerLoggingStrings . ActivityName ) ;
67
68
activity . Start ( ) ;
@@ -82,26 +83,26 @@ await base.SendAsync(request, cancellationToken).ConfigureAwait(false) :
82
83
Guid loggingRequestId = Guid . Empty ;
83
84
84
85
// There is a listener. Check if listener wants to be notified about HttpClient Activities
85
- if ( s_diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ActivityName , request ) )
86
+ if ( diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ActivityName , request ) )
86
87
{
87
88
activity = new Activity ( DiagnosticsHandlerLoggingStrings . ActivityName ) ;
88
89
89
90
// Only send start event to users who subscribed for it, but start activity anyway
90
- if ( s_diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ActivityStartName ) )
91
+ if ( diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ActivityStartName ) )
91
92
{
92
- s_diagnosticListener . StartActivity ( activity , new ActivityStartData ( request ) ) ;
93
+ diagnosticListener . StartActivity ( activity , new ActivityStartData ( request ) ) ;
93
94
}
94
95
else
95
96
{
96
97
activity . Start ( ) ;
97
98
}
98
99
}
99
100
// try to write System.Net.Http.Request event (deprecated)
100
- if ( s_diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . RequestWriteNameDeprecated ) )
101
+ if ( diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . RequestWriteNameDeprecated ) )
101
102
{
102
103
long timestamp = Stopwatch . GetTimestamp ( ) ;
103
104
loggingRequestId = Guid . NewGuid ( ) ;
104
- s_diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . RequestWriteNameDeprecated ,
105
+ diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . RequestWriteNameDeprecated ,
105
106
new RequestData ( request , loggingRequestId , timestamp ) ) ;
106
107
}
107
108
@@ -132,12 +133,12 @@ await base.SendAsync(request, cancellationToken).ConfigureAwait(false) :
132
133
{
133
134
taskStatus = TaskStatus . Faulted ;
134
135
135
- if ( s_diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ExceptionEventName ) )
136
+ if ( diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ExceptionEventName ) )
136
137
{
137
138
// If request was initially instrumented, Activity.Current has all necessary context for logging
138
139
// Request is passed to provide some context if instrumentation was disabled and to avoid
139
140
// extensive Activity.Tags usage to tunnel request properties
140
- s_diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . ExceptionEventName , new ExceptionData ( ex , request ) ) ;
141
+ diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . ExceptionEventName , new ExceptionData ( ex , request ) ) ;
141
142
}
142
143
throw ;
143
144
}
@@ -146,7 +147,7 @@ await base.SendAsync(request, cancellationToken).ConfigureAwait(false) :
146
147
// always stop activity if it was started
147
148
if ( activity != null )
148
149
{
149
- s_diagnosticListener . StopActivity ( activity , new ActivityStopData (
150
+ diagnosticListener . StopActivity ( activity , new ActivityStopData (
150
151
response ,
151
152
// If request is failed or cancelled, there is no response, therefore no information about request;
152
153
// pass the request in the payload, so consumers can have it in Stop for failed/canceled requests
@@ -155,10 +156,10 @@ await base.SendAsync(request, cancellationToken).ConfigureAwait(false) :
155
156
taskStatus ) ) ;
156
157
}
157
158
// Try to write System.Net.Http.Response event (deprecated)
158
- if ( s_diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ResponseWriteNameDeprecated ) )
159
+ if ( diagnosticListener . IsEnabled ( DiagnosticsHandlerLoggingStrings . ResponseWriteNameDeprecated ) )
159
160
{
160
161
long timestamp = Stopwatch . GetTimestamp ( ) ;
161
- s_diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . ResponseWriteNameDeprecated ,
162
+ diagnosticListener . Write ( DiagnosticsHandlerLoggingStrings . ResponseWriteNameDeprecated ,
162
163
new ResponseData (
163
164
response ,
164
165
loggingRequestId ,
@@ -246,29 +247,35 @@ internal ResponseData(HttpResponseMessage? response, Guid loggingRequestId, long
246
247
public override string ToString ( ) => $ "{{ { nameof ( Response ) } = { Response } , { nameof ( LoggingRequestId ) } = { LoggingRequestId } , { nameof ( Timestamp ) } = { Timestamp } , { nameof ( RequestTaskStatus ) } = { RequestTaskStatus } }}";
247
248
}
248
249
249
- private const string EnableActivityPropagationEnvironmentVariableSettingName = "DOTNET_SYSTEM_NET_HTTP_ENABLEACTIVITYPROPAGATION" ;
250
- private const string EnableActivityPropagationAppCtxSettingName = "System.Net.Http.EnableActivityPropagation" ;
250
+ private static class Settings
251
+ {
252
+ private const string EnableActivityPropagationEnvironmentVariableSettingName = "DOTNET_SYSTEM_NET_HTTP_ENABLEACTIVITYPROPAGATION" ;
253
+ private const string EnableActivityPropagationAppCtxSettingName = "System.Net.Http.EnableActivityPropagation" ;
251
254
252
- private static readonly bool s_enableActivityPropagation = GetEnableActivityPropagationValue ( ) ;
255
+ public static readonly bool s_activityPropagationEnabled = GetEnableActivityPropagationValue ( ) ;
253
256
254
- private static bool GetEnableActivityPropagationValue ( )
255
- {
256
- // First check for the AppContext switch, giving it priority over the environment variable.
257
- if ( AppContext . TryGetSwitch ( EnableActivityPropagationAppCtxSettingName , out bool enableActivityPropagation ) )
257
+ private static bool GetEnableActivityPropagationValue ( )
258
258
{
259
- return enableActivityPropagation ;
260
- }
259
+ // First check for the AppContext switch, giving it priority over the environment variable.
260
+ if ( AppContext . TryGetSwitch ( EnableActivityPropagationAppCtxSettingName , out bool enableActivityPropagation ) )
261
+ {
262
+ return enableActivityPropagation ;
263
+ }
261
264
262
- // AppContext switch wasn't used. Check the environment variable to determine which handler should be used.
263
- string ? envVar = Environment . GetEnvironmentVariable ( EnableActivityPropagationEnvironmentVariableSettingName ) ;
264
- if ( envVar != null && ( envVar . Equals ( "false" , StringComparison . OrdinalIgnoreCase ) || envVar . Equals ( "0" ) ) )
265
- {
266
- // Suppress Activity propagation.
267
- return false ;
265
+ // AppContext switch wasn't used. Check the environment variable to determine which handler should be used.
266
+ string ? envVar = Environment . GetEnvironmentVariable ( EnableActivityPropagationEnvironmentVariableSettingName ) ;
267
+ if ( envVar != null && ( envVar . Equals ( "false" , StringComparison . OrdinalIgnoreCase ) || envVar . Equals ( "0" ) ) )
268
+ {
269
+ // Suppress Activity propagation.
270
+ return false ;
271
+ }
272
+
273
+ // Defaults to enabling Activity propagation.
274
+ return true ;
268
275
}
269
276
270
- // Defaults to enabling Activity propagation.
271
- return true ;
277
+ public static readonly DiagnosticListener s_diagnosticListener =
278
+ new DiagnosticListener ( DiagnosticsHandlerLoggingStrings . DiagnosticListenerName ) ;
272
279
}
273
280
274
281
private static void InjectHeaders ( Activity currentActivity , HttpRequestMessage request )
@@ -309,9 +316,6 @@ private static void InjectHeaders(Activity currentActivity, HttpRequestMessage r
309
316
}
310
317
}
311
318
312
- private static readonly DiagnosticListener s_diagnosticListener =
313
- new DiagnosticListener ( DiagnosticsHandlerLoggingStrings . DiagnosticListenerName ) ;
314
-
315
319
#endregion
316
320
}
317
321
}
0 commit comments