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

Bug fix in TelemetryConfiguration + ConnectionStrings #1230

Merged
merged 5 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,63 @@ public void InitializesInstanceWithInformationFromConfigurationFileWhenItExists(
}
}

[TestMethod]
[TestCategory("ConnectionString")]
public void VerifyChannelEndpointsAreSetWhenParsingFromConfigFile()
{
#pragma warning disable CS0618 // Type or member is obsolete
// PART 1 - CONFIGURATION FACTORY IS EXPECTED TO CREATE A CONFIG THAT MATCHES THE XML
string ikeyConfig = "00000000-0000-0000-1111-000000000000";
string ikeyConfigConnectionString = "00000000-0000-0000-2222-000000000000";

string configString = @"<InstrumentationKey>00000000-0000-0000-1111-000000000000</InstrumentationKey>
<TelemetryChannel Type=""Microsoft.ApplicationInsights.Channel.InMemoryChannel, Microsoft.ApplicationInsights"">
<EndpointAddress>http://10.0.0.0/v2/track</EndpointAddress>
<DeveloperMode>true</DeveloperMode>
</TelemetryChannel>";

string configFileContents = Configuration(configString);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);

Assert.AreEqual(ikeyConfig, configuration.InstrumentationKey);
Assert.AreEqual(true, configuration.TelemetryChannel.DeveloperMode);
Assert.AreEqual("http://10.0.0.0/v2/track", configuration.TelemetryChannel.EndpointAddress, "failed to set Channel Endpoint to config value");

// PART 2 - VERIFY SETTING THE CONNECTION STRING WILL OVERWRITE CHANNEL ENDPOINT.
TelemetryConfiguration.Active = configuration;

TelemetryConfiguration.Active.ConnectionString = $"InstrumentationKey={ikeyConfigConnectionString};IngestionEndpoint=https://localhost:63029/";

var client = new TelemetryClient();

Assert.AreEqual(string.Empty, client.InstrumentationKey);
Assert.AreEqual(ikeyConfigConnectionString, client.TelemetryConfiguration.InstrumentationKey);
Assert.AreEqual("https://localhost:63029/v2/track", client.TelemetryConfiguration.TelemetryChannel.EndpointAddress);
#pragma warning restore CS0618 // Type or member is obsolete
}

[TestMethod]
[TestCategory("ConnectionString")]
public void VerifyThatChannelEndpointIsNotOverwrittenIfManuallyConfigured()
{
var configuration = new TelemetryConfiguration();
Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);

var customEndpoint = "http://10.0.0.0/v2/track";
var customChannel = new InMemoryChannel
{
EndpointAddress = customEndpoint
};

Assert.AreEqual(customEndpoint, customChannel.EndpointAddress);

configuration.TelemetryChannel = customChannel;

Assert.AreEqual(customEndpoint, customChannel.EndpointAddress, "channel endpoint was overwritten by config");
}


[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_EnVarConnectionString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public TelemetryConfiguration(string instrumentationKey, ITelemetryChannel chann
{
this.instrumentationKey = instrumentationKey ?? throw new ArgumentNullException(nameof(instrumentationKey));

SetTelemetryChannelEndpoint(channel, this.EndpointContainer.FormattedIngestionEndpoint);
SetTelemetryChannelEndpoint(channel, this.EndpointContainer.FormattedIngestionEndpoint, force: true);
var defaultSink = new TelemetrySink(this, channel);
defaultSink.Name = "default";
this.telemetrySinks.Add(defaultSink);
Expand Down Expand Up @@ -296,11 +296,11 @@ public string ConnectionString
// UPDATE TELEMETRY CHANNEL
foreach (var tSink in this.TelemetrySinks)
{
SetTelemetryChannelEndpoint(tSink.TelemetryChannel, this.EndpointContainer.FormattedIngestionEndpoint);
SetTelemetryChannelEndpoint(tSink.TelemetryChannel, this.EndpointContainer.FormattedIngestionEndpoint, force: true);
}

// UPDATE APPLICATION ID PROVIDER
SetApplicationIdEndpoint(this.ApplicationIdProvider, this.EndpointContainer.FormattedApplicationIdEndpoint);
SetApplicationIdEndpoint(this.ApplicationIdProvider, this.EndpointContainer.FormattedApplicationIdEndpoint, force: true);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -427,19 +427,26 @@ internal MetricManager GetMetricManager(bool createIfNotExists)
/// </summary>
/// <param name="applicationIdProvider">ApplicationIdProvider to set.</param>
/// <param name="endpoint">Endpoint value to set.</param>
private static void SetApplicationIdEndpoint(IApplicationIdProvider applicationIdProvider, string endpoint)
/// <param name="force">When the ConnectionString is set, ApplicationId Endpoint should be forced to update. If the ApplicationId has been set separately, we will only set endpoint if it is null.</param>
private static void SetApplicationIdEndpoint(IApplicationIdProvider applicationIdProvider, string endpoint, bool force = false)
{
if (applicationIdProvider != null)
{
if (applicationIdProvider is ApplicationInsightsApplicationIdProvider applicationInsightsApplicationIdProvider)
{
applicationInsightsApplicationIdProvider.ProfileQueryEndpoint = endpoint;
if (force || applicationInsightsApplicationIdProvider.ProfileQueryEndpoint == null)
{
applicationInsightsApplicationIdProvider.ProfileQueryEndpoint = endpoint;
}
}
else if (applicationIdProvider is DictionaryApplicationIdProvider dictionaryApplicationIdProvider)
{
if (dictionaryApplicationIdProvider.Next is ApplicationInsightsApplicationIdProvider innerApplicationIdProvider)
{
innerApplicationIdProvider.ProfileQueryEndpoint = endpoint;
if (force || innerApplicationIdProvider.ProfileQueryEndpoint == null)
{
innerApplicationIdProvider.ProfileQueryEndpoint = endpoint;
}
}
}
}
Expand All @@ -451,13 +458,17 @@ private static void SetApplicationIdEndpoint(IApplicationIdProvider applicationI
/// </summary>
/// <param name="channel">TelemetryChannel to set.</param>
/// <param name="endpoint">Endpoint value to set.</param>
private static void SetTelemetryChannelEndpoint(ITelemetryChannel channel, string endpoint)
/// /// <param name="force">When the ConnectionString is set, Channel Endpoint should be forced to update. If the Channel has been set separately, we will only set endpoint if it is null.</param>
private static void SetTelemetryChannelEndpoint(ITelemetryChannel channel, string endpoint, bool force = false)
{
if (channel != null)
{
if (channel is InMemoryChannel || channel.GetType().FullName == "Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel")
{
channel.EndpointAddress = endpoint;
if (force || channel.EndpointAddress == null)
{
channel.EndpointAddress = endpoint;
}
}
}
}
Expand Down