Skip to content

Commit

Permalink
Merge pull request #379 from Microsoft/dnduffy/FixIKeySourcePriorityBug
Browse files Browse the repository at this point in the history
Changed the logic for loading the iKey from an environment variable to not override config or code settings.
  • Loading branch information
dnduffy authored Nov 30, 2016
2 parents ff03225 + f96ff00 commit 47925e5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 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
48 changes: 21 additions & 27 deletions Test/CoreSDK.Test/Shared/TelemetryClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public void TrackEventWillSendPropertiesIfProvidedInline()
Assert.Equal("yoyo", eventTelemetry.Properties["blah"]);
}

#endregion

#region Initialize

[TestMethod]
public void InitializeSetsDateTime()
{
Expand Down Expand Up @@ -492,43 +496,23 @@ public void ChannelIsInitializedInTrackWhenTelemetryConfigurationIsConstructedVi
}

[TestMethod]
public void TrackUsesInstrumentationKeyIfSetInCodeFirst()
public void TrackUsesInstrumentationKeyFromClientContextIfSetInCodeFirst()
{
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 environmentKey = Guid.NewGuid().ToString();
string expectedKey = Guid.NewGuid().ToString();
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);

PlatformSingleton.Current = null;
}

[TestMethod]
public void TrackUsesInstrumentationKeyFromEnvironmentIfEmptyInCode()
{
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();
configuration.InstrumentationKey = Guid.NewGuid().ToString(); // Set in config
Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", expectedKey); // Set via env. variable
Assert.DoesNotThrow(() => client.TrackTrace("Test Message"));
Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", environmentKey); // Set via the environment variable.

client.Context.InstrumentationKey = expectedKey;
Assert.DoesNotThrow(() => client.TrackTrace(message));
Assert.Equal(expectedKey, sentTelemetry.Context.InstrumentationKey);

Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null);
Expand Down Expand Up @@ -753,6 +737,7 @@ public void TrackAddsTimestampWhenMissing()
[TestMethod]
public void TrackWritesTelemetryToDebugOutputIfIKeyEmpty()
{
ClearActiveTelemetryConfiguration();
string actualMessage = null;
var debugOutput = new StubDebugOutput
{
Expand Down Expand Up @@ -810,6 +795,7 @@ public void TrackWritesTelemetryToDebugOutputIfIKeyNotEmpty()
[TestMethod]
public void TrackDoesNotWriteTelemetryToDebugOutputIfNotInDeveloperMode()
{
ClearActiveTelemetryConfiguration();
string actualMessage = null;
var debugOutput = new StubDebugOutput { OnWriteLine = message => actualMessage = message };
PlatformSingleton.Current = new StubPlatform { OnGetDebugOutput = () => debugOutput };
Expand Down Expand Up @@ -1024,5 +1010,13 @@ private TelemetryClient InitializeTelemetryClient(ICollection<ITelemetry> sentTe
var client = new TelemetryClient(telemetryConfiguration);
return client;
}

/// <summary>
/// Resets the TelemetryConfiguration.Active default instance to null so that the iKey auto population paths can be followed for testing.
/// </summary>
private void ClearActiveTelemetryConfiguration()
{
TelemetryConfiguration.Active = null;
}
}
}
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
9 changes: 1 addition & 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,12 +377,7 @@ public void Initialize(ITelemetry telemetry)

if (string.IsNullOrEmpty(instrumentationKey))
{
instrumentationKey = PlatformSingleton.Current.GetEnvironmentVariable(InstrumentationKeyWebSitesEnvironmentVariable);

if (string.IsNullOrEmpty(instrumentationKey))
{
instrumentationKey = this.configuration.InstrumentationKey;
}
instrumentationKey = this.configuration.InstrumentationKey;
}

var telemetryWithProperties = telemetry as ISupportProperties;
Expand Down

0 comments on commit 47925e5

Please sign in to comment.