From f96ff00baee89ff34b8c2565017f76c9617e8504 Mon Sep 17 00:00:00 2001 From: David Duffy Date: Wed, 30 Nov 2016 13:40:32 -0800 Subject: [PATCH] Reversed the priority of the environment variable vs. config file for iKey. Fixed and removed some unit tests. --- .../TelemetryConfigurationFactoryTest.cs | 5 +- .../Shared/TelemetryClientTest.cs | 49 ------------------- .../TelemetryConfigurationFactory.cs | 8 +-- src/Core/Managed/Shared/TelemetryClient.cs | 8 --- 4 files changed, 6 insertions(+), 64 deletions(-) diff --git a/Test/CoreSDK.Test/Shared/Extensibility/TelemetryConfigurationFactoryTest.cs b/Test/CoreSDK.Test/Shared/Extensibility/TelemetryConfigurationFactoryTest.cs index d2a53dfeb0..cbf5067cfd 100644 --- a/Test/CoreSDK.Test/Shared/Extensibility/TelemetryConfigurationFactoryTest.cs +++ b/Test/CoreSDK.Test/Shared/Extensibility/TelemetryConfigurationFactoryTest.cs @@ -139,21 +139,20 @@ public void InitializeReadsInstrumentationKeyFromEnvironmentVariableIfNotSpecifi } [TestMethod] - public void InitializeDoesNotReadInstrumentationKeyFromEnvironmentVariableIfSpecifiedInConfig() + public void InitializeReadsInstrumentationKeyFromEnvironmentVariableEvenIfSpecifiedInConfig() { // ARRANGE var ikeyConfig = Guid.NewGuid().ToString(); var ikeyEnvironmentVariable = Guid.NewGuid().ToString(); Environment.SetEnvironmentVariable(EnvironmentVariableName, ikeyEnvironmentVariable); - TelemetryConfiguration configuration = new TelemetryConfiguration() { InstrumentationKey = ikeyConfig }; // ACT new TestableTelemetryConfigurationFactory().Initialize(configuration, null); // ASSERT - Assert.Equal(ikeyConfig, configuration.InstrumentationKey); + Assert.Equal(ikeyEnvironmentVariable, configuration.InstrumentationKey); } [TestMethod] diff --git a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs index 43b15b64d7..58b8ad2ea9 100644 --- a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs +++ b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs @@ -520,55 +520,6 @@ public void TrackUsesInstrumentationKeyFromClientContextIfSetInCodeFirst() PlatformSingleton.Current = null; } - [TestMethod] - public void TrackUsesInstrumentationKeyFromDefaultConfigInstanceIfSetInCodeFirst() - { - ClearActiveTelemetryConfiguration(); - PlatformSingleton.Current = new StubPlatform(); - string message = "Test Message"; - - ITelemetry sentTelemetry = null; - var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; - var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; - var client = new TelemetryClient(configuration); - - string oldActiveKey = TelemetryConfiguration.Active.InstrumentationKey; - string environmentKey = Guid.NewGuid().ToString(); - string expectedKey = Guid.NewGuid().ToString(); - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", environmentKey); // Set via the environment variable. - - TelemetryConfiguration.Active.InstrumentationKey = expectedKey; // Set in code method two, default config. - Assert.DoesNotThrow(() => client.TrackTrace(message)); - Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); - - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); - TelemetryConfiguration.Active.InstrumentationKey = oldActiveKey; // Restore state to not impact other tests. - - PlatformSingleton.Current = null; - } - - [TestMethod] - public void TrackUsesInstrumentationKeyFromEnvironmentIfEmptyInCode() - { - ClearActiveTelemetryConfiguration(); - PlatformSingleton.Current = new StubPlatform(); - - ITelemetry sentTelemetry = null; - var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; - var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; - var client = new TelemetryClient(configuration); - - string expectedKey = Guid.NewGuid().ToString(); - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", expectedKey); // Set via env. variable - Assert.DoesNotThrow(() => client.TrackTrace("Test Message")); - - Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); - - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); - - PlatformSingleton.Current = null; - } - [TestMethod] public void TrackUsesInstrumentationKeyFromConfigIfEnvironmentVariableIsEmpty() { diff --git a/src/Core/Managed/Net40/Extensibility/Implementation/TelemetryConfigurationFactory.cs b/src/Core/Managed/Net40/Extensibility/Implementation/TelemetryConfigurationFactory.cs index 808ae3637e..d4f3a80270 100644 --- a/src/Core/Managed/Net40/Extensibility/Implementation/TelemetryConfigurationFactory.cs +++ b/src/Core/Managed/Net40/Extensibility/Implementation/TelemetryConfigurationFactory.cs @@ -73,11 +73,11 @@ public virtual void Initialize(TelemetryConfiguration configuration, TelemetryMo } } - // If no instrumentation key is found, try to fall back to an environment variable (for blackfield scenario) - if (string.IsNullOrEmpty(configuration.InstrumentationKey)) + // If an environment variable exists with an instrumentation key then use it (instead) for the "blackfield" scenario. + string environmentIKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable); + if (!string.IsNullOrEmpty(environmentIKey)) { - configuration.InstrumentationKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable) - ?? string.Empty; + configuration.InstrumentationKey = environmentIKey; } // Creating the default channel if no channel configuration supplied diff --git a/src/Core/Managed/Shared/TelemetryClient.cs b/src/Core/Managed/Shared/TelemetryClient.cs index 250f844fa1..49251e426a 100644 --- a/src/Core/Managed/Shared/TelemetryClient.cs +++ b/src/Core/Managed/Shared/TelemetryClient.cs @@ -20,8 +20,6 @@ public sealed class TelemetryClient { private const string VersionPrefix = "dotnet:"; - private const string InstrumentationKeyWebSitesEnvironmentVariable = "APPINSIGHTS_INSTRUMENTATIONKEY"; - private readonly TelemetryConfiguration configuration; private TelemetryContext context; private string sdkVersion; @@ -379,13 +377,7 @@ public void Initialize(ITelemetry telemetry) if (string.IsNullOrEmpty(instrumentationKey)) { - // If no configuration override was passed to the constructor then the default configuration instance will have been used. - // If no iKey was present in the config then the environment variable will have been loaded into the default instance. instrumentationKey = this.configuration.InstrumentationKey; - if (string.IsNullOrEmpty(instrumentationKey) && this.configuration != TelemetryConfiguration.Active) - { - instrumentationKey = TelemetryConfiguration.Active.InstrumentationKey; - } } var telemetryWithProperties = telemetry as ISupportProperties;