Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding EnableSqlCommandTextInstrumentation to ApplicationInsightsLoggerOptions #2608

Merged
merged 4 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public string QuickPulseAuthenticationApiKey
/// can result in decreased application startup time. Default value is 15 seconds.
/// </summary>
[Obsolete("Use LiveMetricsInitializationDelay instead.")]
public TimeSpan QuickPulseInitializationDelay {
public TimeSpan QuickPulseInitializationDelay
{
get
{
return LiveMetricsInitializationDelay;
Expand Down Expand Up @@ -105,6 +106,8 @@ public TimeSpan QuickPulseInitializationDelay {
/// </summary>
public bool EnableDependencyTracking { get; set; } = true;

public DependencyTrackingOptions DependencyTrackingOptions { get; set; }
soninaren marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets or sets HTTP request collection options.
/// </summary>
Expand Down Expand Up @@ -159,6 +162,18 @@ public string Format()
{ nameof(HttpAutoCollectionOptions.EnableResponseHeaderInjection), HttpAutoCollectionOptions.EnableResponseHeaderInjection }
};

JObject dependencyTrackingOptions = new JObject
soninaren marked this conversation as resolved.
Show resolved Hide resolved
{
{ nameof(DependencyTrackingOptions.DisableRuntimeInstrumentation), DependencyTrackingOptions.DisableRuntimeInstrumentation },
{ nameof(DependencyTrackingOptions.DisableDiagnosticSourceInstrumentation), DependencyTrackingOptions.DisableDiagnosticSourceInstrumentation},
{ nameof(DependencyTrackingOptions.EnableLegacyCorrelationHeadersInjection), DependencyTrackingOptions.EnableLegacyCorrelationHeadersInjection},
{ nameof(DependencyTrackingOptions.EnableRequestIdHeaderInjectionInW3CMode), DependencyTrackingOptions.EnableRequestIdHeaderInjectionInW3CMode},
{ nameof(DependencyTrackingOptions.EnableSqlCommandTextInstrumentation), DependencyTrackingOptions.EnableSqlCommandTextInstrumentation},
{ nameof(DependencyTrackingOptions.SetComponentCorrelationHttpHeaders), DependencyTrackingOptions.SetComponentCorrelationHttpHeaders},
{ nameof(DependencyTrackingOptions.EnableAzureSdkTelemetryListener), DependencyTrackingOptions.EnableAzureSdkTelemetryListener}
};


JObject options = new JObject
{
{ nameof(SamplingSettings), sampling },
Expand All @@ -169,7 +184,8 @@ public string Format()
{ nameof(HttpAutoCollectionOptions), httpOptions },
{ nameof(LiveMetricsInitializationDelay), LiveMetricsInitializationDelay },
{ nameof(EnableLiveMetrics), EnableLiveMetrics },
{ nameof(EnableDependencyTracking), EnableDependencyTracking }
{ nameof(EnableDependencyTracking), EnableDependencyTracking },
{ nameof(DependencyTrackingOptions), dependencyTrackingOptions }
};

return options.ToString(Formatting.Indented);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace Microsoft.Azure.WebJobs.Logging.ApplicationInsights
{
public class DependencyTrackingOptions
soninaren marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Gets or sets a value indicating whether to disable runtime instrumentation.
/// </summary>
public bool DisableRuntimeInstrumentation { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to disable Http Desktop DiagnosticSource instrumentation.
/// </summary>
public bool DisableDiagnosticSourceInstrumentation { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to enable legacy (x-ms*) correlation headers injection.
/// </summary>
public bool EnableLegacyCorrelationHeadersInjection { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to enable Request-Id correlation headers injection.
/// </summary>
public bool EnableRequestIdHeaderInjectionInW3CMode { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to track the SQL command text in SQL
/// dependencies.
/// </summary>
public bool EnableSqlCommandTextInstrumentation { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the correlation headers would be set
/// on outgoing http requests.
/// </summary>
public bool SetComponentCorrelationHttpHeaders { get; set; }

/// <summary>
/// Gets or sets a value indicating whether telemetry would be produced for Azure
/// SDK methods calls and requests.
/// </summary>
public bool EnableAzureSdkTelemetryListener { get; set; }
}
}
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 @@ -123,6 +124,17 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
includedActivities.Add("Microsoft.Azure.ServiceBus");
includedActivities.Add("Microsoft.Azure.EventHubs");

if (options.DependencyTrackingOptions != null)
{
dependencyCollector.DisableRuntimeInstrumentation = options.DependencyTrackingOptions.DisableRuntimeInstrumentation;
dependencyCollector.DisableDiagnosticSourceInstrumentation = options.DependencyTrackingOptions.DisableDiagnosticSourceInstrumentation;
dependencyCollector.EnableLegacyCorrelationHeadersInjection = options.DependencyTrackingOptions.EnableLegacyCorrelationHeadersInjection;
dependencyCollector.EnableRequestIdHeaderInjectionInW3CMode = options.DependencyTrackingOptions.EnableRequestIdHeaderInjectionInW3CMode;
dependencyCollector.EnableSqlCommandTextInstrumentation = options.DependencyTrackingOptions.EnableSqlCommandTextInstrumentation;
dependencyCollector.SetComponentCorrelationHttpHeaders = options.DependencyTrackingOptions.SetComponentCorrelationHttpHeaders;
dependencyCollector.EnableAzureSdkTelemetryListener = options.DependencyTrackingOptions.EnableAzureSdkTelemetryListener;
}

return dependencyCollector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void DependencyInjectionConfiguration_NoClientIpInitializerWithoutContext
public void DependencyInjectionConfiguration_NoClientIpInitializerWithoutExtendedHttpOptions()
{
using (var host = new HostBuilder()
.ConfigureServices(b =>
.ConfigureServices(b =>
b.AddHttpContextAccessor())
.ConfigureLogging(b =>
{
Expand All @@ -296,7 +296,7 @@ public void DependencyInjectionConfiguration_NoClientIpInitializerWithoutExtende
public void DependencyInjectionConfiguration_ConfiguresClientIpInitializer()
{
using (var host = new HostBuilder()
.ConfigureServices(b =>
.ConfigureServices(b =>
b.AddHttpContextAccessor())
.ConfigureLogging(b =>
{
Expand All @@ -308,7 +308,7 @@ public void DependencyInjectionConfiguration_ConfiguresClientIpInitializer()
.Build())
{
var config = host.Services.GetServices<TelemetryConfiguration>().Single();

Assert.Contains(config.TelemetryInitializers, ti => ti is ClientIpHeaderTelemetryInitializer);
}
}
Expand Down Expand Up @@ -404,6 +404,50 @@ public void DependencyInjectionConfiguration_DisablesDependencyTracking()
}
}

[Fact]
public void DependencyInjectionConfiguration_EnableSqlCommandTextInstrumentation()
{
using (var host = new HostBuilder()
.ConfigureLogging(b =>
{
b.AddApplicationInsights(o =>
{
o.InstrumentationKey = "KI";
o.EnableDependencyTracking = true;
o.DependencyTrackingOptions = new DependencyTrackingOptions() { 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;
o.DependencyTrackingOptions = new DependencyTrackingOptions() { EnableSqlCommandTextInstrumentation = true };
});
})
.Build())
{
var modules = host.Services.GetServices<ITelemetryModule>();
Assert.True(modules.Count(m => m is DependencyTrackingTelemetryModule) == 0);
}
}

[Fact]
public void DependencyInjectionConfiguration_DisablesQuickPulseTelemetry()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,16 @@ public void ApplicationInsightsLoggerOptions_Format()
EnableResponseHeaderInjection = false,
EnableW3CDistributedTracing = false,
EnableHttpTriggerExtendedInfoCollection = true
},
DependencyTrackingOptions = new DependencyTrackingOptions()
{
DisableDiagnosticSourceInstrumentation = true,
DisableRuntimeInstrumentation = true,
EnableSqlCommandTextInstrumentation = true,
EnableAzureSdkTelemetryListener = true,
EnableLegacyCorrelationHeadersInjection = true,
EnableRequestIdHeaderInjectionInW3CMode = true,
SetComponentCorrelationHttpHeaders = true
}
};

Expand Down Expand Up @@ -726,6 +736,14 @@ public void ApplicationInsightsLoggerOptions_Format()
Assert.Equal(options.SnapshotConfiguration.SnapshotsPerTenMinutesLimit, deserializedOptions.SnapshotConfiguration.SnapshotsPerTenMinutesLimit);
Assert.Equal(options.SnapshotConfiguration.TempFolder, deserializedOptions.SnapshotConfiguration.TempFolder);
Assert.Equal(options.SnapshotConfiguration.ThresholdForSnapshotting, deserializedOptions.SnapshotConfiguration.ThresholdForSnapshotting);

Assert.Equal(options.DependencyTrackingOptions.SetComponentCorrelationHttpHeaders, deserializedOptions.DependencyTrackingOptions.SetComponentCorrelationHttpHeaders);
Assert.Equal(options.DependencyTrackingOptions.DisableDiagnosticSourceInstrumentation, deserializedOptions.DependencyTrackingOptions.DisableDiagnosticSourceInstrumentation);
Assert.Equal(options.DependencyTrackingOptions.DisableRuntimeInstrumentation, deserializedOptions.DependencyTrackingOptions.DisableRuntimeInstrumentation);
Assert.Equal(options.DependencyTrackingOptions.EnableAzureSdkTelemetryListener, deserializedOptions.DependencyTrackingOptions.EnableAzureSdkTelemetryListener);
Assert.Equal(options.DependencyTrackingOptions.EnableLegacyCorrelationHeadersInjection, deserializedOptions.DependencyTrackingOptions.EnableLegacyCorrelationHeadersInjection);
Assert.Equal(options.DependencyTrackingOptions.EnableRequestIdHeaderInjectionInW3CMode, deserializedOptions.DependencyTrackingOptions.EnableRequestIdHeaderInjectionInW3CMode);
Assert.Equal(options.DependencyTrackingOptions.EnableSqlCommandTextInstrumentation, deserializedOptions.DependencyTrackingOptions.EnableSqlCommandTextInstrumentation);
}

[Fact]
Expand Down