Skip to content

Commit

Permalink
Reversed the priority of the environment variable vs. config file for…
Browse files Browse the repository at this point in the history
… iKey. Fixed and removed some unit tests.
  • Loading branch information
dnduffy committed Nov 30, 2016
1 parent d190e23 commit f96ff00
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
49 changes: 0 additions & 49 deletions Test/CoreSDK.Test/Shared/TelemetryClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/Core/Managed/Shared/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f96ff00

Please sign in to comment.