Skip to content
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
9d47ff7
Merge pull request #1819 from microsoft/develop
rajkumar-rangaraj Apr 28, 2020
4ab6b71
Reads configuration from all defined sources from app along with all …
rajkumar-rangaraj Apr 28, 2020
c61a7ad
Added test cases and changed configuration binding.
rajkumar-rangaraj May 1, 2020
9548b0c
Merge branch 'develop' into rajrang/aspnetconfiguration
rajkumar-rangaraj May 1, 2020
6f26997
Modified test appSettings.json
rajkumar-rangaraj May 1, 2020
d9ed086
Added support to NET46 test case.
rajkumar-rangaraj May 3, 2020
4e41c7e
Modified test names
rajkumar-rangaraj May 4, 2020
b3c2ffb
Modified ChangeLog
rajkumar-rangaraj May 4, 2020
b9848bc
Merge branch 'develop' into rajrang/aspnetconfiguration
rajkumar-rangaraj May 4, 2020
916393b
Merge branch 'develop' into rajrang/aspnetconfiguration
May 4, 2020
433cf53
refactor static super class into abstract base class - tests pass
May 5, 2020
22203ae
refactor AddApplicationInsightsSettings - test pass
May 5, 2020
47a1c30
refactor super class and addApplicationInsightsTelemetry - tests pass
May 5, 2020
96171d8
move class - test pass
May 5, 2020
01d8877
rafactor usings
May 5, 2020
0f2a998
cleanup
May 5, 2020
2c87691
Remove TelemetryConfiguration.Active
May 6, 2020
72bdd3b
fixing internal reference
May 7, 2020
5c33a0a
fix module count
May 7, 2020
ebb85e7
fixing heartbeat initialization
May 8, 2020
8be254d
Merge branch 'develop' into rajrang/aspnetconfiguration
May 8, 2020
3e41354
merge from raj's branch
May 8, 2020
1661182
Merge branch 'tilee/refactor_tests+raj' into tilee/netcore_remove_act…
May 8, 2020
0a612fd
merge develop
Jun 2, 2020
a5a58de
fix merge conflict
Jun 2, 2020
691f80c
fix merge conflict
Jun 2, 2020
9eb1755
cleanup
Jun 2, 2020
a947aab
Merge branch 'develop' into tilee/netcore_active
Jun 3, 2020
2c3c446
merge develop
Jun 5, 2020
49b7628
fix merge conflict
Jun 5, 2020
394f4c0
cleanup
Jun 5, 2020
031a266
adding EnableDiagnosticsTelemetryModule
Jun 5, 2020
db4ef1d
Merge branch 'develop' into tilee/netcore_active
Jun 5, 2020
3791642
update public api
Jun 5, 2020
72b57fe
fix
Jun 5, 2020
6efc7aa
fix for test failure
Jun 9, 2020
e05f36f
Merge branch 'develop' into tilee/netcore_active
Jun 16, 2020
6a29604
Merge branch 'tilee/netcore_active' of https://github.com/microsoft/A…
Jun 17, 2020
3e30aa2
Merge branch 'develop' into tilee/netcore_active
Jun 17, 2020
5ec1092
Merge branch 'tilee/netcore_active' of https://github.com/microsoft/A…
Jun 17, 2020
4e85bd5
fix public api
Jun 17, 2020
95046a7
fix public api for real
Jun 17, 2020
47bdeb2
backwards compat for .Active and some tests
Jun 18, 2020
26085e5
cleanup
Jun 18, 2020
39196bc
fix compile error
Jun 18, 2020
1f2c30e
testing fix for tests
Jun 19, 2020
221838a
testing fix for GetEntryAssembly
Jun 19, 2020
e4ddf76
add new test for BackwardsCompat
Jun 19, 2020
0580086
tests
Jun 20, 2020
a74fc2d
cleanup
Jun 20, 2020
5c2d0b5
cleanup
Jun 22, 2020
622f576
fix for failing tests
Jun 23, 2020
8a512f7
cleanup the AddSingleton
Jun 24, 2020
b081d9d
pushing work in progress for Cijo
Jun 26, 2020
e34d467
incorporating Cijo's feedback.
Jun 30, 2020
4a07f2c
merge develop
Jun 30, 2020
9d594aa
cleanup
Jun 30, 2020
df6d2f7
rename EnableActiveTelemetryConfigurationSetup
Jun 30, 2020
7cc750b
Merge branch 'develop' into tilee/netcore_active
Jun 30, 2020
1f6c080
remove backcompat TC.Active from WorkerService
Jun 30, 2020
1c50885
fix enable settings
Jul 1, 2020
73beae3
Merge branch 'develop' into tilee/netcore_active
Jul 1, 2020
e19e30c
cleanup
Jul 1, 2020
a7506d2
adding tests for suggested changes and fixed compile issues
Jul 2, 2020
e040742
cleanup
Jul 2, 2020
4036ef6
Merge branch 'develop' into tilee/netcore_active
Jul 2, 2020
7c0f857
comment
Jul 2, 2020
7c4cd62
last tests
Jul 2, 2020
d61ba47
fix worker service test
Jul 2, 2020
739c266
cleanup
Jul 2, 2020
ebfa752
merge develop
Jul 2, 2020
f9d8b5e
revert changes to ApplicationNameProvider
Jul 2, 2020
4acda76
revert
Jul 2, 2020
a288c52
clean
Jul 2, 2020
8f0e7a5
code review comments
Jul 6, 2020
a600d03
Merge branch 'develop' into tilee/netcore_active
Jul 6, 2020
13e4c9c
changelog
Jul 6, 2020
aee2453
code review comments
Jul 7, 2020
182d3ea
fix comments
Jul 7, 2020
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
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.WindowsServer.AppServicesHeartbeatTelemetryModule.HeartbeatPropertyManager.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.IHeartbeatPropertyManager
Microsoft.ApplicationInsights.WindowsServer.AppServicesHeartbeatTelemetryModule.HeartbeatPropertyManager.set -> void
Microsoft.ApplicationInsights.WindowsServer.AzureInstanceMetadataTelemetryModule.HeartbeatPropertyManager.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.IHeartbeatPropertyManager
Microsoft.ApplicationInsights.WindowsServer.AzureInstanceMetadataTelemetryModule.HeartbeatPropertyManager.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.WindowsServer.AppServicesHeartbeatTelemetryModule.HeartbeatPropertyManager.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.IHeartbeatPropertyManager
Microsoft.ApplicationInsights.WindowsServer.AppServicesHeartbeatTelemetryModule.HeartbeatPropertyManager.set -> void
Microsoft.ApplicationInsights.WindowsServer.AzureInstanceMetadataTelemetryModule.HeartbeatPropertyManager.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.IHeartbeatPropertyManager
Microsoft.ApplicationInsights.WindowsServer.AzureInstanceMetadataTelemetryModule.HeartbeatPropertyManager.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.set -> void
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.set -> void
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.set -> void
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.set -> void
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.get -> bool
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableActiveTelemetryConfigurationSetup.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.get -> bool
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Options;

Expand All @@ -17,7 +19,8 @@ internal class TelemetryConfigurationOptions : IOptions<TelemetryConfiguration>
/// Initializes a new instance of the <see cref="TelemetryConfigurationOptions"/> class.
/// </summary>
/// <param name="configureOptions">Options to be configured.</param>
public TelemetryConfigurationOptions(IEnumerable<IConfigureOptions<TelemetryConfiguration>> configureOptions)
/// <param name="applicationInsightsServiceOptions">User defined serviceOptions.</param>
public TelemetryConfigurationOptions(IEnumerable<IConfigureOptions<TelemetryConfiguration>> configureOptions, IOptions<ApplicationInsightsServiceOptions> applicationInsightsServiceOptions)
{
this.Value = TelemetryConfiguration.CreateDefault();

Expand All @@ -27,18 +30,21 @@ public TelemetryConfigurationOptions(IEnumerable<IConfigureOptions<TelemetryConf
c.Configure(this.Value);
}

lock (LockObject)
if (applicationInsightsServiceOptions.Value.EnableActiveTelemetryConfigurationSetup)
{
// workaround for Microsoft/ApplicationInsights-dotnet#613
// as we expect some customers to use TelemetryConfiguration.Active together with dependency injection
// we make sure it has been set up, it must be done only once even if there are multiple Web Hosts in the process
if (!IsActiveConfigured(this.Value.InstrumentationKey))
lock (LockObject)
{
foreach (var c in configureOptionsArray)
// workaround for Microsoft/ApplicationInsights-dotnet#613
// as we expect some customers to use TelemetryConfiguration.Active together with dependency injection
// we make sure it has been set up, it must be done only once even if there are multiple Web Hosts in the process
if (!IsActiveConfigured(this.Value.InstrumentationKey))
{
foreach (var c in configureOptionsArray)
{
#pragma warning disable CS0618 // This must be maintained for backwards compatibility.
c.Configure(TelemetryConfiguration.Active);
c.Configure(TelemetryConfiguration.Active);
#pragma warning restore CS0618
}
}
}
}
Expand Down
27 changes: 26 additions & 1 deletion NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ internal static void AddTelemetryConfiguration(
}
}

/// <summary>
/// The AddSingleton method will not check if a class has already been added as an ImplementationType.
/// This extension method is to encapsulate those checks.
/// </summary>
/// <remarks>
/// Must check all three properties to avoid duplicates or null ref exceptions.
/// </remarks>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
internal static void AddSingletonIfNotExists<TService, TImplementation>(this IServiceCollection services)
where TService : class
where TImplementation : class, TService
{
if (!services.Any(o => o.ImplementationFactory == null && typeof(TImplementation).IsAssignableFrom(o.ImplementationType ?? o.ImplementationInstance.GetType())))
{
services.AddSingleton<TService, TImplementation>();
}
}

private static bool TryGetValue(this IConfiguration config, string primaryKey, out string value, string backupKey = null)
{
value = config[primaryKey];
Expand Down Expand Up @@ -316,9 +336,14 @@ private static void AddCommonInitializers(IServiceCollection services)

private static void AddCommonTelemetryModules(IServiceCollection services)
{
services.AddSingleton<ITelemetryModule, PerformanceCollectorModule>();
// Previously users were encouraged to manually add the DiagnosticsTelemetryModule.
services.AddSingletonIfNotExists<ITelemetryModule, DiagnosticsTelemetryModule>();

// These modules add properties to Heartbeat and expect the DiagnosticsTelemetryModule to be configured in DI.
services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>();
services.AddSingleton<ITelemetryModule, AzureInstanceMetadataTelemetryModule>();

services.AddSingleton<ITelemetryModule, PerformanceCollectorModule>();
services.AddSingleton<ITelemetryModule, QuickPulseTelemetryModule>();

AddAndConfigureDependencyTracking(services);
Expand Down
48 changes: 45 additions & 3 deletions NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace Microsoft.ApplicationInsights.WorkerService
/// </summary>
public class ApplicationInsightsServiceOptions
{
private bool enableAppServicesHeartbeatTelemetryModule = true;
private bool enableAzureInstanceMetadataTelemetryModule = true;
private bool enableHeartbeat = true;

/// <summary>
/// Gets or sets a value indicating whether QuickPulseTelemetryModule and QuickPulseTelemetryProcessor are registered with the configuration.
/// Setting EnableQuickPulseMetricStream to <value>false</value>, will disable the default quick pulse metric stream. Defaults to <value>true</value>.
Expand All @@ -27,14 +31,24 @@ public class ApplicationInsightsServiceOptions
/// <summary>
/// Gets or sets a value indicating whether AppServicesHeartbeatTelemetryModule should be enabled.
/// Defaults to <value>true</value>.
/// IMPORTANT: This setting will be disabled if <see cref="EnableDiagnosticsTelemetryModule"/> is set to false.
/// </summary>
public bool EnableAppServicesHeartbeatTelemetryModule { get; set; } = true;
public bool EnableAppServicesHeartbeatTelemetryModule
{
get => this.enableAppServicesHeartbeatTelemetryModule && this.EnableDiagnosticsTelemetryModule;
set => this.enableAppServicesHeartbeatTelemetryModule = value;
}

/// <summary>
/// Gets or sets a value indicating whether AzureInstanceMetadataTelemetryModule should be enabled.
/// Defaults to <value>true</value>.
/// IMPORTANT: This setting will be disabled if <see cref="EnableDiagnosticsTelemetryModule"/> is set to false.
/// </summary>
public bool EnableAzureInstanceMetadataTelemetryModule { get; set; } = true;
public bool EnableAzureInstanceMetadataTelemetryModule
{
get => this.enableAzureInstanceMetadataTelemetryModule && this.EnableDiagnosticsTelemetryModule;
set => this.enableAzureInstanceMetadataTelemetryModule = value;
}

/// <summary>
/// Gets or sets a value indicating whether DependencyTrackingTelemetryModule should be enabled.
Expand Down Expand Up @@ -88,8 +102,13 @@ public class ApplicationInsightsServiceOptions

/// <summary>
/// Gets or sets a value indicating whether heartbeats are enabled.
/// IMPORTANT: This setting will be disabled if <see cref="EnableDiagnosticsTelemetryModule"/> is set to false.
/// </summary>
public bool EnableHeartbeat { get; set; } = true;
public bool EnableHeartbeat
{
get => this.enableHeartbeat && this.EnableDiagnosticsTelemetryModule;
set => this.enableHeartbeat = value;
}

/// <summary>
/// Gets or sets a value indicating whether AutoCollectedMetricExtractors are added or not.
Expand All @@ -116,11 +135,31 @@ public class ApplicationInsightsServiceOptions
public bool EnableAuthenticationTrackingJavaScript { get; set; } = false;
#endif

/// <summary>
/// Gets or sets a value indicating whether the <see cref="Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.DiagnosticsTelemetryModule"/> should be enabled.
/// IMPORTANT: Disabling this will also disable the following settings:
/// <see cref="EnableHeartbeat"/>.
/// <see cref="EnableAzureInstanceMetadataTelemetryModule"/>.
/// <see cref="EnableAppServicesHeartbeatTelemetryModule"/>.
/// </summary>
public bool EnableDiagnosticsTelemetryModule { get; set; } = true;

/// <summary>
/// Gets <see cref="DependencyCollectionOptions"/> that allow to manage <see cref="DependencyTrackingTelemetryModule"/>.
/// </summary>
public DependencyCollectionOptions DependencyCollectionOptions { get; } = new DependencyCollectionOptions();

#if AI_ASPNETCORE_WEB
/// <summary>
/// Gets or sets a value indicating whether TelemetryConfiguration.Active should be initialized.
/// Former versions of this library had a dependency on this static instance.
/// This dependency has been removed and we no longer initialize this by default.
/// If users depended on this behavior you should enable this.
/// However, we recommend migrating away from using TelemetryConfiguration.Active in your projects.
/// </summary>
public bool EnableActiveTelemetryConfigurationSetup { get; set; } = false;
#endif

/// <summary>
/// Copy the properties from this <see cref="ApplicationInsightsServiceOptions"/> to a target instance.
/// </summary>
Expand Down Expand Up @@ -157,12 +196,15 @@ internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target)
target.EnableDependencyTrackingTelemetryModule = this.EnableDependencyTrackingTelemetryModule;
target.EnableAppServicesHeartbeatTelemetryModule = this.EnableAppServicesHeartbeatTelemetryModule;
target.EnableAzureInstanceMetadataTelemetryModule = this.EnableAzureInstanceMetadataTelemetryModule;
target.EnableDiagnosticsTelemetryModule = this.EnableDiagnosticsTelemetryModule;

#if NETSTANDARD2_0
target.EnableEventCounterCollectionModule = this.EnableEventCounterCollectionModule;
#endif
#if AI_ASPNETCORE_WEB
target.EnableAuthenticationTrackingJavaScript = this.EnableAuthenticationTrackingJavaScript;
target.EnableRequestTrackingTelemetryModule = this.EnableRequestTrackingTelemetryModule;
target.EnableActiveTelemetryConfigurationSetup = this.EnableActiveTelemetryConfigurationSetup;
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public void Configure(TelemetryConfiguration configuration)
configuration.TelemetryInitializers.Add(initializer);
}

// Find the DiagnosticsTelemetryModule, this is needed to initialize AzureInstanceMetadataTelemetryModule and AppServicesHeartbeatTelemetryModule.
DiagnosticsTelemetryModule diagModule =
(this.applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule && this.applicationInsightsServiceOptions.EnableHeartbeat)
? this.modules.OfType<DiagnosticsTelemetryModule>().FirstOrDefault()
: null;

// Checks ApplicationInsightsServiceOptions and disable TelemetryModules if explicitly disabled.
foreach (ITelemetryModule module in this.modules)
{
// If any of the modules are disabled explicitly using aioptions,
Expand All @@ -149,6 +156,15 @@ public void Configure(TelemetryConfiguration configuration)
// So this approach of adding all modules to DI, but selectively
// disable those modules which user has disabled is chosen.

if (module is DiagnosticsTelemetryModule)
{
if (!this.applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule)
{
DisposeIfDisposable(module);
continue;
}
}

// DependencyTrackingTelemetryModule
if (module is DependencyTrackingTelemetryModule)
{
Expand Down Expand Up @@ -194,23 +210,33 @@ public void Configure(TelemetryConfiguration configuration)
}

// AppServicesHeartbeatTelemetryModule
if (module is AppServicesHeartbeatTelemetryModule)
if (module is AppServicesHeartbeatTelemetryModule appServicesHeartbeatTelemetryModule)
{
if (!this.applicationInsightsServiceOptions.EnableAppServicesHeartbeatTelemetryModule)
{
DisposeIfDisposable(module);
continue;
}
else if (diagModule != null)
{
// diagModule is set to Null above if (applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule || this.applicationInsightsServiceOptions.EnableHeartbeat) == false.
appServicesHeartbeatTelemetryModule.HeartbeatPropertyManager = diagModule;
}
}

// AzureInstanceMetadataTelemetryModule
if (module is AzureInstanceMetadataTelemetryModule)
if (module is AzureInstanceMetadataTelemetryModule azureInstanceMetadataTelemetryModule)
{
if (!this.applicationInsightsServiceOptions.EnableAzureInstanceMetadataTelemetryModule)
{
DisposeIfDisposable(module);
continue;
}
else if (diagModule != null)
{
// diagModule is set to Null above if (applicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule || this.applicationInsightsServiceOptions.EnableHeartbeat) == false.
azureInstanceMetadataTelemetryModule.HeartbeatPropertyManager = diagModule;
}
}

// QuickPulseTelemetryModule
Expand All @@ -223,7 +249,14 @@ public void Configure(TelemetryConfiguration configuration)
}
}

module.Initialize(configuration);
try
{
module.Initialize(configuration);
}
catch (Exception ex)
{
throw new Exception($"Failed to initialize module '{module.GetType()}'", ex);
}
}

foreach (ITelemetryProcessor processor in configuration.TelemetryProcessors)
Expand Down Expand Up @@ -320,7 +353,7 @@ private void DisableHeartBeatIfConfigured()
// Disable heartbeat if user sets it (by default it is on)
if (!this.applicationInsightsServiceOptions.EnableHeartbeat)
{
foreach (var module in TelemetryModules.Instance.Modules)
foreach (var module in this.modules)
{
if (module is IHeartbeatPropertyManager hbeatMan)
{
Expand Down
Loading