From b2671dc4e070fa211097f836540fde00574ace58 Mon Sep 17 00:00:00 2001 From: yantang Date: Mon, 1 May 2017 15:27:12 -0700 Subject: [PATCH 1/3] Fix issue #416. Add certain domains to dependency collector's exclude list. --- .../Extensions/ApplicationInsightsExtensions.cs | 11 ++++++++++- .../Extensions/ApplicationInsightsExtensionsTests.cs | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 0e6d44f4..db209124 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -135,7 +135,16 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(provider => { + var module = new DependencyTrackingTelemetryModule(); + var excludedDomains = module.ExcludeComponentCorrelationHttpHeadersOnDomains; + excludedDomains.Add("core.windows.net"); + excludedDomains.Add("core.chinacloudapi.cn"); + excludedDomains.Add("core.cloudapi.de"); + excludedDomains.Add("core.usgovcloudapi.net"); + + return module; + }); #if NET451 services.AddSingleton(); diff --git a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs index e4bb7564..4d8386d7 100644 --- a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs +++ b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs @@ -314,7 +314,7 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWi Assert.Equal(1, modules.Count()); #endif - var dependencyModule = services.FirstOrDefault(t => t.ImplementationType == typeof(DependencyTrackingTelemetryModule)); + var dependencyModule = services.FirstOrDefault(t => t.ImplementationFactory?.GetMethodInfo().ReturnType == typeof(DependencyTrackingTelemetryModule)); Assert.NotNull(dependencyModule); } From 19f9bc9c81e07cd11154ec5a3f55d393620cdf75 Mon Sep 17 00:00:00 2001 From: yantang Date: Tue, 2 May 2017 05:04:03 -0700 Subject: [PATCH 2/3] Add unit tests of the scenario that domain is excluded in DependencyCollector --- .../DependencyTelemetryMvcTests.cs | 68 +++++++++++++++++++ .../ApplicationInsightsExtensionsTests.cs | 7 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs b/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs index bca2ab75..5b0db2f5 100644 --- a/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs +++ b/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs @@ -11,11 +11,79 @@ namespace SampleWebAppIntegration.FunctionalTest using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; using Xunit; + using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.ApplicationInsights.DependencyCollector; + using Microsoft.Extensions.DependencyInjection; public class DependencyTelemetryMvcTests : TelemetryTestsBase { private const string assemblyName = "MVCFramework45.FunctionalTests"; + [Fact] + public void OperationIdOfOutgoingRequestIsPropagated() + { +#if !NET451 // Correlation is not supported in NET451. It works with NET46 and .Net core. + InProcessServer server; + + using (server = new InProcessServer(assemblyName, InProcessServer.UseApplicationInsights)) + { + using (var httpClient = new HttpClient()) + { + var task = httpClient.GetAsync(server.BaseHost + "/"); + task.Wait(TestTimeoutMs); + } + } + + var telemetries = server.BackChannel.Buffer; + try + { + Assert.True(telemetries.Count >= 2); + var requestTelemetry = telemetries.OfType().Single(); + var dependencyTelemetry = telemetries.OfType().Single(); + Assert.Equal(requestTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id); + } + catch (Exception e) + { + string data = DebugTelemetryItems(telemetries); + throw new Exception(data, e); + } +#endif + } + + [Fact] + public void OperationIdOfOutgoingRequestIsNotPropagatedIfDomainIsExcluded() + { +#if !NET451 // Correlation is not supported in NET451. It works with NET46 and .Net core. + InProcessServer server; + + using (server = new InProcessServer(assemblyName, InProcessServer.UseApplicationInsights)) + { + var dependencyCollectorModule = server.ApplicationServices.GetServices().OfType().Single(); + dependencyCollectorModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add(server.BaseHost); + + using (var httpClient = new HttpClient()) + { + var task = httpClient.GetAsync(server.BaseHost + "/"); + task.Wait(TestTimeoutMs); + } + } + + var telemetries = server.BackChannel.Buffer; + try + { + Assert.True(telemetries.Count >= 2); + var requestTelemetry = telemetries.OfType().Single(); + var dependencyTelemetry = telemetries.OfType().Single(); + Assert.NotEqual(requestTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id); + } + catch (Exception e) + { + string data = DebugTelemetryItems(telemetries); + throw new Exception(data, e); + } +#endif + } + [Fact] public void OperationIdOfRequestIsPropagatedToChildDependency() { diff --git a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs index 4d8386d7..931dbbe7 100644 --- a/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs +++ b/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests.cs @@ -314,8 +314,11 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWi Assert.Equal(1, modules.Count()); #endif - var dependencyModule = services.FirstOrDefault(t => t.ImplementationFactory?.GetMethodInfo().ReturnType == typeof(DependencyTrackingTelemetryModule)); - Assert.NotNull(dependencyModule); + var dependencyModuleDescriptor = services.FirstOrDefault(t => t.ImplementationFactory?.GetMethodInfo().ReturnType == typeof(DependencyTrackingTelemetryModule)); + Assert.NotNull(dependencyModuleDescriptor); + + var dependencyModule = modules.OfType().Single(); + Assert.True(dependencyModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Count > 0); } #if NET451 From 10fc0fd7713b676ae0876e54c26be8ce1fc1f8e8 Mon Sep 17 00:00:00 2001 From: yantang Date: Tue, 2 May 2017 13:21:59 -0700 Subject: [PATCH 3/3] Resolve comments on unit tests --- .../FunctionalTest/CorrelationMvcTests.cs | 39 +++++++++++++++++++ .../DependencyTelemetryMvcTests.cs | 33 +--------------- 2 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 test/MVCFramework45.FunctionalTests/FunctionalTest/CorrelationMvcTests.cs diff --git a/test/MVCFramework45.FunctionalTests/FunctionalTest/CorrelationMvcTests.cs b/test/MVCFramework45.FunctionalTests/FunctionalTest/CorrelationMvcTests.cs new file mode 100644 index 00000000..23d736c3 --- /dev/null +++ b/test/MVCFramework45.FunctionalTests/FunctionalTest/CorrelationMvcTests.cs @@ -0,0 +1,39 @@ +namespace MVCFramework45.FunctionalTests.FunctionalTest +{ + using FunctionalTestUtils; + using Microsoft.ApplicationInsights.DataContracts; + using System; + using System.Linq; + using System.Net.Http; + using Xunit; + + public class CorrelationMvcTests : TelemetryTestsBase + { + private const string assemblyName = "MVCFramework45.FunctionalTests"; + + [Fact] + public void CorrelationInfoIsPropagatedToDependendedService() + { +#if !NET451 // Correlation is not supported in NET451. It works with NET46 and .Net core. + InProcessServer server; + + using (server = new InProcessServer(assemblyName, InProcessServer.UseApplicationInsights)) + { + using (var httpClient = new HttpClient()) + { + var task = httpClient.GetAsync(server.BaseHost + "/"); + task.Wait(TestTimeoutMs); + } + } + + var telemetries = server.BackChannel.Buffer; + + Assert.True(telemetries.Count >= 2); + var requestTelemetry = telemetries.OfType().Single(); + var dependencyTelemetry = telemetries.OfType().Single(); + Assert.Equal(requestTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id); + Assert.Equal(requestTelemetry.Context.Operation.ParentId, dependencyTelemetry.Id); +#endif + } + } +} diff --git a/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs b/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs index 5b0db2f5..be5c53ab 100644 --- a/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs +++ b/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs @@ -20,38 +20,7 @@ public class DependencyTelemetryMvcTests : TelemetryTestsBase private const string assemblyName = "MVCFramework45.FunctionalTests"; [Fact] - public void OperationIdOfOutgoingRequestIsPropagated() - { -#if !NET451 // Correlation is not supported in NET451. It works with NET46 and .Net core. - InProcessServer server; - - using (server = new InProcessServer(assemblyName, InProcessServer.UseApplicationInsights)) - { - using (var httpClient = new HttpClient()) - { - var task = httpClient.GetAsync(server.BaseHost + "/"); - task.Wait(TestTimeoutMs); - } - } - - var telemetries = server.BackChannel.Buffer; - try - { - Assert.True(telemetries.Count >= 2); - var requestTelemetry = telemetries.OfType().Single(); - var dependencyTelemetry = telemetries.OfType().Single(); - Assert.Equal(requestTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id); - } - catch (Exception e) - { - string data = DebugTelemetryItems(telemetries); - throw new Exception(data, e); - } -#endif - } - - [Fact] - public void OperationIdOfOutgoingRequestIsNotPropagatedIfDomainIsExcluded() + public void CorrelationInfoIsNotAddedToRequestHeaderIfUserAddDomainToExcludedList() { #if !NET451 // Correlation is not supported in NET451. It works with NET46 and .Net core. InProcessServer server;