Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read iKey from environment variable (APPINSIGHTS_INSTRUMENTATIONKEY) #315

Merged
merged 7 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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).
- 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.
Expand Down
39 changes: 39 additions & 0 deletions Test/CoreSDK.Test/Shared/TelemetryClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,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()
{
Expand All @@ -458,6 +477,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()
{
Expand Down
5 changes: 5 additions & 0 deletions Test/CoreSDK.Test/TestFramework/Shared/StubPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public IDebugOutput GetDebugOutput()
{
return this.OnGetDebugOutput();
}

public string GetEnvironmentVariable(string name)
{
return Environment.GetEnvironmentVariable(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,10 @@ public IDebugOutput GetDebugOutput()
{
return this.debugOutput ?? (this.debugOutput = new TelemetryDebugWriter());
}

public string GetEnvironmentVariable(string name)
{
return Environment.GetEnvironmentVariable(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public IDebugOutput GetDebugOutput()

return this.debugOutput;
}

public string GetEnvironmentVariable(string name)
{
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there maybe a .NET Core equivalent for getting environment variables we could use?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something in Microsoft.Extensions.Configuration.EnvironmentVariables or Microsoft.Extensions.Configuration could be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in PCL. Those are .Net Core specific namespaces. If ever change target framework we can start supporting it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I keep thinking of .NET Core as a replacement for PCL but it is completely new and isn't portable backwards (as such).

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ internal interface IPlatform
/// Returns the platform specific Debugger writer to the VS output console.
/// </summary>
IDebugOutput GetDebugOutput();

// Read environment variable.
string GetEnvironmentVariable(string name);
}
}
7 changes: 7 additions & 0 deletions src/Core/Managed/Shared/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -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;
Expand Down Expand Up @@ -371,6 +373,11 @@ public void Initialize(ITelemetry telemetry)
if (string.IsNullOrEmpty(instrumentationKey))
{
instrumentationKey = this.configuration.InstrumentationKey;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this ikey has the least priority? Is it what we want for Web Apps case? In Web Apps you can configure ikey in AI.config file and then override it using Web App settings. Not other way around.

Also I'm concerned about performance of it. Maybe just read it once and cache? Or we need to support the scenario when environment variable will be changed later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree on caching, discussing the priority ordering, but I don't think it should change during runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, priority order is correct for web sites (@iusafaro)
Resolved caching.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iusafaro, this priority order doesn't make sense to me. Can you pls elaborate why it is done this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per offline discussion we will change the order so it is: code -> environment variable -> config

}
}

var telemetryWithProperties = telemetry as ISupportProperties;
Expand Down