1
0
Форкнуть 0

Provide easy way to turnoff built-in modules, and an ikey read fix. (#990)

* Fix 988 989
This commit is contained in:
Cijo Thomas 2019-09-25 10:17:00 -07:00 коммит произвёл GitHub
Родитель f328f948b2
Коммит 1387908e47
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 436 добавлений и 106 удалений

Просмотреть файл

@ -2,7 +2,8 @@
## Version 2.12.0-beta1 ## Version 2.12.0-beta1
- Skipping version numbers to keep in sync with Base SDK. - Skipping version numbers to keep in sync with Base SDK.
- [Fix Null/Empty Ikey from ApplicationInsightsServiceOptions overrding one from appsettings.json](https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/989)
- [Provide ApplicationInsightsServiceOptions for easy disabling of any default TelemetryModules](https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/988)
## Version 2.8.0 ## Version 2.8.0
- Updated Bask SDK/Web SDK/Logging Adaptor SDK to 2.11.0 - Updated Bask SDK/Web SDK/Logging Adaptor SDK to 2.11.0

Просмотреть файл

@ -31,6 +31,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Shared.Implementation;
/// <summary> /// <summary>
/// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application. /// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application.
@ -109,19 +110,7 @@
ApplicationInsightsServiceOptions options) ApplicationInsightsServiceOptions options)
{ {
services.AddApplicationInsightsTelemetry(); services.AddApplicationInsightsTelemetry();
services.Configure((ApplicationInsightsServiceOptions o) => ConfigureAiServiceOption(services, options);
{
o.ApplicationVersion = options.ApplicationVersion;
o.DeveloperMode = options.DeveloperMode;
o.EnableAdaptiveSampling = options.EnableAdaptiveSampling;
o.EnableAuthenticationTrackingJavaScript = options.EnableAuthenticationTrackingJavaScript;
o.EnableDebugLogger = options.EnableDebugLogger;
o.EnableQuickPulseMetricStream = options.EnableQuickPulseMetricStream;
o.EndpointAddress = options.EndpointAddress;
o.InstrumentationKey = options.InstrumentationKey;
o.EnableHeartbeat = options.EnableHeartbeat;
o.AddAutoCollectedMetricExtractor = options.AddAutoCollectedMetricExtractor;
});
return services; return services;
} }
@ -143,10 +132,27 @@
AddCommonInitializers(services); AddCommonInitializers(services);
// Request Tracking. // Request Tracking.
services.AddSingleton<ITelemetryModule, RequestTrackingTelemetryModule>(); services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
var appIdProvider = provider.GetService<IApplicationIdProvider>();
if (options.EnableRequestTrackingTelemetryModule)
{
return new RequestTrackingTelemetryModule(appIdProvider);
}
else
{
return new NoOpTelemetryModule();
}
});
services.ConfigureTelemetryModule<RequestTrackingTelemetryModule>((module, options) => services.ConfigureTelemetryModule<RequestTrackingTelemetryModule>((module, options) =>
{
if(options.EnableRequestTrackingTelemetryModule)
{ {
module.CollectionOptions = options.RequestCollectionOptions; module.CollectionOptions = options.RequestCollectionOptions;
}
}); });
AddCommonTelemetryModules(services); AddCommonTelemetryModules(services);

Просмотреть файл

@ -86,19 +86,7 @@ namespace Microsoft.Extensions.DependencyInjection
ApplicationInsightsServiceOptions options) ApplicationInsightsServiceOptions options)
{ {
services.AddApplicationInsightsTelemetryWorkerService(); services.AddApplicationInsightsTelemetryWorkerService();
services.Configure((ApplicationInsightsServiceOptions o) => ConfigureAiServiceOption(services, options);
{
o.ApplicationVersion = options.ApplicationVersion;
o.DeveloperMode = options.DeveloperMode;
o.EnableAdaptiveSampling = options.EnableAdaptiveSampling;
o.EnableAuthenticationTrackingJavaScript = options.EnableAuthenticationTrackingJavaScript;
o.EnableDebugLogger = options.EnableDebugLogger;
o.EnableQuickPulseMetricStream = options.EnableQuickPulseMetricStream;
o.EndpointAddress = options.EndpointAddress;
o.InstrumentationKey = options.InstrumentationKey;
o.EnableHeartbeat = options.EnableHeartbeat;
o.AddAutoCollectedMetricExtractor = options.AddAutoCollectedMetricExtractor;
});
return services; return services;
} }

Просмотреть файл

@ -32,6 +32,7 @@
#endif #endif
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId; using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId;
using Shared.Implementation;
/// <summary> /// <summary>
/// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application. /// Extension methods for <see cref="IServiceCollection"/> that allow adding Application Insights services to application.
@ -274,13 +275,77 @@
private static void AddCommonTelemetryModules(IServiceCollection services) private static void AddCommonTelemetryModules(IServiceCollection services)
{ {
services.AddSingleton<ITelemetryModule, PerformanceCollectorModule>(); services.AddSingleton<ITelemetryModule>(provider =>
services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>(); {
services.AddSingleton<ITelemetryModule, AzureInstanceMetadataTelemetryModule>(); var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
services.AddSingleton<ITelemetryModule, QuickPulseTelemetryModule>();
if(options.EnablePerformanceCounterCollectionModule)
{
return new PerformanceCollectorModule();
}
else
{
return new NoOpTelemetryModule();
}
});
services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
if (options.EnableAppServicesHeartbeatTelemetryModule)
{
return new AppServicesHeartbeatTelemetryModule();
}
else
{
return new NoOpTelemetryModule();
}
});
services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
if (options.EnableAzureInstanceMetadataTelemetryModule)
{
return new AzureInstanceMetadataTelemetryModule();
}
else
{
return new NoOpTelemetryModule();
}
});
services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
if (options.EnableQuickPulseMetricStream)
{
return new QuickPulseTelemetryModule();
}
else
{
return new NoOpTelemetryModule();
}
});
AddAndConfigureDependencyTracking(services); AddAndConfigureDependencyTracking(services);
#if NETSTANDARD2_0 #if NETSTANDARD2_0
services.AddSingleton<ITelemetryModule, EventCounterCollectionModule>(); services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
if (options.EnableEventCounterCollectionModule)
{
return new EventCounterCollectionModule();
}
else
{
return new NoOpTelemetryModule();
}
});
#endif #endif
} }
@ -306,8 +371,23 @@
private static void AddAndConfigureDependencyTracking(IServiceCollection services) private static void AddAndConfigureDependencyTracking(IServiceCollection services)
{ {
services.AddSingleton<ITelemetryModule, DependencyTrackingTelemetryModule>(); services.AddSingleton<ITelemetryModule>(provider =>
{
var options = provider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
if (options.EnableDependencyTrackingTelemetryModule)
{
return new DependencyTrackingTelemetryModule();
}
else
{
return new NoOpTelemetryModule();
}
});
services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) => services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) =>
{
if(o.EnableDependencyTrackingTelemetryModule)
{ {
module.EnableLegacyCorrelationHeadersInjection = module.EnableLegacyCorrelationHeadersInjection =
o.DependencyCollectionOptions.EnableLegacyCorrelationHeadersInjection; o.DependencyCollectionOptions.EnableLegacyCorrelationHeadersInjection;
@ -327,6 +407,7 @@
var includedActivities = module.IncludeDiagnosticSourceActivities; var includedActivities = module.IncludeDiagnosticSourceActivities;
includedActivities.Add("Microsoft.Azure.EventHubs"); includedActivities.Add("Microsoft.Azure.EventHubs");
includedActivities.Add("Microsoft.Azure.ServiceBus"); includedActivities.Add("Microsoft.Azure.ServiceBus");
}
}); });
} }
@ -341,6 +422,8 @@
private static void ConfigureEventCounterModuleWithSystemCounters(IServiceCollection services) private static void ConfigureEventCounterModuleWithSystemCounters(IServiceCollection services)
{ {
services.ConfigureTelemetryModule<EventCounterCollectionModule>((eventCounterModule, options) => services.ConfigureTelemetryModule<EventCounterCollectionModule>((eventCounterModule, options) =>
{
if(options.EnableEventCounterCollectionModule)
{ {
// Ref this code for actual names. https://github.com/dotnet/coreclr/blob/dbc5b56c48ce30635ee8192c9814c7de998043d5/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs // Ref this code for actual names. https://github.com/dotnet/coreclr/blob/dbc5b56c48ce30635ee8192c9814c7de998043d5/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "cpu-usage"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "cpu-usage");
@ -362,22 +445,65 @@
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "threadpool-queue-length"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "threadpool-queue-length");
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "threadpool-completed-items-count"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "threadpool-completed-items-count");
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "active-timer-count"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForSystemRuntime, "active-timer-count");
}
}); });
} }
private static void ConfigureEventCounterModuleWithAspNetCounters(IServiceCollection services) private static void ConfigureEventCounterModuleWithAspNetCounters(IServiceCollection services)
{ {
services.ConfigureTelemetryModule<EventCounterCollectionModule>((eventCounterModule, options) => services.ConfigureTelemetryModule<EventCounterCollectionModule>((eventCounterModule, options) =>
{
if (options.EnableEventCounterCollectionModule)
{ {
// Ref this code for actual names. https://github.com/aspnet/AspNetCore/blob/f3f9a1cdbcd06b298035b523732b9f45b1408461/src/Hosting/Hosting/src/Internal/HostingEventSource.cs // Ref this code for actual names. https://github.com/aspnet/AspNetCore/blob/f3f9a1cdbcd06b298035b523732b9f45b1408461/src/Hosting/Hosting/src/Internal/HostingEventSource.cs
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "requests-per-second"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "requests-per-second");
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "total-requests"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "total-requests");
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "current-requests"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "current-requests");
AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "failed-requests"); AddEventCounterIfNotExist(eventCounterModule, EventSourceNameForAspNetCoreHosting, "failed-requests");
}
}); });
} }
#endif #endif
private static void ConfigureAiServiceOption(IServiceCollection services, ApplicationInsightsServiceOptions options)
{
services.Configure((ApplicationInsightsServiceOptions o) =>
{
if (options.DeveloperMode != null)
{
o.DeveloperMode = options.DeveloperMode;
}
if (!string.IsNullOrEmpty(options.EndpointAddress))
{
o.EndpointAddress = options.EndpointAddress;
}
if (!string.IsNullOrEmpty(options.InstrumentationKey))
{
o.InstrumentationKey = options.InstrumentationKey;
}
o.ApplicationVersion = options.ApplicationVersion;
o.EnableAdaptiveSampling = options.EnableAdaptiveSampling;
o.EnableDebugLogger = options.EnableDebugLogger;
o.EnableQuickPulseMetricStream = options.EnableQuickPulseMetricStream;
o.EnableHeartbeat = options.EnableHeartbeat;
o.AddAutoCollectedMetricExtractor = options.AddAutoCollectedMetricExtractor;
o.EnablePerformanceCounterCollectionModule = options.EnablePerformanceCounterCollectionModule;
o.EnableDependencyTrackingTelemetryModule = options.EnableDependencyTrackingTelemetryModule;
o.EnableAppServicesHeartbeatTelemetryModule = options.EnableAppServicesHeartbeatTelemetryModule;
o.EnableAzureInstanceMetadataTelemetryModule = options.EnableAzureInstanceMetadataTelemetryModule;
#if NETSTANDARD2_0
o.EnableEventCounterCollectionModule = options.EnableEventCounterCollectionModule;
#endif
#if AI_ASPNETCORE_WEB
o.EnableAuthenticationTrackingJavaScript = options.EnableAuthenticationTrackingJavaScript;
o.EnableRequestTrackingTelemetryModule = options.EnableRequestTrackingTelemetryModule;
#endif
});
}
private static void AddApplicationInsightsLoggerProvider(IServiceCollection services) private static void AddApplicationInsightsLoggerProvider(IServiceCollection services)
{ {
#if NETSTANDARD2_0 #if NETSTANDARD2_0

Просмотреть файл

@ -18,15 +18,24 @@
/// </summary> /// </summary>
public ApplicationInsightsServiceOptions() public ApplicationInsightsServiceOptions()
{ {
this.EnablePerformanceCounterCollectionModule = true;
this.EnableQuickPulseMetricStream = true; this.EnableQuickPulseMetricStream = true;
this.EnableAdaptiveSampling = true; this.EnableAdaptiveSampling = true;
this.EnableDebugLogger = true; this.EnableDebugLogger = true;
this.EnableAuthenticationTrackingJavaScript = false;
this.EnableHeartbeat = true; this.EnableHeartbeat = true;
this.AddAutoCollectedMetricExtractor = true; this.AddAutoCollectedMetricExtractor = true;
#if AI_ASPNETCORE_WEB #if AI_ASPNETCORE_WEB
this.EnableRequestTrackingTelemetryModule = true;
this.EnableAuthenticationTrackingJavaScript = false;
this.RequestCollectionOptions = new RequestCollectionOptions(); this.RequestCollectionOptions = new RequestCollectionOptions();
#endif #endif
#if NETSTANDARD2_0
this.EnableEventCounterCollectionModule = true;
#endif
this.EnableDependencyTrackingTelemetryModule = true;
this.EnableAzureInstanceMetadataTelemetryModule = true;
this.EnableAppServicesHeartbeatTelemetryModule = true;
this.DependencyCollectionOptions = new DependencyCollectionOptions(); this.DependencyCollectionOptions = new DependencyCollectionOptions();
this.ApplicationVersion = Assembly.GetEntryAssembly()?.GetName().Version.ToString(); this.ApplicationVersion = Assembly.GetEntryAssembly()?.GetName().Version.ToString();
} }
@ -37,6 +46,38 @@
/// </summary> /// </summary>
public bool EnableQuickPulseMetricStream { get; set; } public bool EnableQuickPulseMetricStream { get; set; }
/// <summary>
/// Gets or sets a value indicating whether PerformanceCollectorModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnablePerformanceCounterCollectionModule { get; set; }
/// <summary>
/// Gets or sets a value indicating whether AppServicesHeartbeatTelemetryModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnableAppServicesHeartbeatTelemetryModule { get; set; }
/// <summary>
/// Gets or sets a value indicating whether AzureInstanceMetadataTelemetryModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnableAzureInstanceMetadataTelemetryModule { get; set; }
/// <summary>
/// Gets or sets a value indicating whether DependencyTrackingTelemetryModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnableDependencyTrackingTelemetryModule { get; set; }
#if NETSTANDARD2_0
/// <summary>
/// Gets or sets a value indicating whether EventCounterCollectionModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnableEventCounterCollectionModule { get; set; }
#endif
/// <summary> /// <summary>
/// Gets or sets a value indicating whether telemetry processor that controls sampling is added to the service. /// Gets or sets a value indicating whether telemetry processor that controls sampling is added to the service.
/// Setting EnableAdaptiveSampling to <c>false</c>, will disable the default adaptive sampling feature. Defaults to <code>true</code>. /// Setting EnableAdaptiveSampling to <c>false</c>, will disable the default adaptive sampling feature. Defaults to <code>true</code>.
@ -68,12 +109,6 @@
/// </summary> /// </summary>
public bool EnableDebugLogger { get; set; } public bool EnableDebugLogger { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a JavaScript snippet to track the current authenticated user should
/// be printed along with the main ApplicationInsights tracking script.
/// </summary>
public bool EnableAuthenticationTrackingJavaScript { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether heartbeats are enabled. /// Gets or sets a value indicating whether heartbeats are enabled.
/// </summary> /// </summary>
@ -89,6 +124,18 @@
/// Gets <see cref="RequestCollectionOptions"/> that allow to manage <see cref="RequestTrackingTelemetryModule"/> /// Gets <see cref="RequestCollectionOptions"/> that allow to manage <see cref="RequestTrackingTelemetryModule"/>
/// </summary> /// </summary>
public RequestCollectionOptions RequestCollectionOptions { get; } public RequestCollectionOptions RequestCollectionOptions { get; }
/// <summary>
/// Gets or sets a value indicating whether RequestTrackingTelemetryModule should be enabled.
/// Defaults to <code>true</code>.
/// </summary>
public bool EnableRequestTrackingTelemetryModule { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a JavaScript snippet to track the current authenticated user should
/// be printed along with the main ApplicationInsights tracking script.
/// </summary>
public bool EnableAuthenticationTrackingJavaScript { get; set; }
#endif #endif

Просмотреть файл

@ -0,0 +1,17 @@
using Microsoft.ApplicationInsights.Extensibility;
using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Implementation
{
/// <summary>
/// No-op telemetry module that is added instead of actual one, when the actual module is disabled
/// </summary>
internal class NoOpTelemetryModule : ITelemetryModule
{
public void Initialize(TelemetryConfiguration configuration)
{
}
}
}

Просмотреть файл

@ -14,6 +14,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DependencyCollectionOptions.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Extensions\DependencyCollectionOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\ITelemetryModuleConfigurator.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Implementation\ITelemetryModuleConfigurator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\ITelemetryProcessorFactory.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Implementation\ITelemetryProcessorFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\NoOpTelemetryModule.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryConfigurationOptionsSetup.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryConfigurationOptionsSetup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryModuleConfigurator.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryModuleConfigurator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryProcessorFactory.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Implementation\TelemetryProcessorFactory.cs" />

Просмотреть файл

@ -334,6 +334,39 @@ namespace Microsoft.Extensions.DependencyInjection.Test
} }
} }
/// <summary>
/// Validates that while using services.AddApplicationInsightsTelemetry(ApplicationInsightsServiceOptions), with null ikey
/// and endpoint, ikey and endpoint from AppSettings.Json is NOT overwritten with the null/empty ones from
/// ApplicationInsightsServiceOptions
/// </summary>
[Fact]
public static void AddApplicationInsightsTelemetryDoesNotOverrideEmptyInstrumentationKeyFromAiOptions()
{
// Create new options, which will be default have null ikey and endpoint.
var options = new ApplicationInsightsServiceOptions();
string ikey = Guid.NewGuid().ToString();
string text = File.ReadAllText("appsettings.json");
try
{
text = text.Replace("ikeyhere", ikey);
text = text.Replace("hosthere", "newhost");
File.WriteAllText("appsettings.json", text);
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
services.AddApplicationInsightsTelemetry(options);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration();
Assert.Equal(ikey, telemetryConfiguration.InstrumentationKey);
Assert.Equal("http://newhost/v2/track/", telemetryConfiguration.DefaultTelemetrySink.TelemetryChannel.EndpointAddress);
}
finally
{
text = text.Replace(ikey, "ikeyhere");
text = text.Replace("newhost", "hosthere");
File.WriteAllText("appsettings.json", text);
}
}
[Fact] [Fact]
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromEnvironment() public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromEnvironment()
{ {
@ -498,29 +531,28 @@ namespace Microsoft.Extensions.DependencyInjection.Test
Assert.Equal(6, modules.Count()); Assert.Equal(6, modules.Count());
#endif #endif
var perfCounterModule = modules.OfType<PerformanceCollectorModule>().Single();
var perfCounterModule = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(PerformanceCollectorModule));
Assert.NotNull(perfCounterModule); Assert.NotNull(perfCounterModule);
#if NETCOREAPP2_0 #if NETCOREAPP2_0
var eventCounterModule = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(EventCounterCollectionModule)); var eventCounterModule = modules.OfType<EventCounterCollectionModule>().Single();
Assert.NotNull(eventCounterModule); Assert.NotNull(eventCounterModule);
#endif #endif
var dependencyModuleDescriptor = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(DependencyTrackingTelemetryModule)); var dependencyModuleDescriptor = modules.OfType<DependencyTrackingTelemetryModule>().Single();
Assert.NotNull(dependencyModuleDescriptor); Assert.NotNull(dependencyModuleDescriptor);
var reqModuleDescriptor = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(RequestTrackingTelemetryModule)); var reqModuleDescriptor = modules.OfType<RequestTrackingTelemetryModule>().Single();
Assert.NotNull(reqModuleDescriptor); Assert.NotNull(reqModuleDescriptor);
var appServiceHeartBeatModuleDescriptor = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(AppServicesHeartbeatTelemetryModule)); var appServiceHeartBeatModuleDescriptor = modules.OfType<AppServicesHeartbeatTelemetryModule>().Single();
Assert.NotNull(appServiceHeartBeatModuleDescriptor); Assert.NotNull(appServiceHeartBeatModuleDescriptor);
var azureMetadataHeartBeatModuleDescriptor = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(AzureInstanceMetadataTelemetryModule)); var azureMetadataHeartBeatModuleDescriptor = modules.OfType<AzureInstanceMetadataTelemetryModule>().Single();
Assert.NotNull(azureMetadataHeartBeatModuleDescriptor); Assert.NotNull(azureMetadataHeartBeatModuleDescriptor);
var quickPulseModuleDescriptor = services.FirstOrDefault<ServiceDescriptor>(t => t.ImplementationType == typeof(QuickPulseTelemetryModule)); var quickPulseModuleDescriptor = modules.OfType<QuickPulseTelemetryModule>().Single();
Assert.NotNull(quickPulseModuleDescriptor); Assert.NotNull(quickPulseModuleDescriptor);
} }
@ -557,6 +589,113 @@ namespace Microsoft.Extensions.DependencyInjection.Test
} }
#endif #endif
[Fact]
public static void UserCanDisablePerfCollectorModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnablePerformanceCounterCollectionModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<PerformanceCollectorModule>());
}
#if NETCOREAPP2_0
[Fact]
public static void UserCanDisableEventCounterCollectorModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableEventCounterCollectionModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<EventCounterCollectionModule>());
}
#endif
[Fact]
public static void UserCanDisableRequestCounterCollectorModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableRequestTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<RequestTrackingTelemetryModule>());
}
[Fact]
public static void UserCanDisableDependencyCollectorModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableDependencyTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<DependencyTrackingTelemetryModule>());
}
[Fact]
public static void UserCanDisableQuickPulseCollectorModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableQuickPulseMetricStream = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<QuickPulseTelemetryModule>());
}
[Fact]
public static void UserCanDisableAppServiceHeartbeatModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableAppServicesHeartbeatTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<AppServicesHeartbeatTelemetryModule>());
}
[Fact]
public static void UserCanDisableAzureInstanceMetadataModule()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
var aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableAzureInstanceMetadataTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
IServiceProvider serviceProvider = services.BuildServiceProvider();
var modules = serviceProvider.GetServices<ITelemetryModule>();
Assert.NotNull(modules);
Assert.Empty(modules.OfType<AzureInstanceMetadataTelemetryModule>());
}
[Fact] [Fact]
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesDependencyCollectorWithDefaultValues() public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesDependencyCollectorWithDefaultValues()
{ {

Просмотреть файл

@ -12,6 +12,7 @@
using System.Linq; using System.Linq;
using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
public class RequestCorrelationTests : TelemetryTestsBase public class RequestCorrelationTests : TelemetryTestsBase
{ {
@ -27,9 +28,10 @@
{ {
return builder.ConfigureServices(services => return builder.ConfigureServices(services =>
{ {
services.AddApplicationInsightsTelemetry(); var aiOptions = new ApplicationInsightsServiceOptions();
// disable Dependency tracking (i.e. header injection) // disable Dependency tracking (i.e. header injection)
services.Remove(services.FirstOrDefault(sd => sd.ImplementationType == typeof(DependencyTrackingTelemetryModule))); aiOptions.EnableDependencyTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
}); });
} }
@ -65,9 +67,10 @@
{ {
return builder.ConfigureServices(services => return builder.ConfigureServices(services =>
{ {
services.AddApplicationInsightsTelemetry(); var aiOptions = new ApplicationInsightsServiceOptions();
// disable Dependency tracking (i.e. header injection) // disable Dependency tracking (i.e. header injection)
services.Remove(services.FirstOrDefault(sd => sd.ImplementationType == typeof(DependencyTrackingTelemetryModule))); aiOptions.EnableDependencyTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
}); });
} }
@ -105,9 +108,10 @@
{ {
return builder.ConfigureServices(services => return builder.ConfigureServices(services =>
{ {
services.AddApplicationInsightsTelemetry(); var aiOptions = new ApplicationInsightsServiceOptions();
// disable Dependency tracking (i.e. header injection) // disable Dependency tracking (i.e. header injection)
services.Remove(services.FirstOrDefault(sd => sd.ImplementationType == typeof(DependencyTrackingTelemetryModule))); aiOptions.EnableDependencyTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
}); });
} }
@ -146,9 +150,10 @@
{ {
return builder.ConfigureServices(services => return builder.ConfigureServices(services =>
{ {
services.AddApplicationInsightsTelemetry(); var aiOptions = new ApplicationInsightsServiceOptions();
// disable Dependency tracking (i.e. header injection) // disable Dependency tracking (i.e. header injection)
services.Remove(services.FirstOrDefault(sd => sd.ImplementationType == typeof(DependencyTrackingTelemetryModule))); aiOptions.EnableDependencyTrackingTelemetryModule = false;
services.AddApplicationInsightsTelemetry(aiOptions);
}); });
} }