diff --git a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs index b16e346aa..b08aa5b06 100644 --- a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs +++ b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs @@ -251,22 +251,24 @@ public void Dispose() /// internal void OnException(Exception exception, HttpRequestMessage request) { + // Even though we have the IsEnabled filter, to reject ApplicationInsights URLs before any events are fired, + // Exceptions are special and fired even if request instrumentation is disabled. + if (this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) + { + return; + } + Activity currentActivity = Activity.Current; if (currentActivity == null) { - DependencyCollectorEventSource.Log.CurrentActivityIsNull(); + DependencyCollectorEventSource.Log.CurrentActivityIsNull(HttpExceptionEventName); return; } DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerException(currentActivity.Id); - // Even though we have the IsEnabled filter, to reject ApplicationInsights URLs before any events are fired, - // Exceptions are special and fired even if request instrumentation is disabled. - if (!this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) - { - this.pendingExceptions.TryAdd(currentActivity.Id, exception); - this.client.TrackException(exception); - } + this.pendingExceptions.TryAdd(currentActivity.Id, exception); + this.client.TrackException(exception); } //// netcoreapp 2.0 event @@ -276,21 +278,23 @@ internal void OnException(Exception exception, HttpRequestMessage request) /// internal void OnActivityStart(HttpRequestMessage request) { + // Even though we have the IsEnabled filter to reject ApplicationInsights URLs before any events are fired, if there + // are multiple subscribers and one subscriber returns true to IsEnabled then all subscribers will receive the event. + if (this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) + { + return; + } + var currentActivity = Activity.Current; if (currentActivity == null) { - DependencyCollectorEventSource.Log.CurrentActivityIsNull(); + DependencyCollectorEventSource.Log.CurrentActivityIsNull(HttpOutStartEventName); return; } DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerStart(currentActivity.Id); - // Even though we have the IsEnabled filter to reject ApplicationInsights URLs before any events are fired, if there - // are multiple subscribers and one subscriber returns true to IsEnabled then all subscribers will receive the event. - if (!this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) - { - this.InjectRequestHeaders(request, this.configuration.InstrumentationKey); - } + this.InjectRequestHeaders(request, this.configuration.InstrumentationKey); } //// netcoreapp 2.0 event @@ -300,22 +304,22 @@ internal void OnActivityStart(HttpRequestMessage request) /// internal void OnActivityStop(HttpResponseMessage response, HttpRequestMessage request, TaskStatus requestTaskStatus) { - Activity currentActivity = Activity.Current; - if (currentActivity == null) + // Even though we have the IsEnabled filter to reject ApplicationInsights URLs before any events are fired, if there + // are multiple subscribers and one subscriber returns true to IsEnabled then all subscribers will receive the event. + if (this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) { - DependencyCollectorEventSource.Log.CurrentActivityIsNull(); return; } - DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerStop(currentActivity.Id); - - // Even though we have the IsEnabled filter to reject ApplicationInsights URLs before any events are fired, if there - // are multiple subscribers and one subscriber returns true to IsEnabled then all subscribers will receive the event. - if (this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(request.RequestUri)) + Activity currentActivity = Activity.Current; + if (currentActivity == null) { + DependencyCollectorEventSource.Log.CurrentActivityIsNull(HttpOutStopEventName); return; } + DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerStop(currentActivity.Id); + Uri requestUri = request.RequestUri; var resourceName = request.Method.Method + " " + requestUri.AbsolutePath; diff --git a/Src/DependencyCollector/Shared/Implementation/DependencyCollectorEventSource.cs b/Src/DependencyCollector/Shared/Implementation/DependencyCollectorEventSource.cs index 949cbe79d..dacb64d17 100644 --- a/Src/DependencyCollector/Shared/Implementation/DependencyCollectorEventSource.cs +++ b/Src/DependencyCollector/Shared/Implementation/DependencyCollectorEventSource.cs @@ -250,9 +250,9 @@ public void EndAsyncExceptionCallbackCalled(string id, string appDomainName = "I 23, Message = "Current Activity is null", Level = EventLevel.Error)] - public void CurrentActivityIsNull(string appDomainName = "Incorrect") + public void CurrentActivityIsNull(string diagnosticsSourceEventName, string appDomainName = "Incorrect") { - this.WriteEvent(23, this.ApplicationName); + this.WriteEvent(23, diagnosticsSourceEventName, this.ApplicationName); } [Event( diff --git a/Src/DependencyCollector/Shared/Implementation/DiagnosticSourceListenerBase.cs b/Src/DependencyCollector/Shared/Implementation/DiagnosticSourceListenerBase.cs index e41880c49..1310eede0 100644 --- a/Src/DependencyCollector/Shared/Implementation/DiagnosticSourceListenerBase.cs +++ b/Src/DependencyCollector/Shared/Implementation/DiagnosticSourceListenerBase.cs @@ -150,18 +150,18 @@ internal IndividualDiagnosticSourceListener(DiagnosticListener diagnosticListene public void OnNext(KeyValuePair evnt) { - Activity currentActivity = Activity.Current; - if (currentActivity == null) - { - DependencyCollectorEventSource.Log.CurrentActivityIsNull(); - return; - } - // while we provide IsEnabled callback during subscription, it does not gurantee events will not be fired // In case of multiple subscribers, it's enough for one to reply true to IsEnabled. // I.e. check for if activity is not disabled and particular handler wants to receive the event. if (this.telemetryDiagnosticSourceListener.IsActivityEnabled(evnt.Key, this.Context) && this.eventHandler.IsEventEnabled(evnt.Key, null, null)) { + Activity currentActivity = Activity.Current; + if (currentActivity == null) + { + DependencyCollectorEventSource.Log.CurrentActivityIsNull(evnt.Key); + return; + } + DependencyCollectorEventSource.Log.TelemetryDiagnosticSourceListenerEvent(evnt.Key, currentActivity.Id); try