Skip to content

Commit

Permalink
Adding EnableSqlCommandTextInstrumentation to ApplicationInsightsLogg…
Browse files Browse the repository at this point in the history
…erOptions
  • Loading branch information
soninaren committed Oct 18, 2020
1 parent b243ac0 commit dc92938
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public TimeSpan QuickPulseInitializationDelay {
/// </summary>
public bool EnableDependencyTracking { get; set; } = true;


public bool EnableSqlCommandTextInstrumentation { get; set; } = false;

/// <summary>
/// Gets or sets HTTP request collection options.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
{
var options = provider.GetService<IOptions<ApplicationInsightsLoggerOptions>>().Value;

DependencyTrackingTelemetryModule dependencyCollector = null;
if (options.EnableDependencyTracking)
{
var dependencyCollector = new DependencyTrackingTelemetryModule();
dependencyCollector = new DependencyTrackingTelemetryModule();
var excludedDomains = dependencyCollector.ExcludeComponentCorrelationHttpHeadersOnDomains;
excludedDomains.Add("core.windows.net");
excludedDomains.Add("core.chinacloudapi.cn");
Expand All @@ -122,11 +123,15 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
var includedActivities = dependencyCollector.IncludeDiagnosticSourceActivities;
includedActivities.Add("Microsoft.Azure.ServiceBus");
includedActivities.Add("Microsoft.Azure.EventHubs");
}

return dependencyCollector;
if (options.EnableSqlCommandTextInstrumentation)
{
dependencyCollector = dependencyCollector ?? new DependencyTrackingTelemetryModule();
dependencyCollector.EnableSqlCommandTextInstrumentation = options.EnableSqlCommandTextInstrumentation;
}

return NullTelemetryModule.Instance;
return dependencyCollector as ITelemetryModule ?? NullTelemetryModule.Instance;
});

services.AddSingleton<ITelemetryModule>(provider =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,48 @@ public void DependencyInjectionConfiguration_DisablesDependencyTracking()
}
}

[Fact]
public void DependencyInjectionConfiguration_EnableSqlCommandTextInstrumentation()
{
using (var host = new HostBuilder()
.ConfigureLogging(b =>
{
b.AddApplicationInsights(o =>
{
o.InstrumentationKey = "KI";
o.EnableSqlCommandTextInstrumentation = true;
});
})
.Build())
{
var modules = host.Services.GetServices<ITelemetryModule>();
var matchingModules = modules.Where(m => m is DependencyTrackingTelemetryModule);
Assert.True(matchingModules.Count() == 1);

var module = matchingModules.First() as DependencyTrackingTelemetryModule;
Assert.True(module.EnableSqlCommandTextInstrumentation);
}
}

[Fact]
public void DependencyInjectionConfiguration_SqlCommandTextInstrumentation_DisabledByDefault()
{
using (var host = new HostBuilder()
.ConfigureLogging(b =>
{
b.AddApplicationInsights(o =>
{
o.InstrumentationKey = "KI";
o.EnableDependencyTracking = false;
});
})
.Build())
{
var modules = host.Services.GetServices<ITelemetryModule>();
Assert.True(modules.Count(m => m is DependencyTrackingTelemetryModule) == 0);
}
}

[Fact]
public void DependencyInjectionConfiguration_DisablesQuickPulseTelemetry()
{
Expand Down

0 comments on commit dc92938

Please sign in to comment.