@@ -55,14 +55,14 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
5555 // TODO: If loggingEnabled check to change to if ActivityPropogation options are enabled
5656 if ( loggingEnabled || ( diagnosticListenerEnabled && _diagnosticListener . IsEnabled ( ActivityName , httpContext ) ) )
5757 {
58- // Review: Force creation of Activity by creating dummy listener
5958 _dummyListener = new ActivityListener ( )
6059 {
6160 ShouldListenTo = activitySource => activitySource . Name . Equals ( ActivitySourceName ) ,
6261 Sample = ( ref ActivityCreationOptions < ActivityContext > _ ) => ActivitySamplingResult . PropagationData
6362 } ;
6463 ActivitySource . AddActivityListener ( _dummyListener ) ;
6564 }
65+
6666 context . Activity = StartActivity ( httpContext , out var hasDiagnosticListener ) ;
6767 context . HasDiagnosticListener = hasDiagnosticListener ;
6868
@@ -269,7 +269,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
269269 hasDiagnosticListener = true ;
270270 }
271271
272- // TODO: Don't look at headers if there no listeners
272+ // Short-circuit to avoid doing an expensive header lookup
273+ if ( ! ( hasDiagnosticListener || _activitySource . HasListeners ( ) ) )
274+ {
275+ return null ;
276+ }
277+
273278 var headers = httpContext . Request . Headers ;
274279 if ( ! headers . TryGetValue ( HeaderNames . TraceParent , out var requestId ) )
275280 {
@@ -307,6 +312,12 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
307312
308313 // Review: Breaking change: We will no longer fire OnActivityImport before Activity.Start()
309314 _diagnosticListener . OnActivityImport ( activity , httpContext ) ;
315+
316+ if ( hasDiagnosticListener )
317+ {
318+ //Review: Do we need to explicity call this?
319+ _diagnosticListener . Write ( ActivityStartKey , httpContext ) ;
320+ }
310321 }
311322 return activity ;
312323 }
@@ -316,15 +327,20 @@ private void StopActivity(HttpContext httpContext, Activity activity, bool hasDi
316327 {
317328 if ( hasDiagnosticListener )
318329 {
319- // Stop sets the end time if it was unset, but we want it set before we issue the write
320- // so we do it now.
321- if ( activity . Duration == TimeSpan . Zero )
322- {
323- activity . SetEndTime ( DateTime . UtcNow ) ;
324- }
325- _diagnosticListener . Write ( ActivityStopKey , httpContext ) ;
330+ StopActivity ( activity , httpContext ) ;
331+ }
332+ activity . Stop ( ) ;
333+ }
334+
335+ private void StopActivity ( Activity activity , HttpContext httpContext )
336+ {
337+ // Stop sets the end time if it was unset, but we want it set before we issue the write
338+ // so we do it now.
339+ if ( activity . Duration == TimeSpan . Zero )
340+ {
341+ activity . SetEndTime ( DateTime . UtcNow ) ;
326342 }
327- activity . Dispose ( ) ;
343+ _diagnosticListener . Write ( ActivityStopKey , httpContext ) ;
328344 }
329345 }
330346}
0 commit comments