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/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 bca2ab75..be5c53ab 100644 --- a/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs +++ b/test/MVCFramework45.FunctionalTests/FunctionalTest/DependencyTelemetryMvcTests.cs @@ -11,11 +11,48 @@ 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 CorrelationInfoIsNotAddedToRequestHeaderIfUserAddDomainToExcludedList() + { +#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 e4bb7564..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.ImplementationType == 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