From 715bf0abfb7912532fafb68d6d3eb6b41bafdc2f Mon Sep 17 00:00:00 2001 From: abaranch Date: Mon, 26 Sep 2016 15:13:45 -0700 Subject: [PATCH 1/6] Read iKey from environment variable (APPINSIGHTS_INSTRUMENTATIONKEY) --- CHANGELOG.md | 4 ++ .../Shared/TelemetryClientTest.cs | 39 +++++++++++++++++++ .../Platform/PlatformImplementation.cs | 5 +++ .../Platform/PlatformImplementation.cs | 5 +++ .../Extensibility/Implementation/IPlatform.cs | 3 ++ src/Core/Managed/Shared/TelemetryClient.cs | 7 ++++ 6 files changed, 63 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5005f58f88..12e9b3ccb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This changelog will be used to generate documentation on [release notes page](http://azure.microsoft.com/en-us/documentation/articles/app-insights-release-notes-dotnet/). +## Version 2.2.0-beta3 + +- Read InstrumentationKey from environment variable APPINSIGHTS_INSTRUMENTATIONKEY if it is was not provided inline or though configuration (not available in PCL version of SDK). + ## Version 2.2.0-beta2 - InMemoryChannel has a new override for Flush method that accepts timeout. diff --git a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs index 87eef7ef62..bd30cba604 100644 --- a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs +++ b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs @@ -446,6 +446,25 @@ public void TrackUsesInstrumentationKeyFromConfigurationWhenTheInstrumenationKey Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); } + [TestMethod] + public void TrackUsesInstrumentationKeyFromEnvironmentVariableWhenTheInstrumenationKeyIsEmptyInConfiguration() + { + ITelemetry sentTelemetry = null; + var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; + var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; + var client = new TelemetryClient(configuration); + var observe = client.Context.InstrumentationKey; + + string expectedKey = Guid.NewGuid().ToString(); + + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", expectedKey); + + Assert.DoesNotThrow(() => client.TrackTrace("Test Message")); + Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); + + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); + } + [TestMethod] public void TrackDoesNotInitializeInstrumentationKeyWhenItWasSetExplicitly() { @@ -459,6 +478,26 @@ public void TrackDoesNotInitializeInstrumentationKeyWhenItWasSetExplicitly() Assert.Equal(expectedKey, client.Context.InstrumentationKey); } + [TestMethod] + public void TrackDoesNotUsesInstrumentationKeyFromEnvironmentVariableWhenTheInstrumenationKeyIsPresentInConfiguration() + { + ITelemetry sentTelemetry = null; + var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; + var configuration = new TelemetryConfiguration { TelemetryChannel = channel}; + var client = new TelemetryClient(configuration); + var observe = client.Context.InstrumentationKey; + + string expectedKey = Guid.NewGuid().ToString(); + configuration.InstrumentationKey = expectedKey; + + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", Guid.NewGuid().ToString()); + + Assert.DoesNotThrow(() => client.TrackTrace("Test Message")); + Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); + + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); + } + [TestMethod] public void TrackDoesNotSendDataWhenTelemetryIsDisabled() { diff --git a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs index 5bf52f4ace..51bf722997 100644 --- a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs +++ b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs @@ -64,5 +64,10 @@ public IDebugOutput GetDebugOutput() { return this.debugOutput ?? (this.debugOutput = new TelemetryDebugWriter()); } + + public string GetEnvironmentVariable(string name) + { + return Environment.GetEnvironmentVariable(name); + } } } diff --git a/src/Core/Managed/PCL/Extensibility/Implementation/Platform/PlatformImplementation.cs b/src/Core/Managed/PCL/Extensibility/Implementation/Platform/PlatformImplementation.cs index eeb65a584f..14b871a41e 100644 --- a/src/Core/Managed/PCL/Extensibility/Implementation/Platform/PlatformImplementation.cs +++ b/src/Core/Managed/PCL/Extensibility/Implementation/Platform/PlatformImplementation.cs @@ -35,5 +35,10 @@ public IDebugOutput GetDebugOutput() return this.debugOutput; } + + public string GetEnvironmentVariable(string name) + { + return null; + } } } diff --git a/src/Core/Managed/Shared/Extensibility/Implementation/IPlatform.cs b/src/Core/Managed/Shared/Extensibility/Implementation/IPlatform.cs index 82363f14b8..83b5cdb640 100644 --- a/src/Core/Managed/Shared/Extensibility/Implementation/IPlatform.cs +++ b/src/Core/Managed/Shared/Extensibility/Implementation/IPlatform.cs @@ -23,5 +23,8 @@ internal interface IPlatform /// Returns the platform specific Debugger writer to the VS output console. /// IDebugOutput GetDebugOutput(); + + // Read environment variable. + string GetEnvironmentVariable(string name); } } diff --git a/src/Core/Managed/Shared/TelemetryClient.cs b/src/Core/Managed/Shared/TelemetryClient.cs index 660dc0e83e..2688919ba4 100644 --- a/src/Core/Managed/Shared/TelemetryClient.cs +++ b/src/Core/Managed/Shared/TelemetryClient.cs @@ -11,6 +11,7 @@ using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; + using Microsoft.ApplicationInsights.Extensibility.Implementation.Platform; using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; /// @@ -19,6 +20,7 @@ public sealed class TelemetryClient { private const string VersionPrefix = "dotnet:"; + private const string InstrumentationKeyWebSitesEnvironmentVariable = "APPINSIGHTS_INSTRUMENTATIONKEY"; private readonly TelemetryConfiguration configuration; private TelemetryContext context; @@ -356,6 +358,11 @@ public void Initialize(ITelemetry telemetry) if (string.IsNullOrEmpty(instrumentationKey)) { instrumentationKey = this.configuration.InstrumentationKey; + + if (string.IsNullOrEmpty(instrumentationKey)) + { + instrumentationKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable); + } } var telemetryWithProperties = telemetry as ISupportProperties; From 9405d44a5b0f8dbfe5c6bdd094ced11e6efb621c Mon Sep 17 00:00:00 2001 From: abaranch Date: Mon, 26 Sep 2016 16:11:01 -0700 Subject: [PATCH 2/6] Fix build --- Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs b/Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs index 4bbe93a6d1..eb8b07fb71 100644 --- a/Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs +++ b/Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs @@ -23,5 +23,10 @@ public IDebugOutput GetDebugOutput() { return this.OnGetDebugOutput(); } + + public string GetEnvironmentVariable(string name) + { + return Environment.GetEnvironmentVariable(name); + } } } From f1ad891581e046062c04d2d62c78781715402128 Mon Sep 17 00:00:00 2001 From: abaranch Date: Mon, 26 Sep 2016 16:19:50 -0700 Subject: [PATCH 3/6] Misprint --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12e9b3ccb8..30e3b9f1cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This changelog will be used to generate documentation on [release notes page](ht ## Version 2.2.0-beta3 -- Read InstrumentationKey from environment variable APPINSIGHTS_INSTRUMENTATIONKEY if it is was not provided inline or though configuration (not available in PCL version of SDK). +- Read InstrumentationKey from environment variable APPINSIGHTS_INSTRUMENTATIONKEY if it is was not provided inline or through configuration (not available in PCL version of SDK). ## Version 2.2.0-beta2 From ac35f0f431fd18b7cc579c277365f6bfe389a69f Mon Sep 17 00:00:00 2001 From: abaranch Date: Mon, 26 Sep 2016 17:17:23 -0700 Subject: [PATCH 4/6] Read all environment variables first to improve performance --- .../Platform/PlatformImplementation.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs index 51bf722997..bb3caf5fac 100644 --- a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs +++ b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs @@ -1,8 +1,10 @@ namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Platform { using System; + using System.Collections; using System.IO; using System.Security; + using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; @@ -12,6 +14,8 @@ /// internal class PlatformImplementation : IPlatform { + private readonly IDictionary environmentVariables = Environment.GetEnvironmentVariables(); + private IDebugOutput debugOutput = null; /// @@ -67,7 +71,20 @@ public IDebugOutput GetDebugOutput() public string GetEnvironmentVariable(string name) { - return Environment.GetEnvironmentVariable(name); + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentNullException(); + } + + object resultObj = this.environmentVariables[name]; + if (resultObj != null) + { + return resultObj.ToString(); + } + else + { + return null; + } } } } From 75cd73511524a34f884f134a68f64ce37147de15 Mon Sep 17 00:00:00 2001 From: abaranch Date: Tue, 27 Sep 2016 13:15:48 -0700 Subject: [PATCH 5/6] Get priority to iKey set via env. var. --- CHANGELOG.md | 2 +- .../Shared/TelemetryClientTest.cs | 38 ++++++++++++++----- .../Platform/PlatformImplementation.cs | 9 +---- src/Core/Managed/Shared/TelemetryClient.cs | 6 +-- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 482df806f6..535f2ae316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This changelog will be used to generate documentation on [release notes page](ht ## Version 2.2.0-beta3 -- Read InstrumentationKey from environment variable APPINSIGHTS_INSTRUMENTATIONKEY if it is was not provided inline or through configuration (not available in PCL version of SDK). +- Read InstrumentationKey from environment variable APPINSIGHTS_INSTRUMENTATIONKEY if it is was not provided inline. If provided it overrides what is set though configuration file. (Feature is not available in PCL version of SDK). - Context properties `NetworkType`, `ScreenResolution` and `Language` marked as obsolete. Please use custom properties to report network type, screen resolution and language. Values stored in these properties will be send as custom properties. - Dependency type was updated to reflect the latest developments in Applicaiton Insights Applicaiton Map feature. You can set a new field - `Target`. `CommandName` was renamed to `Data` for consistancy with the Application Analytics schema. `DependencyKind` will never be send any more and will not be set to "Other" by default. Also there are two more constructors for `DependencyTelemetry` item. - Type `SessionStateTelemetry` was marked obsolete. Use `IsFirst` flag in `SessionContext` to indicate that the session is just started. diff --git a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs index e78d2b1c08..50193b5a84 100644 --- a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs +++ b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs @@ -417,7 +417,7 @@ public void TrackMethodIsInvisibleThroughIntelliSenseSoThatCustomersDontGetConfu } [TestMethod] - public void TrackMethodDontThrowsWhenInstrumentationKeyIsEmptyAndNotSendingTheTelemetryItem() + public void TrackMethodDoesNotThrowWhenInstrumentationKeyIsEmptyAndNotSendingTheTelemetryItem() { var channel = new StubTelemetryChannel { ThrowError = true }; TelemetryConfiguration.Active = new TelemetryConfiguration @@ -430,40 +430,58 @@ public void TrackMethodDontThrowsWhenInstrumentationKeyIsEmptyAndNotSendingTheTe } [TestMethod] - public void TrackUsesInstrumentationKeyFromConfigurationWhenTheInstrumenationKeyIsEmpty() + public void TrackUsesInstrumentationKeyIfSetInCodeFirst() { ITelemetry sentTelemetry = null; var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; var client = new TelemetryClient(configuration); - var observe = client.Context.InstrumentationKey; - + string expectedKey = Guid.NewGuid().ToString(); - configuration.InstrumentationKey = expectedKey; + client.Context.InstrumentationKey = expectedKey; // Set in code + configuration.InstrumentationKey = Guid.NewGuid().ToString(); // Set in config + 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); } [TestMethod] - public void TrackUsesInstrumentationKeyFromEnvironmentVariableWhenTheInstrumenationKeyIsEmptyInConfiguration() + public void TrackUsesInstrumentationKeyFromEnvironmentIfEmptyInCode() { ITelemetry sentTelemetry = null; var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; var configuration = new TelemetryConfiguration { TelemetryChannel = channel }; var client = new TelemetryClient(configuration); - var observe = client.Context.InstrumentationKey; string expectedKey = Guid.NewGuid().ToString(); - - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", expectedKey); - + configuration.InstrumentationKey = Guid.NewGuid().ToString(); // Set in config + 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); } + [TestMethod] + public void TrackUsesInstrumentationKeyFromConfigIfEnvironmentVariableIsEmpty() + { + 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(); + configuration.InstrumentationKey = expectedKey; // Set in config + Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); // Not set via env. variable + Assert.DoesNotThrow(() => client.TrackTrace("Test Message")); + + Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); + } + [TestMethod] public void TrackDoesNotInitializeInstrumentationKeyWhenItWasSetExplicitly() { diff --git a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs index bb3caf5fac..4f3c0ba111 100644 --- a/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs +++ b/src/Core/Managed/Net40/Extensibility/Implementation/Platform/PlatformImplementation.cs @@ -77,14 +77,7 @@ public string GetEnvironmentVariable(string name) } object resultObj = this.environmentVariables[name]; - if (resultObj != null) - { - return resultObj.ToString(); - } - else - { - return null; - } + return resultObj != null ? resultObj.ToString() : null; } } } diff --git a/src/Core/Managed/Shared/TelemetryClient.cs b/src/Core/Managed/Shared/TelemetryClient.cs index 5ec6b12660..d9f3e0a983 100644 --- a/src/Core/Managed/Shared/TelemetryClient.cs +++ b/src/Core/Managed/Shared/TelemetryClient.cs @@ -372,11 +372,11 @@ public void Initialize(ITelemetry telemetry) if (string.IsNullOrEmpty(instrumentationKey)) { - instrumentationKey = this.configuration.InstrumentationKey; - + instrumentationKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable); + if (string.IsNullOrEmpty(instrumentationKey)) { - instrumentationKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable); + instrumentationKey = this.configuration.InstrumentationKey; } } From 73933ae0fb86c1a4810ba5cc62e7c88cb4d7cdca Mon Sep 17 00:00:00 2001 From: abaranch Date: Tue, 27 Sep 2016 13:45:15 -0700 Subject: [PATCH 6/6] Remove obsolete test --- .../Shared/TelemetryClientTest.cs | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs index 50193b5a84..3e2f1fdea9 100644 --- a/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs +++ b/Test/CoreSDK.Test/Shared/TelemetryClientTest.cs @@ -483,9 +483,13 @@ public void TrackUsesInstrumentationKeyFromConfigIfEnvironmentVariableIsEmpty() } [TestMethod] - public void TrackDoesNotInitializeInstrumentationKeyWhenItWasSetExplicitly() + public void TrackDoesNotInitializeInstrumentationKeyFromConfigWhenItWasSetExplicitly() { - var configuration = new TelemetryConfiguration { TelemetryChannel = new StubTelemetryChannel(), InstrumentationKey = Guid.NewGuid().ToString() }; + var configuration = new TelemetryConfiguration + { + TelemetryChannel = new StubTelemetryChannel(), + InstrumentationKey = Guid.NewGuid().ToString() + }; var client = new TelemetryClient(configuration); var expectedKey = Guid.NewGuid().ToString(); @@ -495,26 +499,6 @@ public void TrackDoesNotInitializeInstrumentationKeyWhenItWasSetExplicitly() Assert.Equal(expectedKey, client.Context.InstrumentationKey); } - [TestMethod] - public void TrackDoesNotUsesInstrumentationKeyFromEnvironmentVariableWhenTheInstrumenationKeyIsPresentInConfiguration() - { - ITelemetry sentTelemetry = null; - var channel = new StubTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; - var configuration = new TelemetryConfiguration { TelemetryChannel = channel}; - var client = new TelemetryClient(configuration); - var observe = client.Context.InstrumentationKey; - - string expectedKey = Guid.NewGuid().ToString(); - configuration.InstrumentationKey = expectedKey; - - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", Guid.NewGuid().ToString()); - - Assert.DoesNotThrow(() => client.TrackTrace("Test Message")); - Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey); - - Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null); - } - [TestMethod] public void TrackDoesNotSendDataWhenTelemetryIsDisabled() {