From 7169d3a3334136eb586ca487e39f6d766d932ccf Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 20 Mar 2018 18:36:55 -0700 Subject: [PATCH 01/19] Refactor CorrelationIdLookupHelper to depend on config value (not implemented yet). Remove individual instances of Helper and instead depend on a singleton instance. Refactor tests to use single MockHelper class. --- Src/Common/Common.projitems | 5 +- .../CorrelationIdLookupHelper.cs | 66 ++++++------------- .../CorrelationIdLookupSingleton.cs | 45 +++++++++++++ .../ICorrelationIdLookupHelper.cs | 2 +- ...pendencyTrackingTelemetryModuleHttpTest.cs | 2 - .../CorrelationIdLookupHelperTests.cs | 1 + ...DependencyCollector.Shared.Tests.projitems | 1 - ...orDiagnosticListenerTests.Netstandard16.cs | 15 ++--- ...orDiagnosticListenerTests.Netstandard20.cs | 2 +- ...ktopDiagnosticSourceHttpProcessingTests.cs | 14 ++-- .../FrameworkHttpProcessingTest.cs | 6 +- .../MockCorrelationIdLookupHelper.cs | 20 ------ .../ProfilerHttpProcessingTest.cs | 65 ++++++------------ .../DependencyTrackingTelemetryModule.cs | 25 ++----- .../HttpCoreDiagnosticSourceListener.cs | 14 ++-- .../DesktopDiagnosticSourceHttpProcessing.cs | 4 +- .../Implementation/FrameworkHttpProcessing.cs | 4 +- .../Shared/Implementation/HttpProcessing.cs | 20 ++---- .../Implementation/ProfilerHttpProcessing.cs | 4 +- .../Shared/MockCorrelationIdLookupHelper.cs | 22 +++++++ .../Shared/TestFramework.Shared.projitems | 1 + .../RequestTrackingTelemetryModuleTest.cs | 56 ++++++---------- .../RequestTrackingTelemetryModule.cs | 56 ++-------------- 23 files changed, 177 insertions(+), 273 deletions(-) rename Src/Common/{ => CorrelationLookup}/CorrelationIdLookupHelper.cs (87%) create mode 100644 Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs rename Src/Common/{ => CorrelationLookup}/ICorrelationIdLookupHelper.cs (91%) delete mode 100644 Src/DependencyCollector/Shared.Tests/Implementation/MockCorrelationIdLookupHelper.cs create mode 100644 Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs diff --git a/Src/Common/Common.projitems b/Src/Common/Common.projitems index ae60f8124..7d635d8fe 100644 --- a/Src/Common/Common.projitems +++ b/Src/Common/Common.projitems @@ -11,10 +11,11 @@ - + + - + diff --git a/Src/Common/CorrelationIdLookupHelper.cs b/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs similarity index 87% rename from Src/Common/CorrelationIdLookupHelper.cs rename to Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs index 0cdf094e1..38f0f5b1a 100644 --- a/Src/Common/CorrelationIdLookupHelper.cs +++ b/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs @@ -1,4 +1,4 @@ -namespace Microsoft.ApplicationInsights.Common +namespace Microsoft.ApplicationInsights.Common.CorrelationLookup { using System; using System.Collections.Concurrent; @@ -26,13 +26,12 @@ internal class CorrelationIdLookupHelper : ICorrelationIdLookupHelper private const string CorrelationIdFormat = "cid-v1:{0}"; private const string AppIdQueryApiRelativeUriFormat = "api/profiles/{0}/appId"; + private const string AppIdQueryApiFullUriFormat = "https://dc.services.visualstudio.com/api/profiles/{0}/appId"; // We have arbitrarily chosen 5 second delay between trying to get app Id once we get a failure while trying to get it. // This is to throttle tries between failures to safeguard against performance hits. The impact would be that telemetry generated during this interval would not have x-component correlation id. private readonly TimeSpan intervalBetweenFailedRetries = TimeSpan.FromSeconds(30); - private Uri endpointAddress; - private ConcurrentDictionary knownCorrelationIds = new ConcurrentDictionary(); private ConcurrentDictionary fetchTasks = new ConcurrentDictionary(); @@ -43,57 +42,22 @@ internal class CorrelationIdLookupHelper : ICorrelationIdLookupHelper private Func> provideAppId; /// - /// Initializes a new instance of the class mostly to be used by the test classes to provide an override for fetching appId logic. - /// - /// The delegate to be called to fetch the appId. - public CorrelationIdLookupHelper(Func> appIdProviderMethod) - { - if (appIdProviderMethod == null) - { - throw new ArgumentNullException(nameof(appIdProviderMethod)); - } - - this.provideAppId = appIdProviderMethod; - } - - /// - /// Initializes a new instance of the class mostly to be used by the test classes to seed the instrumentation key -> app Id relationship. + /// Initializes a new instance of the class. /// - /// A dictionary that contains known instrumentation key - app id relationship. - public CorrelationIdLookupHelper(Dictionary mapSeed) + public CorrelationIdLookupHelper() { - if (mapSeed == null) - { - throw new ArgumentNullException(nameof(mapSeed)); - } - this.provideAppId = this.FetchAppIdFromService; - - foreach (var entry in mapSeed) - { - this.knownCorrelationIds[entry.Key] = entry.Value; - } } /// - /// Initializes a new instance of the class. + /// Unit Test Only! Initializes a new instance of the class with an override for fetching appId logic. /// - /// Endpoint that is to be used to fetch appId. - public CorrelationIdLookupHelper(string endpointAddress) + /// The delegate to be called to fetch the appId. + internal CorrelationIdLookupHelper(Func> appIdProviderMethod) { - if (string.IsNullOrEmpty(endpointAddress)) - { - throw new ArgumentNullException(nameof(endpointAddress)); - } - - Uri endpointUri = new Uri(endpointAddress); - - // Get the base URI, so that we can append the known relative segments to it. - this.endpointAddress = new Uri(endpointUri.AbsoluteUri.Substring(0, endpointUri.AbsoluteUri.Length - endpointUri.LocalPath.Length)); - - this.provideAppId = this.FetchAppIdFromService; + this.provideAppId = appIdProviderMethod ?? throw new ArgumentNullException(nameof(appIdProviderMethod)); } - + /// /// Retrieves the correlation id corresponding to a given instrumentation key. /// @@ -274,7 +238,17 @@ private async Task FetchAppIdFromService(string instrumentationKey) /// Computed Uri. private Uri GetAppIdEndPointUri(string instrumentationKey) { - return new Uri(this.endpointAddress, string.Format(CultureInfo.InvariantCulture, AppIdQueryApiRelativeUriFormat, instrumentationKey)); + Uri endpointProxyBaseUri = null; + //// TODO: endpointProxyBaseUri = TelemetryConfiguration.Active.GetEndpointProxyBaseUri(); // MUST WAIT FOR CHANGES IN BASE SDK!!! + + if (endpointProxyBaseUri != null) + { + return new Uri(endpointProxyBaseUri, string.Format(CultureInfo.InvariantCulture, AppIdQueryApiRelativeUriFormat, instrumentationKey)); + } + else + { + return new Uri(string.Format(CultureInfo.InvariantCulture, AppIdQueryApiFullUriFormat, instrumentationKey)); + } } /// diff --git a/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs b/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs new file mode 100644 index 000000000..69e33a8b9 --- /dev/null +++ b/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs @@ -0,0 +1,45 @@ +namespace Microsoft.ApplicationInsights.Common.CorrelationLookup +{ + /// + /// This class holds a single instance of CorrelationIdLookupHelper for the entire application. + /// This is a workaround because we don't use dependency injection. + /// This instance can be overwritten by UnitTests. + /// + internal static class CorrelationIdLookupSingleton + { + private static object semaphore = new object(); + private static ICorrelationIdLookupHelper instance; + + /// + /// Gets or sets the instance of ICorrelationIdLookupHelper to be used by Modules. + /// Modules should not copy this locally and should always refer to this Instance. + /// Set will be used by Unit Tests. + /// + public static ICorrelationIdLookupHelper Instance + { + get + { + if (instance == null) + { + lock (semaphore) + { + if (instance == null) + { + instance = new CorrelationIdLookupHelper(); + } + } + } + + return instance; + } + + set + { + lock (semaphore) + { + instance = value; + } + } + } + } +} diff --git a/Src/Common/ICorrelationIdLookupHelper.cs b/Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs similarity index 91% rename from Src/Common/ICorrelationIdLookupHelper.cs rename to Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs index 6a1868385..081d81ddd 100644 --- a/Src/Common/ICorrelationIdLookupHelper.cs +++ b/Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs @@ -1,4 +1,4 @@ -namespace Microsoft.ApplicationInsights.Common +namespace Microsoft.ApplicationInsights.Common.CorrelationLookup { /// /// An interface for getting a correlation id from a provided instrumentation key. diff --git a/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs b/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs index a4bb3f65f..7fb6ac1b7 100644 --- a/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs +++ b/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs @@ -218,7 +218,6 @@ public void OnBeginOnEndAreNotCalledForAppInsightsUrl() { using (var module = new DependencyTrackingTelemetryModule()) { - module.ProfileQueryEndpoint = FakeProfileApiEndpoint; module.Initialize(this.config); using (var listener = new TestEventListener()) @@ -561,7 +560,6 @@ private DependencyTrackingTelemetryModule CreateDependencyTrackingModule(bool en module.DisableDiagnosticSourceInstrumentation = true; } - module.ProfileQueryEndpoint = FakeProfileApiEndpoint; module.Initialize(this.config); Assert.AreEqual(enableDiagnosticSource, DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated); diff --git a/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs b/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs index ca9ef845f..78aeaa5b7 100644 --- a/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs +++ b/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.VisualStudio.TestTools.UnitTesting; /// diff --git a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems index f78b435ea..e2167258c 100644 --- a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems +++ b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems @@ -22,7 +22,6 @@ - diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs index 9f6f71c7c..0cd863ac0 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs @@ -9,6 +9,7 @@ namespace Microsoft.ApplicationInsights.Tests using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -34,8 +35,8 @@ public partial class DependencyCollectorDiagnosticListenerTests private readonly List sentTelemetry = new List(); private string instrumentationKey; + private string correlationId; private StubTelemetryChannel telemetryChannel; - private MockCorrelationIdLookupHelper mockCorrelationIdLookupHelper; private HttpCoreDiagnosticSourceListener listener; /// @@ -45,6 +46,7 @@ public partial class DependencyCollectorDiagnosticListenerTests public void Initialize() { this.instrumentationKey = Guid.NewGuid().ToString(); + this.correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(this.instrumentationKey); this.telemetryChannel = new StubTelemetryChannel() { @@ -52,10 +54,7 @@ public void Initialize() OnSend = this.sentTelemetry.Add }; - this.mockCorrelationIdLookupHelper = new MockCorrelationIdLookupHelper(new Dictionary() - { - [this.instrumentationKey] = MockAppId - }); + CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); var configuration = new TelemetryConfiguration { @@ -66,10 +65,8 @@ public void Initialize() configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); this.listener = new HttpCoreDiagnosticSourceListener( configuration, - this.telemetryChannel.EndpointAddress, setComponentCorrelationHttpHeaders: true, - correlationDomainExclusionList: new string[] { "excluded.host.com" }, - correlationIdLookupHelper: this.mockCorrelationIdLookupHelper); + correlationDomainExclusionList: new string[] { "excluded.host.com" }); } /// @@ -212,7 +209,7 @@ public void OnRequestWithRequestEvent() Assert.AreEqual(string.Empty, telemetry.ResultCode); Assert.AreEqual(true, telemetry.Success); - Assert.AreEqual(MockAppId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); + Assert.AreEqual(this.correlationId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); Assert.AreEqual(null, GetRequestContextKeyValue(request, RequestResponseHeaders.StandardRootIdHeader)); var legacyParentIdHeader = GetRequestHeaderValues(request, RequestResponseHeaders.StandardParentIdHeader).Single(); diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs index f41e8ea8b..b65a517a7 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs @@ -38,7 +38,7 @@ public void OnActivityStartInjectsHeaders() // check only legacy headers here Assert.AreEqual(activity.RootId, request.Headers.GetValues(RequestResponseHeaders.StandardRootIdHeader).Single()); Assert.AreEqual(activity.Id, request.Headers.GetValues(RequestResponseHeaders.StandardParentIdHeader).Single()); - Assert.AreEqual(MockAppId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); + Assert.AreEqual("cid-v1:" + this.instrumentationKey + "-appId", GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); } /// diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs index 291e24476..11b2d0640 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs @@ -10,6 +10,7 @@ namespace Microsoft.ApplicationInsights.Tests using System.Threading; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -24,7 +25,6 @@ namespace Microsoft.ApplicationInsights.Tests public class DesktopDiagnosticSourceHttpProcessingTests { #region Fields - private const string RandomAppIdEndpoint = "http://app.id.endpoint"; // appIdEndpoint - this really won't be used for tests because of the app id provider override. private const int TimeAccuracyMilliseconds = 50; private Uri testUrl = new Uri("http://www.microsoft.com/"); private int sleepTimeMsecBetweenBeginAndEnd = 100; @@ -42,9 +42,10 @@ public void TestInitialize() this.sendItems = new List(); this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; this.configuration.InstrumentationKey = Guid.NewGuid().ToString(); - this.httpDesktopProcessingFramework = new DesktopDiagnosticSourceHttpProcessing(this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), /*setCorrelationHeaders*/ true, new List(), RandomAppIdEndpoint); - this.httpDesktopProcessingFramework.OverrideCorrelationIdLookupHelper(new CorrelationIdLookupHelper(new Dictionary { { this.configuration.InstrumentationKey, "cid-v1:" + this.configuration.InstrumentationKey } })); + this.httpDesktopProcessingFramework = new DesktopDiagnosticSourceHttpProcessing(this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), /*setCorrelationHeaders*/ true, new List()); DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false; + + CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); } [TestCleanup] @@ -224,8 +225,7 @@ public void RddTestHttpDesktopProcessingFrameworkOnBeginSkipsAddingSourceHeaderP this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), false, - new List(), - RandomAppIdEndpoint); + new List()); localHttpProcessingFramework.OnBegin(request); Assert.IsNull(request.Headers[RequestResponseHeaders.RequestContextHeader]); @@ -236,8 +236,8 @@ public void RddTestHttpDesktopProcessingFrameworkOnBeginSkipsAddingSourceHeaderP this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), true, - exclusionList, - RandomAppIdEndpoint); + exclusionList); + localHttpProcessingFramework.OnBegin(request); Assert.IsNull(request.Headers[RequestResponseHeaders.RequestContextHeader]); Assert.AreEqual(0, request.Headers.Keys.Cast().Count(x => x.StartsWith("x-ms-", StringComparison.OrdinalIgnoreCase))); diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs index 88f1ea302..acf621818 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -44,9 +45,10 @@ public void TestInitialize() this.sendItems = new List(); this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; this.configuration.InstrumentationKey = Guid.NewGuid().ToString(); - this.httpProcessingFramework = new FrameworkHttpProcessing(this.configuration, this.cache, /*setCorrelationHeaders*/ true, new List(), RandomAppIdEndpoint); - this.httpProcessingFramework.OverrideCorrelationIdLookupHelper(new CorrelationIdLookupHelper(new Dictionary { { this.configuration.InstrumentationKey, "cid-v1:" + this.configuration.InstrumentationKey } })); + this.httpProcessingFramework = new FrameworkHttpProcessing(this.configuration, this.cache, /*setCorrelationHeaders*/ true, new List()); DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false; + + CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); } [TestCleanup] diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/MockCorrelationIdLookupHelper.cs b/Src/DependencyCollector/Shared.Tests/Implementation/MockCorrelationIdLookupHelper.cs deleted file mode 100644 index 381ae03f9..000000000 --- a/Src/DependencyCollector/Shared.Tests/Implementation/MockCorrelationIdLookupHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Microsoft.ApplicationInsights.Tests -{ - using System.Collections.Generic; - using Microsoft.ApplicationInsights.Common; - - internal class MockCorrelationIdLookupHelper : ICorrelationIdLookupHelper - { - private readonly Dictionary instrumentationKeyToCorrelationIdMap; - - public MockCorrelationIdLookupHelper(Dictionary instrumentationKeyToCorrelationIdMap) - { - this.instrumentationKeyToCorrelationIdMap = instrumentationKeyToCorrelationIdMap; - } - - public bool TryGetXComponentCorrelationId(string instrumentationKey, out string correlationId) - { - return this.instrumentationKeyToCorrelationIdMap.TryGetValue(instrumentationKey, out correlationId); - } - } -} diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs index e0e6e75de..6a4416d79 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs @@ -15,6 +15,7 @@ using Common; using Microsoft.ApplicationInsights.Channel; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -32,7 +33,6 @@ public sealed class ProfilerHttpProcessingTest : IDisposable { #region Fields private const int TimeAccuracyMilliseconds = 150; // this may be big number when under debugger - private const string RandomAppIdEndpoint = "http://app.id.endpoint"; // appIdEndpoint - this really won't be used for tests because of the app id provider override. private TelemetryConfiguration configuration; private Uri testUrl = new Uri("http://www.microsoft.com/"); private Uri testUrlNonStandardPort = new Uri("http://www.microsoft.com:911/"); @@ -47,7 +47,23 @@ public sealed class ProfilerHttpProcessingTest : IDisposable [TestInitialize] public void TestInitialize() { - this.Initialize(Guid.NewGuid().ToString()); + var instrumentationKey = Guid.NewGuid().ToString(); + + this.configuration = new TelemetryConfiguration(); + this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); + this.sendItems = new List(); + this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; + this.configuration.InstrumentationKey = instrumentationKey; + this.httpProcessingProfiler = new ProfilerHttpProcessing( + this.configuration, + null, + new ObjectInstanceBasedOperationHolder(), + true /*setCorrelationHeaders*/, + new List()); + + CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); + + this.ex = new Exception(); } [TestCleanup] @@ -112,19 +128,7 @@ public void RddTestHttpProcessingProfilerOnEndAddsAppIdToTargetField() // This will not match the current component's App ID. Hence represents an external component. string ikey = "0935FC42-FE1A-4C67-975C-0C9D5CBDEE8E"; string appId = ikey + "-appId"; - - this.httpProcessingProfiler.OverrideCorrelationIdLookupHelper(new CorrelationIdLookupHelper(new Dictionary - { - { - ikey, - "cid-v1:" + appId - }, - { - this.configuration.InstrumentationKey, - "cid-v1:" + this.configuration.InstrumentationKey + "-appId" - } - })); - + this.SimulateWebRequestResponseWithAppId(appId); Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent"); @@ -257,13 +261,13 @@ public void RddTestHttpProcessingProfilerOnBeginSkipsAddingSourceHeaderPerConfig Assert.IsNull(request.Headers[RequestResponseHeaders.RequestContextHeader]); Assert.AreEqual(0, request.Headers.Keys.Cast().Where((x) => { return x.StartsWith("x-ms-", StringComparison.OrdinalIgnoreCase); }).Count()); - var httpProcessingProfiler = new ProfilerHttpProcessing(this.configuration, null, new ObjectInstanceBasedOperationHolder(), /*setCorrelationHeaders*/ false, new List(), RandomAppIdEndpoint); + var httpProcessingProfiler = new ProfilerHttpProcessing(this.configuration, null, new ObjectInstanceBasedOperationHolder(), /*setCorrelationHeaders*/ false, new List()); httpProcessingProfiler.OnBeginForGetResponse(request); Assert.IsNull(request.Headers[RequestResponseHeaders.RequestContextHeader]); Assert.AreEqual(0, request.Headers.Keys.Cast().Where((x) => { return x.StartsWith("x-ms-", StringComparison.OrdinalIgnoreCase); }).Count()); ICollection exclusionList = new SanitizedHostList() { "randomstringtoexclude", hostnamepart }; - httpProcessingProfiler = new ProfilerHttpProcessing(this.configuration, null, new ObjectInstanceBasedOperationHolder(), /*setCorrelationHeaders*/ true, exclusionList, RandomAppIdEndpoint); + httpProcessingProfiler = new ProfilerHttpProcessing(this.configuration, null, new ObjectInstanceBasedOperationHolder(), /*setCorrelationHeaders*/ true, exclusionList); httpProcessingProfiler.OnBeginForGetResponse(request); Assert.IsNull(request.Headers[RequestResponseHeaders.RequestContextHeader]); Assert.AreEqual(0, request.Headers.Keys.Cast().Where((x) => { return x.StartsWith("x-ms-", StringComparison.OrdinalIgnoreCase); }).Count()); @@ -890,33 +894,6 @@ private string GetCorrelationIdHeaderValue(string appId) { return string.Format(CultureInfo.InvariantCulture, "{0}=cid-v1:{1}", RequestResponseHeaders.RequestContextCorrelationTargetKey, appId); } - - private void Initialize(string instrumentationKey) - { - this.configuration = new TelemetryConfiguration(); - this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); - this.sendItems = new List(); - this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; - this.configuration.InstrumentationKey = instrumentationKey; - this.httpProcessingProfiler = new ProfilerHttpProcessing( - this.configuration, - null, - new ObjectInstanceBasedOperationHolder(), - true /*setCorrelationHeaders*/, - new List(), - RandomAppIdEndpoint); - - var correlationIdLookupHelper = new CorrelationIdLookupHelper(new Dictionary - { - { - instrumentationKey, - "cid-v1:" + instrumentationKey + "-appId" - } - }); - - this.httpProcessingProfiler.OverrideCorrelationIdLookupHelper(correlationIdLookupHelper); - this.ex = new Exception(); - } #endregion Helpers } } diff --git a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs index 68f3d0b59..b42b9ec92 100644 --- a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs +++ b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs @@ -95,19 +95,6 @@ public bool SetComponentCorrelationHttpHeaders } } - /// - /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). - /// - public string ProfileQueryEndpoint { get; set; } - - internal string EffectiveProfileQueryEndpoint - { - get - { - return string.IsNullOrEmpty(this.ProfileQueryEndpoint) ? this.telemetryConfiguration.TelemetryChannel.EndpointAddress : this.ProfileQueryEndpoint; - } - } - /// /// IDisposable implementation. /// @@ -145,10 +132,8 @@ public void Initialize(TelemetryConfiguration configuration) // NET45 referencing .net core System.Net.Http supports diagnostic listener this.httpCoreDiagnosticSourceListener = new HttpCoreDiagnosticSourceListener( configuration, - this.EffectiveProfileQueryEndpoint, this.SetComponentCorrelationHttpHeaders, - this.ExcludeComponentCorrelationHttpHeadersOnDomains, - null); + this.ExcludeComponentCorrelationHttpHeadersOnDomains); if (this.IncludeDiagnosticSourceActivities != null && this.IncludeDiagnosticSourceActivities.Count > 0) { @@ -195,7 +180,7 @@ internal virtual void InitializeForRuntimeProfiler() var agentVersion = Decorator.GetAgentVersion(); DependencyCollectorEventSource.Log.RemoteDependencyModuleInformation("AgentVersion is " + agentVersion); - this.httpProcessing = new ProfilerHttpProcessing(this.telemetryConfiguration, agentVersion, DependencyTableStore.Instance.WebRequestConditionalHolder, this.SetComponentCorrelationHttpHeaders, this.ExcludeComponentCorrelationHttpHeadersOnDomains, this.EffectiveProfileQueryEndpoint); + this.httpProcessing = new ProfilerHttpProcessing(this.telemetryConfiguration, agentVersion, DependencyTableStore.Instance.WebRequestConditionalHolder, this.SetComponentCorrelationHttpHeaders, this.ExcludeComponentCorrelationHttpHeadersOnDomains); this.sqlCommandProcessing = new ProfilerSqlCommandProcessing(this.telemetryConfiguration, agentVersion, DependencyTableStore.Instance.SqlRequestConditionalHolder); this.sqlConnectionProcessing = new ProfilerSqlConnectionProcessing(this.telemetryConfiguration, agentVersion, DependencyTableStore.Instance.SqlRequestConditionalHolder); @@ -270,8 +255,7 @@ private void InitializeForDiagnosticAndFrameworkEventSource() this.telemetryConfiguration, DependencyTableStore.Instance.WebRequestCacheHolder, this.SetComponentCorrelationHttpHeaders, - this.ExcludeComponentCorrelationHttpHeadersOnDomains, - this.EffectiveProfileQueryEndpoint); + this.ExcludeComponentCorrelationHttpHeadersOnDomains); this.httpDesktopDiagnosticSourceListener = new HttpDesktopDiagnosticSourceListener(desktopHttpProcessing, new ApplicationInsightsUrlFilter(this.telemetryConfiguration)); } @@ -279,8 +263,7 @@ private void InitializeForDiagnosticAndFrameworkEventSource() this.telemetryConfiguration, DependencyTableStore.Instance.WebRequestCacheHolder, this.SetComponentCorrelationHttpHeaders, - this.ExcludeComponentCorrelationHttpHeadersOnDomains, - this.EffectiveProfileQueryEndpoint); + this.ExcludeComponentCorrelationHttpHeadersOnDomains); // In 4.5 EventListener has a race condition issue in constructor so we retry to create listeners this.httpEventListener = RetryPolicy.Retry( diff --git a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs index de021548d..a06952238 100644 --- a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs +++ b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs @@ -12,6 +12,7 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; @@ -30,7 +31,6 @@ internal class HttpCoreDiagnosticSourceListener : IObserver correlationDomainExclusionList; private readonly ApplicationInsightsUrlFilter applicationInsightsUrlFilter; private readonly bool setComponentCorrelationHttpHeaders; - private readonly ICorrelationIdLookupHelper correlationIdLookupHelper; private readonly TelemetryClient client; private readonly TelemetryConfiguration configuration; private readonly HttpCoreDiagnosticSourceSubscriber subscriber; @@ -57,12 +57,7 @@ internal class HttpCoreDiagnosticSourceListener : IObserver correlationDomainExclusionList, - ICorrelationIdLookupHelper correlationIdLookupHelper) + public HttpCoreDiagnosticSourceListener(TelemetryConfiguration configuration, bool setComponentCorrelationHttpHeaders, IEnumerable correlationDomainExclusionList) { this.client = new TelemetryClient(configuration); this.client.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("rdd" + RddSource.DiagnosticSourceCore + ":"); @@ -73,7 +68,6 @@ public HttpCoreDiagnosticSourceListener( this.configuration = configuration; this.applicationInsightsUrlFilter = new ApplicationInsightsUrlFilter(configuration); this.setComponentCorrelationHttpHeaders = setComponentCorrelationHttpHeaders; - this.correlationIdLookupHelper = correlationIdLookupHelper ?? new CorrelationIdLookupHelper(effectiveProfileQueryEndpoint); this.correlationDomainExclusionList = correlationDomainExclusionList ?? Enumerable.Empty(); this.subscriber = new HttpCoreDiagnosticSourceSubscriber(this, this.applicationInsightsUrlFilter, this.isNetCore20HttpClient); @@ -442,7 +436,7 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta if (!string.IsNullOrEmpty(instrumentationKey) && !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey)) { string sourceApplicationId; - if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumentationKey, out sourceApplicationId)) + if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(instrumentationKey, out sourceApplicationId)) { HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId); } @@ -508,7 +502,7 @@ private void ParseResponse(HttpResponseMessage response, DependencyTelemetry tel { // We only add the cross component correlation key if the key does not represent the current component. string sourceApplicationId; - if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) && + if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) && targetApplicationId != sourceApplicationId) { telemetry.Type = RemoteDependencyConstants.AI; diff --git a/Src/DependencyCollector/Shared/Implementation/DesktopDiagnosticSourceHttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/DesktopDiagnosticSourceHttpProcessing.cs index 463c258da..777dc7302 100644 --- a/Src/DependencyCollector/Shared/Implementation/DesktopDiagnosticSourceHttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/DesktopDiagnosticSourceHttpProcessing.cs @@ -16,8 +16,8 @@ internal sealed class DesktopDiagnosticSourceHttpProcessing : HttpProcessing { private readonly CacheBasedOperationHolder telemetryTable; - internal DesktopDiagnosticSourceHttpProcessing(TelemetryConfiguration configuration, CacheBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList, string appIdEndpoint) - : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.DiagnosticSourceDesktop + ":"), null, setCorrelationHeaders, correlationDomainExclusionList, appIdEndpoint) + internal DesktopDiagnosticSourceHttpProcessing(TelemetryConfiguration configuration, CacheBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList) + : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.DiagnosticSourceDesktop + ":"), null, setCorrelationHeaders, correlationDomainExclusionList) { if (telemetryTupleHolder == null) { diff --git a/Src/DependencyCollector/Shared/Implementation/FrameworkHttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/FrameworkHttpProcessing.cs index 537657a6d..b15ad157c 100644 --- a/Src/DependencyCollector/Shared/Implementation/FrameworkHttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/FrameworkHttpProcessing.cs @@ -18,8 +18,8 @@ internal sealed class FrameworkHttpProcessing : HttpProcessing internal CacheBasedOperationHolder TelemetryTable; private readonly ApplicationInsightsUrlFilter applicationInsightsUrlFilter; - internal FrameworkHttpProcessing(TelemetryConfiguration configuration, CacheBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList, string appIdEndpoint) - : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.Framework + ":"), null, setCorrelationHeaders, correlationDomainExclusionList, appIdEndpoint) + internal FrameworkHttpProcessing(TelemetryConfiguration configuration, CacheBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList) + : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.Framework + ":"), null, setCorrelationHeaders, correlationDomainExclusionList) { if (telemetryTupleHolder == null) { diff --git a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs index 18f9ec612..b8819e2c7 100644 --- a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs @@ -9,6 +9,7 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using System.Web; using Extensibility.Implementation.Tracing; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector.Implementation.Operation; using Microsoft.ApplicationInsights.Extensibility; @@ -24,12 +25,11 @@ internal abstract class HttpProcessing private readonly ApplicationInsightsUrlFilter applicationInsightsUrlFilter; private ICollection correlationDomainExclusionList; private bool setCorrelationHeaders; - private CorrelationIdLookupHelper correlationIdLookupHelper; /// /// Initializes a new instance of the class. /// - public HttpProcessing(TelemetryConfiguration configuration, string sdkVersion, string agentVersion, bool setCorrelationHeaders, ICollection correlationDomainExclusionList, string appIdEndpoint) + public HttpProcessing(TelemetryConfiguration configuration, string sdkVersion, string agentVersion, bool setCorrelationHeaders, ICollection correlationDomainExclusionList) { if (configuration == null) { @@ -45,7 +45,6 @@ public HttpProcessing(TelemetryConfiguration configuration, string sdkVersion, s this.telemetryClient = new TelemetryClient(configuration); this.correlationDomainExclusionList = correlationDomainExclusionList; this.setCorrelationHeaders = setCorrelationHeaders; - this.correlationIdLookupHelper = new CorrelationIdLookupHelper(appIdEndpoint); this.telemetryClient.Context.GetInternalContext().SdkVersion = sdkVersion; if (!string.IsNullOrEmpty(agentVersion)) @@ -69,16 +68,7 @@ internal Uri GetUrl(WebRequest webRequest) return resource; } - - /// - /// Simple test hook, that allows for using a stub rather than the implementation that calls the original service. - /// - /// Lookup header to use. - internal void OverrideCorrelationIdLookupHelper(CorrelationIdLookupHelper correlationIdLookupHelper) - { - this.correlationIdLookupHelper = correlationIdLookupHelper; - } - + /// /// Common helper for all Begin Callbacks. /// @@ -172,7 +162,7 @@ internal object OnBegin(object thisObj, bool injectCorrelationHeaders = true) && webRequest.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey) == null) { string appId; - if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out appId)) + if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out appId)) { webRequest.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey, appId); } @@ -431,7 +421,7 @@ private void SetTarget(DependencyTelemetry telemetry, WebHeaderCollection respon } string currentComponentAppId; - if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out currentComponentAppId)) + if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out currentComponentAppId)) { // We only add the cross component correlation key if the key does not remain the current component. if (!string.IsNullOrEmpty(targetAppId) && targetAppId != currentComponentAppId) diff --git a/Src/DependencyCollector/Shared/Implementation/ProfilerHttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/ProfilerHttpProcessing.cs index 6fdde6fab..eb3b5a024 100644 --- a/Src/DependencyCollector/Shared/Implementation/ProfilerHttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/ProfilerHttpProcessing.cs @@ -20,8 +20,8 @@ internal sealed class ProfilerHttpProcessing : HttpProcessing /// /// Initializes a new instance of the class. /// - public ProfilerHttpProcessing(TelemetryConfiguration configuration, string agentVersion, ObjectInstanceBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList, string appIdEndpoint) - : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.Profiler + ":"), agentVersion, setCorrelationHeaders, correlationDomainExclusionList, appIdEndpoint) + public ProfilerHttpProcessing(TelemetryConfiguration configuration, string agentVersion, ObjectInstanceBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection correlationDomainExclusionList) + : base(configuration, SdkVersionUtils.GetSdkVersion("rdd" + RddSource.Profiler + ":"), agentVersion, setCorrelationHeaders, correlationDomainExclusionList) { if (telemetryTupleHolder == null) { diff --git a/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs b/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs new file mode 100644 index 000000000..3f2dfafd7 --- /dev/null +++ b/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs @@ -0,0 +1,22 @@ +namespace Microsoft.ApplicationInsights.Web.TestFramework +{ + using System.Globalization; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; + + /// + /// Use this to have TryGet return the instrumentation key as the correlation id. + /// + internal class MockCorrelationIdLookupHelper : ICorrelationIdLookupHelper + { + public static string GetCorrelationIdValue(string instrumentationKey) + { + return string.Format(CultureInfo.InvariantCulture, "cid-v1:{0}-appId", instrumentationKey); + } + + public bool TryGetXComponentCorrelationId(string instrumentationKey, out string correlationId) + { + correlationId = GetCorrelationIdValue(instrumentationKey); + return true; + } + } +} diff --git a/Src/TestFramework/Shared/TestFramework.Shared.projitems b/Src/TestFramework/Shared/TestFramework.Shared.projitems index d9e92783f..bd39371d2 100644 --- a/Src/TestFramework/Shared/TestFramework.Shared.projitems +++ b/Src/TestFramework/Shared/TestFramework.Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index 4b9ef54c2..dfdfc2795 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -9,6 +9,7 @@ using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.TestFramework; @@ -25,13 +26,11 @@ [TestClass] public partial class RequestTrackingTelemetryModuleTest { - private CorrelationIdLookupHelper correlationIdLookupHelper = new CorrelationIdLookupHelper((string ikey) => + [TestInitialize] + public void TestInitialize() { - // Pretend App Id is the same as Ikey - var tcs = new TaskCompletionSource(); - tcs.SetResult(ikey + "-appId"); - return tcs.Task; - }); + CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); + } [TestCleanup] public void Cleanup() @@ -348,31 +347,18 @@ public void OnEndAddsSourceFieldForRequestWithCorrelationId() { // ARRANGE string instrumentationKey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf"; - string appId = instrumentationKey + "-appId"; + string correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(instrumentationKey); Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(appId)); + headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(instrumentationKey)); var context = HttpModuleHelper.GetFakeHttpContext(headers); // My instrumentation key and hence app id is random / newly generated. The appId header is different - hence a different component. var config = TelemetryConfiguration.CreateDefault(); config.InstrumentationKey = Guid.NewGuid().ToString(); - - // Add ikey -> app Id mappings in correlation helper - var correlationHelper = new CorrelationIdLookupHelper(new Dictionary() - { - { - config.InstrumentationKey, - config.InstrumentationKey + "-appId" - }, - { - instrumentationKey, - appId + "-appId" - } - }); - - var module = this.RequestTrackingTelemetryModuleFactory(null /*use default*/, correlationHelper); + + var module = this.RequestTrackingTelemetryModuleFactory(null /*use default*/); // ACT module.Initialize(config); @@ -380,7 +366,7 @@ public void OnEndAddsSourceFieldForRequestWithCorrelationId() module.OnEndRequest(context); // VALIDATE - Assert.Equal(this.GetCorrelationIdValue(appId), context.GetRequestTelemetry().Source); + Assert.Equal(correlationId, context.GetRequestTelemetry().Source); } [TestMethod] @@ -408,8 +394,10 @@ public void OnEndDoesNotAddSourceFieldForRequestWithOutSourceIkeyHeader() [TestMethod] public void OnEndDoesNotOverrideSourceField() { - // ARRANGE - string appIdInHeader = this.GetCorrelationIdHeaderValue("b3eb14d6-bb32-4542-9b93-473cd94aaedf"); + // ARRANGE + string instrumentationKey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf"; + + string appIdInHeader = this.GetCorrelationIdHeaderValue(instrumentationKey); string someFieldHeader = "SomeField=SomeNameHere"; string appIdInSourceField = "9AB8EDCB-21D2-44BB-A64A-C33BB4515F20"; @@ -454,25 +442,21 @@ private string GetActivityRootId(string telemetryId) return telemetryId.Substring(1, telemetryId.IndexOf('.') - 1); } - private RequestTrackingTelemetryModule RequestTrackingTelemetryModuleFactory(TelemetryConfiguration config = null, CorrelationIdLookupHelper correlationHelper = null) + private RequestTrackingTelemetryModule RequestTrackingTelemetryModuleFactory(TelemetryConfiguration config = null) { var module = new RequestTrackingTelemetryModule() { EnableChildRequestTrackingSuppression = false }; - module.OverrideCorrelationIdLookupHelper(correlationHelper ?? this.correlationIdLookupHelper); + module.Initialize(config ?? this.CreateDefaultConfig(HttpModuleHelper.GetFakeHttpContext())); return module; } - - private string GetCorrelationIdValue(string appId) - { - return string.Format(CultureInfo.InvariantCulture, "cid-v1:{0}", appId); - } - - private string GetCorrelationIdHeaderValue(string appId) + + private string GetCorrelationIdHeaderValue(string ikey) { - return string.Format(CultureInfo.InvariantCulture, "{0}=cid-v1:{1}", RequestResponseHeaders.RequestContextCorrelationSourceKey, appId); + var correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(ikey); + return string.Format(CultureInfo.InvariantCulture, "{0}={1}", RequestResponseHeaders.RequestContextCorrelationSourceKey, correlationId); } internal class FakeHttpHandler : IHttpHandler diff --git a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs index f9c0b0b70..479a50262 100644 --- a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs +++ b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs @@ -8,6 +8,7 @@ using Extensibility.Implementation.Tracing; using Microsoft.ApplicationInsights.Common; + using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Web.Implementation; @@ -22,7 +23,6 @@ public class RequestTrackingTelemetryModule : ITelemetryModule private bool initializationErrorReported; private bool correlationHeadersEnabled = true; private string telemetryChannelEnpoint; - private CorrelationIdLookupHelper correlationIdLookupHelper; private ChildRequestTrackingSuppressionModule childRequestTrackingSuppressionModule = null; /// @@ -80,19 +80,6 @@ public bool SetComponentCorrelationHttpHeaders } } - /// - /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). - /// - public string ProfileQueryEndpoint { get; set; } - - internal string EffectiveProfileQueryEndpoint - { - get - { - return string.IsNullOrEmpty(this.ProfileQueryEndpoint) ? this.telemetryChannelEnpoint : this.ProfileQueryEndpoint; - } - } - /// /// Implements on begin callback of http module. /// @@ -201,13 +188,11 @@ public void OnEndRequest(HttpContext context) AppMapCorrelationEventSource.Log.GetCrossComponentCorrelationHeaderFailed(ex.ToInvariantString()); } - bool correlationIdLookupHelperInitialized = this.TryInitializeCorrelationHelperIfNotInitialized(); - string currentComponentAppId = string.Empty; bool foundMyAppId = false; - if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && correlationIdLookupHelperInitialized) + if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey)) { - foundMyAppId = this.correlationIdLookupHelper.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId); + foundMyAppId = CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId); } // If the source header is present on the incoming request, @@ -250,17 +235,14 @@ public void AddTargetHashForResponseHeader(HttpContext context) this.telemetryClient.Initialize(requestTelemetry); } - bool correlationIdHelperInitialized = this.TryInitializeCorrelationHelperIfNotInitialized(); - try { if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) - && context.Response.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey) == null - && correlationIdHelperInitialized) + && context.Response.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey) == null) { string correlationId; - if (this.correlationIdLookupHelper.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out correlationId)) + if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out correlationId)) { context.Response.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey, correlationId); @@ -325,15 +307,6 @@ internal bool NeedProcessRequest(HttpContext httpContext) return true; } - /// - /// Simple test hook, that allows for using a stub rather than the implementation that calls the original service. - /// - /// Lookup header to use. - internal void OverrideCorrelationIdLookupHelper(CorrelationIdLookupHelper correlationIdLookupHelper) - { - this.correlationIdLookupHelper = correlationIdLookupHelper; - } - /// /// Checks whether or not handler is a transfer handler. /// @@ -356,24 +329,7 @@ private bool IsHandlerToFilter(IHttpHandler handler) return false; } - - private bool TryInitializeCorrelationHelperIfNotInitialized() - { - try - { - if (this.correlationIdLookupHelper == null) - { - this.correlationIdLookupHelper = new CorrelationIdLookupHelper(this.EffectiveProfileQueryEndpoint); - } - - return true; - } - catch - { - return false; - } - } - + /// /// can create a Child request to route extension-less requests to a controller. /// (ex: site/home -> site/HomeController.cs) From a33ce436a14db1e4ff668ffe4fae92f169ff0794 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 20 Mar 2018 18:41:52 -0700 Subject: [PATCH 02/19] code cleanup --- .../DependencyCollectorDiagnosticListenerTests.Netstandard20.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs index b65a517a7..809b48e4a 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs @@ -38,7 +38,7 @@ public void OnActivityStartInjectsHeaders() // check only legacy headers here Assert.AreEqual(activity.RootId, request.Headers.GetValues(RequestResponseHeaders.StandardRootIdHeader).Single()); Assert.AreEqual(activity.Id, request.Headers.GetValues(RequestResponseHeaders.StandardParentIdHeader).Single()); - Assert.AreEqual("cid-v1:" + this.instrumentationKey + "-appId", GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); + Assert.AreEqual(this.correlationId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); } /// From 347a85f30577377d336d6aec79f3c11149418875 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 23 Mar 2018 08:36:16 -0700 Subject: [PATCH 03/19] update name of Config method --- .../CorrelationLookup/CorrelationIdLookupHelper.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs b/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs index 38f0f5b1a..34facb68f 100644 --- a/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs +++ b/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs @@ -25,8 +25,8 @@ internal class CorrelationIdLookupHelper : ICorrelationIdLookupHelper private const string CorrelationIdFormat = "cid-v1:{0}"; - private const string AppIdQueryApiRelativeUriFormat = "api/profiles/{0}/appId"; - private const string AppIdQueryApiFullUriFormat = "https://dc.services.visualstudio.com/api/profiles/{0}/appId"; + private const string AppIdServiceRelativeEndpoint = "api/profiles/{0}/appId"; + private const string AppIdServiceEndpoint = "https://dc.services.visualstudio.com/api/profiles/{0}/appId"; // We have arbitrarily chosen 5 second delay between trying to get app Id once we get a failure while trying to get it. // This is to throttle tries between failures to safeguard against performance hits. The impact would be that telemetry generated during this interval would not have x-component correlation id. @@ -239,15 +239,15 @@ private async Task FetchAppIdFromService(string instrumentationKey) private Uri GetAppIdEndPointUri(string instrumentationKey) { Uri endpointProxyBaseUri = null; - //// TODO: endpointProxyBaseUri = TelemetryConfiguration.Active.GetEndpointProxyBaseUri(); // MUST WAIT FOR CHANGES IN BASE SDK!!! + //// TODO: endpointProxyBaseUri = TelemetryConfiguration.Active.GetApplicationInsightsEndpointBaseUri(); // MUST WAIT FOR CHANGES IN BASE SDK!!! if (endpointProxyBaseUri != null) { - return new Uri(endpointProxyBaseUri, string.Format(CultureInfo.InvariantCulture, AppIdQueryApiRelativeUriFormat, instrumentationKey)); + return new Uri(endpointProxyBaseUri, string.Format(CultureInfo.InvariantCulture, AppIdServiceRelativeEndpoint, instrumentationKey)); } else { - return new Uri(string.Format(CultureInfo.InvariantCulture, AppIdQueryApiFullUriFormat, instrumentationKey)); + return new Uri(string.Format(CultureInfo.InvariantCulture, AppIdServiceEndpoint, instrumentationKey)); } } From a05aa2f3ff4dfd394af5988b49d8e274ff49be5c Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 2 Apr 2018 13:02:32 -0700 Subject: [PATCH 04/19] change to use new config.ApplicationIdProvider --- Src/Common/Common.projitems | 3 - .../CorrelationIdLookupHelper.cs | 367 ------------------ .../CorrelationIdLookupSingleton.cs | 45 --- .../ICorrelationIdLookupHelper.cs | 16 - .../CorrelationIdLookupHelperTests.cs | 82 ---- ...DependencyCollector.Shared.Tests.projitems | 1 - ...orDiagnosticListenerTests.Netstandard16.cs | 33 +- ...orDiagnosticListenerTests.Netstandard20.cs | 2 +- ...ktopDiagnosticSourceHttpProcessingTests.cs | 18 +- .../FrameworkHttpProcessingTest.cs | 17 +- .../ProfilerHttpProcessingTest.cs | 37 +- .../HttpCoreDiagnosticSourceListener.cs | 39 +- .../Shared/Implementation/HttpProcessing.cs | 48 +-- .../Shared/MockApplicationIdProvider.cs | 31 ++ .../Shared/MockCorrelationIdLookupHelper.cs | 22 -- .../Shared/TestFramework.Shared.projitems | 2 +- .../RequestTrackingTelemetryModuleTest.cs | 45 +-- .../RequestTrackingTelemetryModule.cs | 42 +- 18 files changed, 150 insertions(+), 700 deletions(-) delete mode 100644 Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs delete mode 100644 Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs delete mode 100644 Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs delete mode 100644 Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs create mode 100644 Src/TestFramework/Shared/MockApplicationIdProvider.cs delete mode 100644 Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs diff --git a/Src/Common/Common.projitems b/Src/Common/Common.projitems index 7d635d8fe..a5a0b3c2d 100644 --- a/Src/Common/Common.projitems +++ b/Src/Common/Common.projitems @@ -11,11 +11,8 @@ - - - diff --git a/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs b/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs deleted file mode 100644 index 34facb68f..000000000 --- a/Src/Common/CorrelationLookup/CorrelationIdLookupHelper.cs +++ /dev/null @@ -1,367 +0,0 @@ -namespace Microsoft.ApplicationInsights.Common.CorrelationLookup -{ - using System; - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using System.Net; -#if NETSTANDARD1_6 - using System.Net.Http; -#endif - using System.Threading.Tasks; - using Extensibility; - using Extensibility.Implementation.Tracing; - - /// - /// A store for instrumentation App Ids. This makes sure we don't query the public endpoint to find an app Id for the same instrumentation key more than once. - /// - internal class CorrelationIdLookupHelper : ICorrelationIdLookupHelper - { - /// - /// Max number of app ids to cache. - /// - private const int MAXSIZE = 100; - - private const string CorrelationIdFormat = "cid-v1:{0}"; - - private const string AppIdServiceRelativeEndpoint = "api/profiles/{0}/appId"; - private const string AppIdServiceEndpoint = "https://dc.services.visualstudio.com/api/profiles/{0}/appId"; - - // We have arbitrarily chosen 5 second delay between trying to get app Id once we get a failure while trying to get it. - // This is to throttle tries between failures to safeguard against performance hits. The impact would be that telemetry generated during this interval would not have x-component correlation id. - private readonly TimeSpan intervalBetweenFailedRetries = TimeSpan.FromSeconds(30); - - private ConcurrentDictionary knownCorrelationIds = new ConcurrentDictionary(); - - private ConcurrentDictionary fetchTasks = new ConcurrentDictionary(); - - // Stores failed instrumentation keys along with the time we tried to retrieve them. - private ConcurrentDictionary failingInstrumentationKeys = new ConcurrentDictionary(); - - private Func> provideAppId; - - /// - /// Initializes a new instance of the class. - /// - public CorrelationIdLookupHelper() - { - this.provideAppId = this.FetchAppIdFromService; - } - - /// - /// Unit Test Only! Initializes a new instance of the class with an override for fetching appId logic. - /// - /// The delegate to be called to fetch the appId. - internal CorrelationIdLookupHelper(Func> appIdProviderMethod) - { - this.provideAppId = appIdProviderMethod ?? throw new ArgumentNullException(nameof(appIdProviderMethod)); - } - - /// - /// Retrieves the correlation id corresponding to a given instrumentation key. - /// - /// Instrumentation key string. - /// AppId corresponding to the provided instrumentation key. - /// true if correlationId was successfully retrieved; false otherwise. - public bool TryGetXComponentCorrelationId(string instrumentationKey, out string correlationId) - { - if (string.IsNullOrEmpty(instrumentationKey)) - { - // This method cannot throw - it's a Try... method. We cannot proceed any further. - correlationId = string.Empty; - return false; - } - - var found = this.knownCorrelationIds.TryGetValue(instrumentationKey, out correlationId); - - if (found) - { - return true; - } - else - { - // Simplistic cleanup to guard against this becoming a memory hog. - if (this.knownCorrelationIds.Keys.Count >= MAXSIZE) - { - this.knownCorrelationIds.Clear(); - } - - try - { - FailedResult lastFailedResult; - if (this.failingInstrumentationKeys.TryGetValue(instrumentationKey, out lastFailedResult)) - { - if (!lastFailedResult.ShouldRetry || DateTime.UtcNow - lastFailedResult.FailureTime <= this.intervalBetweenFailedRetries) - { - // We tried not too long ago and failed to retrieve app Id for this instrumentation key from breeze. Let wait a while before we try again. For now just report failure. - correlationId = string.Empty; - return false; - } - } - - // We only want one task to be there to fetch the ikey. If initial requests come in a bunch, only one of them gets to take responsibility of creating the fetch task. Rest return. - if (this.fetchTasks.TryAdd(instrumentationKey, int.MinValue)) - { - this.provideAppId(instrumentationKey.ToLowerInvariant()) - .ContinueWith((appId) => - { - try - { - this.GenerateCorrelationIdAndAddToDictionary(instrumentationKey, appId.Result); - } - catch (Exception ex) - { - this.RegisterFailure(instrumentationKey, ex); - } - finally - { - this.fetchTasks.TryRemove(instrumentationKey, out int taskId); - } - }); - - return false; - } - else - { - // Fetch tasks are scheduled - don't queue a task. - correlationId = string.Empty; - return false; - } - } - catch (Exception ex) - { - this.RegisterFailure(instrumentationKey, ex); - - correlationId = string.Empty; - return false; - } - } - } - - /// - /// This method is purely a test helper at this point. It checks whether the task to get app ID is still running. - /// - /// True if fetch task is still in progress, false otherwise. - public bool IsFetchAppInProgress(string ikey) - { - int value; - return this.fetchTasks.TryGetValue(ikey, out value); - } - - /// - /// Format and store an iKey and appId pair into the dictionary of known correlation ids. - /// - /// Instrumentation Key is expected to be a Guid string. - /// Application Id is expected to be a Guid string. App Id needs to be Http Header safe, and all non-ASCII characters will be removed. - /// To protect against injection attacks, AppId will be truncated to a maximum length. - private void GenerateCorrelationIdAndAddToDictionary(string ikey, string appId) - { - // Arbitrary maximum length to guard against injections. - appId = StringUtilities.EnforceMaxLength(appId, InjectionGuardConstants.AppIdMaxLengeth); - - if (string.IsNullOrWhiteSpace(appId)) - { - return; - } - - appId = HeadersUtilities.SanitizeString(appId); - if (!string.IsNullOrEmpty(appId)) - { - this.knownCorrelationIds[ikey] = string.Format(CultureInfo.InvariantCulture, CorrelationIdFormat, appId); - } - } - - /// - /// Retrieves the app id given the instrumentation key. - /// - /// Instrumentation key for which app id is to be retrieved. - /// App id. - private async Task FetchAppIdFromService(string instrumentationKey) - { -#if NETSTANDARD1_6 - string result = null; - Uri appIdEndpoint = this.GetAppIdEndPointUri(instrumentationKey); - - using (HttpClient client = new HttpClient()) - { - var resultMessage = await client.GetAsync(appIdEndpoint).ConfigureAwait(false); - if (resultMessage.IsSuccessStatusCode) - { - result = await resultMessage.Content.ReadAsStringAsync().ConfigureAwait(false); - } - else - { - this.RegisterFetchFailure(instrumentationKey, resultMessage.StatusCode); - } - } - - return result; -#else - try - { - SdkInternalOperationsMonitor.Enter(); - - string result = null; - Uri appIdEndpoint = this.GetAppIdEndPointUri(instrumentationKey); - WebRequest request = WebRequest.Create(appIdEndpoint); - request.Method = "GET"; - - using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync().ConfigureAwait(false)) - { - if (response.StatusCode == HttpStatusCode.OK) - { - using (StreamReader reader = new StreamReader(response.GetResponseStream())) - { - result = await reader.ReadToEndAsync(); - } - } - else - { - this.RegisterFetchFailure(instrumentationKey, response.StatusCode); - } - } - - return result; - } - finally - { - SdkInternalOperationsMonitor.Exit(); - } -#endif - } - - /// - /// Strips off any relative path at the end of the base URI and then appends the known relative path to get the app id uri. - /// - /// AI resource's instrumentation key. - /// Computed Uri. - private Uri GetAppIdEndPointUri(string instrumentationKey) - { - Uri endpointProxyBaseUri = null; - //// TODO: endpointProxyBaseUri = TelemetryConfiguration.Active.GetApplicationInsightsEndpointBaseUri(); // MUST WAIT FOR CHANGES IN BASE SDK!!! - - if (endpointProxyBaseUri != null) - { - return new Uri(endpointProxyBaseUri, string.Format(CultureInfo.InvariantCulture, AppIdServiceRelativeEndpoint, instrumentationKey)); - } - else - { - return new Uri(string.Format(CultureInfo.InvariantCulture, AppIdServiceEndpoint, instrumentationKey)); - } - } - - /// - /// Registers failure for further action in future. - /// - /// Instrumentation key for which the failure occurred. - /// Exception indicating failure. - private void RegisterFailure(string instrumentationKey, Exception ex) - { -#if !NETSTANDARD1_6 - var ae = ex as AggregateException; - - if (ae != null) - { - ae = ae.Flatten(); - if (ae.InnerException != null) - { - this.RegisterFailure(instrumentationKey, ae.InnerException); - return; - } - } - - var webException = ex as WebException; - if (webException != null && webException.Response != null) - { - this.failingInstrumentationKeys[instrumentationKey] = new FailedResult( - DateTime.UtcNow, - ((HttpWebResponse)webException.Response).StatusCode); - } - else - { -#endif - this.failingInstrumentationKeys[instrumentationKey] = new FailedResult(DateTime.UtcNow); -#if !NETSTANDARD1_6 - } -#endif - - AppMapCorrelationEventSource.Log.FetchAppIdFailed(this.GetExceptionDetailString(ex)); - } - - /// - /// FetchAppIdFromService failed. - /// Registers failure for further action in future. - /// - /// Instrumentation key for which the failure occurred. - /// Response code from AppId Endpoint. - private void RegisterFetchFailure(string instrumentationKey, HttpStatusCode httpStatusCode) - { - this.failingInstrumentationKeys[instrumentationKey] = new FailedResult(DateTime.UtcNow, httpStatusCode); - - AppMapCorrelationEventSource.Log.FetchAppIdFailedWithResponseCode(httpStatusCode.ToString()); - } - - private string GetExceptionDetailString(Exception ex) - { - var ae = ex as AggregateException; - if (ae != null) - { - return ae.Flatten().InnerException.ToInvariantString(); - } - - return ex.ToInvariantString(); - } - - /// - /// Structure that represents a failed fetch app Id call. - /// - private class FailedResult - { - /// - /// Initializes a new instance of the class. - /// - /// Time when the failure occurred. - /// Failure response code. - public FailedResult(DateTime failureTime, HttpStatusCode failureCode) - { - this.FailureTime = failureTime; - this.FailureCode = (int)failureCode; - } - - /// - /// Initializes a new instance of the class. - /// - /// Time when the failure occurred. - public FailedResult(DateTime failureTime) - { - this.FailureTime = failureTime; - - // Unknown failure code. - this.FailureCode = int.MinValue; - } - - /// - /// Gets the time of failure. - /// - public DateTime FailureTime { get; private set; } - - /// - /// Gets the integer value for response code representing the type of failure. - /// - public int FailureCode { get; private set; } - - /// - /// Gets a value indicating whether the failure is likely to go away when a retry happens. - /// - public bool ShouldRetry - { - get - { - // If not in the 400 range. - return !(this.FailureCode >= 400 && this.FailureCode < 500); - } - } - } - } -} diff --git a/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs b/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs deleted file mode 100644 index 69e33a8b9..000000000 --- a/Src/Common/CorrelationLookup/CorrelationIdLookupSingleton.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace Microsoft.ApplicationInsights.Common.CorrelationLookup -{ - /// - /// This class holds a single instance of CorrelationIdLookupHelper for the entire application. - /// This is a workaround because we don't use dependency injection. - /// This instance can be overwritten by UnitTests. - /// - internal static class CorrelationIdLookupSingleton - { - private static object semaphore = new object(); - private static ICorrelationIdLookupHelper instance; - - /// - /// Gets or sets the instance of ICorrelationIdLookupHelper to be used by Modules. - /// Modules should not copy this locally and should always refer to this Instance. - /// Set will be used by Unit Tests. - /// - public static ICorrelationIdLookupHelper Instance - { - get - { - if (instance == null) - { - lock (semaphore) - { - if (instance == null) - { - instance = new CorrelationIdLookupHelper(); - } - } - } - - return instance; - } - - set - { - lock (semaphore) - { - instance = value; - } - } - } - } -} diff --git a/Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs b/Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs deleted file mode 100644 index 081d81ddd..000000000 --- a/Src/Common/CorrelationLookup/ICorrelationIdLookupHelper.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Microsoft.ApplicationInsights.Common.CorrelationLookup -{ - /// - /// An interface for getting a correlation id from a provided instrumentation key. - /// - internal interface ICorrelationIdLookupHelper - { - /// - /// Retrieves the correlation id corresponding to a given instrumentation key. - /// - /// Instrumentation key string. - /// AppId corresponding to the provided instrumentation key. - /// true if correlationId was successfully retrieved; false otherwise. - bool TryGetXComponentCorrelationId(string instrumentationKey, out string correlationId); - } -} diff --git a/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs b/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs deleted file mode 100644 index 78aeaa5b7..000000000 --- a/Src/DependencyCollector/Shared.Tests/CorrelationIdLookupHelperTests.cs +++ /dev/null @@ -1,82 +0,0 @@ -namespace Microsoft.ApplicationInsights.Tests -{ - using System; - using System.Collections.Generic; - using System.Text; - using System.Threading; - using System.Threading.Tasks; - using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - /// - /// Correlation Id Lookup helper tests. - /// - [TestClass] - public sealed class CorrelationIdLookupHelperTests - { - /// - /// Makes sure that the first call to get app id returns false, because it hasn't been fetched yet. - /// But the second call is able to get it from the dictionary. - /// - [TestMethod] - public void CorrelationIdLookupHelperReturnsAppIdOnSecondCall() - { - var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) => - { - // Pretend App Id is the same as Ikey - return Task.FromResult(ikey); - }); - - string instrumenationKey = Guid.NewGuid().ToString(); - string cid; - - // First call returns false; - Assert.IsFalse(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out cid)); - - // Let's wait for the task to complete. It should be really quick (based on the test setup) but not immediate. - while (correlationIdLookupHelper.IsFetchAppInProgress(instrumenationKey)) - { - Thread.Sleep(10); // wait 10 ms. - } - - // Once fetch is complete, subsequent calls should return correlation id. - Assert.IsTrue(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out cid)); - } - - /// - /// Test that if an malicious value is returned, that value will be truncated. - /// - [TestMethod] - public void CorrelationIdLookupHelperTruncatesMaliciousValue() - { - // 50 character string. - var value = "a123456789b123546789c123456789d123456798e123456789"; - - // An arbitrary string that is expected to be truncated. - var malicious = "00000000000000000000000000000000000000000000000000000000000"; - - var cidPrefix = "cid-v1:"; - - var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) => - { - return Task.FromResult(value + malicious); - }); - - string instrumenationKey = Guid.NewGuid().ToString(); - - // first request fails because this will create the fetch task. - Assert.IsFalse(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out string ignore)); - - // Let's wait for the task to complete. It should be really quick (based on the test setup) but not immediate. - while (correlationIdLookupHelper.IsFetchAppInProgress(instrumenationKey)) - { - Thread.Sleep(10); // wait 10 ms. - } - - // Once fetch is complete, subsequent calls should return correlation id. - Assert.IsTrue(correlationIdLookupHelper.TryGetXComponentCorrelationId(instrumenationKey, out string cid)); - Assert.AreEqual(cidPrefix + value, cid); - } - } -} diff --git a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems index e2167258c..c5a67ee9d 100644 --- a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems +++ b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems @@ -9,7 +9,6 @@ Microsoft.ApplicationInsights.DependencyCollector - diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs index 0cd863ac0..ad6622532 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs @@ -9,7 +9,6 @@ namespace Microsoft.ApplicationInsights.Tests using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -29,13 +28,15 @@ public partial class DependencyCollectorDiagnosticListenerTests private const string RequestUrlWithScheme = "https://" + RequestUrl; private const string HttpOkResultCode = "200"; private const string NotFoundResultCode = "404"; - private const string MockAppId = "MOCK_APP_ID"; - private const string MockAppId2 = "MOCK_APP_ID_2"; + //private const string MockAppId = "MOCK_APP_ID"; + //private const string MockAppId2 = "MOCK_APP_ID_2"; private readonly List sentTelemetry = new List(); - private string instrumentationKey; - private string correlationId; + private string testInstrumentationKey1 = nameof(testInstrumentationKey1); + private string testInstrumentationKey2 = nameof(testInstrumentationKey2); + private string testApplicationId1 = nameof(testApplicationId1); + private string testApplicationId2 = nameof(testApplicationId2); private StubTelemetryChannel telemetryChannel; private HttpCoreDiagnosticSourceListener listener; @@ -45,21 +46,19 @@ public partial class DependencyCollectorDiagnosticListenerTests [TestInitialize] public void Initialize() { - this.instrumentationKey = Guid.NewGuid().ToString(); - this.correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(this.instrumentationKey); - this.telemetryChannel = new StubTelemetryChannel() { EndpointAddress = "https://endpointaddress", OnSend = this.sentTelemetry.Add }; - CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); + this.testInstrumentationKey1 = Guid.NewGuid().ToString(); var configuration = new TelemetryConfiguration { TelemetryChannel = this.telemetryChannel, - InstrumentationKey = this.instrumentationKey, + InstrumentationKey = this.testInstrumentationKey1, + ApplicationIdProvider = new MockApplicationIdProvider(this.testInstrumentationKey1, this.testApplicationId1) }; configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); @@ -209,7 +208,7 @@ public void OnRequestWithRequestEvent() Assert.AreEqual(string.Empty, telemetry.ResultCode); Assert.AreEqual(true, telemetry.Success); - Assert.AreEqual(this.correlationId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); + Assert.AreEqual(this.testApplicationId1, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); Assert.AreEqual(null, GetRequestContextKeyValue(request, RequestResponseHeaders.StandardRootIdHeader)); var legacyParentIdHeader = GetRequestHeaderValues(request, RequestResponseHeaders.StandardParentIdHeader).Single(); @@ -325,14 +324,14 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndSameTarge DependencyTelemetry telemetry = dependency.Telemetry; Assert.AreEqual(string.Empty, telemetry.ResultCode); - Assert.AreEqual(true, telemetry.Success); + Assert.AreEqual(true, telemetry.Success, "request was not successful"); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = request }; - response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, MockAppId); + response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, testApplicationId1); this.listener.OnResponse(response, loggingRequestId); Assert.IsFalse(this.listener.PendingDependencyTelemetry.TryGetValue(request, out dependency)); @@ -342,7 +341,7 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndSameTarge Assert.AreEqual(RemoteDependencyConstants.HTTP, telemetry.Type); Assert.AreEqual(RequestUrl, telemetry.Target); Assert.AreEqual(HttpOkResultCode, telemetry.ResultCode); - Assert.AreEqual(true, telemetry.Success); + Assert.AreEqual(true, telemetry.Success, "response was not successful"); } /// @@ -367,7 +366,7 @@ public void OnResponseWithFailedResponseEventWithMatchingRequestAndSameTargetIns RequestMessage = request }; - response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, MockAppId); + response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, testApplicationId1); this.listener.OnResponse(response, loggingRequestId); Assert.IsFalse(this.listener.PendingDependencyTelemetry.TryGetValue(request, out dependency)); @@ -402,7 +401,7 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndDifferent RequestMessage = request }; - string targetApplicationId = MockAppId2; + string targetApplicationId = testApplicationId2; HttpHeadersUtilities.SetRequestContextKeyValue(response.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey, targetApplicationId); this.listener.OnResponse(response, loggingRequestId); @@ -438,7 +437,7 @@ public void OnResponseWithFailedResponseEventWithMatchingRequestAndDifferentTarg RequestMessage = request }; - string targetApplicationId = MockAppId2; + string targetApplicationId = testApplicationId2; HttpHeadersUtilities.SetRequestContextKeyValue(response.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey, targetApplicationId); this.listener.OnResponse(response, loggingRequestId); diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs index 809b48e4a..94ac25612 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs @@ -38,7 +38,7 @@ public void OnActivityStartInjectsHeaders() // check only legacy headers here Assert.AreEqual(activity.RootId, request.Headers.GetValues(RequestResponseHeaders.StandardRootIdHeader).Single()); Assert.AreEqual(activity.Id, request.Headers.GetValues(RequestResponseHeaders.StandardParentIdHeader).Single()); - Assert.AreEqual(this.correlationId, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); + Assert.AreEqual(this.testApplicationId1, GetRequestContextKeyValue(request, RequestResponseHeaders.RequestContextCorrelationSourceKey)); } /// diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs index 11b2d0640..fa775d68b 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs @@ -10,7 +10,6 @@ namespace Microsoft.ApplicationInsights.Tests using System.Threading; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -29,8 +28,10 @@ public class DesktopDiagnosticSourceHttpProcessingTests private Uri testUrl = new Uri("http://www.microsoft.com/"); private int sleepTimeMsecBetweenBeginAndEnd = 100; private TelemetryConfiguration configuration; - private List sendItems; + private List sendItems = new List(); private DesktopDiagnosticSourceHttpProcessing httpDesktopProcessingFramework; + private const string testInstrumentationKey = nameof(testInstrumentationKey); + private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -38,14 +39,15 @@ public class DesktopDiagnosticSourceHttpProcessingTests [TestInitialize] public void TestInitialize() { - this.configuration = new TelemetryConfiguration(); - this.sendItems = new List(); - this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; - this.configuration.InstrumentationKey = Guid.NewGuid().ToString(); + this.configuration = new TelemetryConfiguration() + { + TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, + InstrumentationKey = testInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + }; + this.httpDesktopProcessingFramework = new DesktopDiagnosticSourceHttpProcessing(this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), /*setCorrelationHeaders*/ true, new List()); DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false; - - CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); } [TestCleanup] diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs index acf621818..61686bfaa 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs @@ -10,7 +10,6 @@ using System.Threading.Tasks; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -31,9 +30,11 @@ public sealed class FrameworkHttpProcessingTest : IDisposable private Uri testUrlNonStandardPort = new Uri("http://www.microsoft.com:911/"); private int sleepTimeMsecBetweenBeginAndEnd = 100; private TelemetryConfiguration configuration; - private List sendItems; + private List sendItems = new List(); private FrameworkHttpProcessing httpProcessingFramework; private CacheBasedOperationHolder cache = new CacheBasedOperationHolder("testCache", 100 * 1000); + private const string testInstrumentationKey = nameof(testInstrumentationKey); + private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -41,14 +42,14 @@ public sealed class FrameworkHttpProcessingTest : IDisposable [TestInitialize] public void TestInitialize() { - this.configuration = new TelemetryConfiguration(); - this.sendItems = new List(); - this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; - this.configuration.InstrumentationKey = Guid.NewGuid().ToString(); + this.configuration = new TelemetryConfiguration() + { + TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, + InstrumentationKey = testInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + }; this.httpProcessingFramework = new FrameworkHttpProcessing(this.configuration, this.cache, /*setCorrelationHeaders*/ true, new List()); DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false; - - CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); } [TestCleanup] diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs index 6a4416d79..aa61ebc34 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs @@ -15,7 +15,6 @@ using Common; using Microsoft.ApplicationInsights.Channel; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; @@ -36,10 +35,12 @@ public sealed class ProfilerHttpProcessingTest : IDisposable private TelemetryConfiguration configuration; private Uri testUrl = new Uri("http://www.microsoft.com/"); private Uri testUrlNonStandardPort = new Uri("http://www.microsoft.com:911/"); - private List sendItems; + private List sendItems = new List(); private int sleepTimeMsecBetweenBeginAndEnd = 100; - private Exception ex; + private Exception ex = new Exception(); private ProfilerHttpProcessing httpProcessingProfiler; + private const string testInstrumentationKey = nameof(testInstrumentationKey); + private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -47,23 +48,20 @@ public sealed class ProfilerHttpProcessingTest : IDisposable [TestInitialize] public void TestInitialize() { - var instrumentationKey = Guid.NewGuid().ToString(); + this.configuration = new TelemetryConfiguration() + { + TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, + InstrumentationKey = testInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + }; - this.configuration = new TelemetryConfiguration(); this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); - this.sendItems = new List(); - this.configuration.TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }; - this.configuration.InstrumentationKey = instrumentationKey; this.httpProcessingProfiler = new ProfilerHttpProcessing( this.configuration, null, new ObjectInstanceBasedOperationHolder(), true /*setCorrelationHeaders*/, new List()); - - CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); - - this.ex = new Exception(); } [TestCleanup] @@ -132,7 +130,7 @@ public void RddTestHttpProcessingProfilerOnEndAddsAppIdToTargetField() this.SimulateWebRequestResponseWithAppId(appId); Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent"); - Assert.AreEqual(this.testUrl.Host + " | " + this.GetCorrelationIdValue(appId), ((DependencyTelemetry)this.sendItems[0]).Target); + Assert.AreEqual(this.testUrl.Host + " | " + appId, ((DependencyTelemetry)this.sendItems[0]).Target); } /// @@ -142,9 +140,7 @@ public void RddTestHttpProcessingProfilerOnEndAddsAppIdToTargetField() [Description("Validates DependencyTelemetry does not send correlation ID if the IKey is from the same component")] public void RddTestHttpProcessingProfilerOnEndDoesNotAddAppIdToTargetFieldForInternalComponents() { - string appId = this.configuration.InstrumentationKey + "-appId"; - - this.SimulateWebRequestResponseWithAppId(appId); + this.SimulateWebRequestResponseWithAppId(testApplicationId); Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent"); @@ -884,15 +880,10 @@ private void SimulateWebRequestWithGivenRequestContextHeaderValue(string headerV this.httpProcessingProfiler.OnBeginForGetResponse(request); var objectReturned = this.httpProcessingProfiler.OnEndForGetResponse(null, returnObjectPassed, request); } - - private string GetCorrelationIdValue(string appId) - { - return string.Format(CultureInfo.InvariantCulture, "cid-v1:{0}", appId); - } - + private string GetCorrelationIdHeaderValue(string appId) { - return string.Format(CultureInfo.InvariantCulture, "{0}=cid-v1:{1}", RequestResponseHeaders.RequestContextCorrelationTargetKey, appId); + return string.Format(CultureInfo.InvariantCulture, "{0}={1}", RequestResponseHeaders.RequestContextCorrelationTargetKey, appId); } #endregion Helpers } diff --git a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs index a06952238..bb636d7be 100644 --- a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs +++ b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs @@ -12,7 +12,6 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; @@ -131,7 +130,6 @@ public void OnNext(KeyValuePair evnt) var response = this.stopResponseFetcher.Fetch(evnt.Value) as HttpResponseMessage; var request = this.stopRequestFetcher.Fetch(evnt.Value) as HttpRequestMessage; var requestTaskStatusString = this.stopRequestStatusFetcher.Fetch(evnt.Value).ToString(); - TaskStatus requestTaskStatus; if (response == null) { @@ -143,7 +141,7 @@ public void OnNext(KeyValuePair evnt) var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateTypeCast, evnt.Key, "request", "HttpRequestMessage"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); } - else if (!Enum.TryParse(requestTaskStatusString, out requestTaskStatus)) + else if (!Enum.TryParse(requestTaskStatusString, out TaskStatus requestTaskStatus)) { var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateValueParse, evnt.Key, requestTaskStatusString, "TaskStatus"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); @@ -189,14 +187,13 @@ public void OnNext(KeyValuePair evnt) var request = this.deprecatedRequestFetcher.Fetch(evnt.Value) as HttpRequestMessage; var loggingRequestIdString = this.deprecatedRequestGuidFetcher.Fetch(evnt.Value).ToString(); - Guid loggingRequestId; if (request == null) { var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateTypeCast, evnt.Key, "request", "HttpRequestMessage"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); } - else if (!Guid.TryParse(loggingRequestIdString, out loggingRequestId)) + else if (!Guid.TryParse(loggingRequestIdString, out Guid loggingRequestId)) { var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateValueParse, evnt.Key, loggingRequestIdString, "Guid"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); @@ -219,14 +216,13 @@ public void OnNext(KeyValuePair evnt) var response = this.deprecatedResponseFetcher.Fetch(evnt.Value) as HttpResponseMessage; var loggingRequestIdString = this.deprecatedResponseGuidFetcher.Fetch(evnt.Value).ToString(); - Guid loggingRequestId; if (response == null) { var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateTypeCast, evnt.Key, "response", "HttpResponseMessage"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); } - else if (!Guid.TryParse(loggingRequestIdString, out loggingRequestId)) + else if (!Guid.TryParse(loggingRequestIdString, out Guid loggingRequestId)) { var error = string.Format(CultureInfo.InvariantCulture, ErrorTemplateValueParse, evnt.Key, loggingRequestIdString, "Guid"); DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerOnNextFailed(error); @@ -362,8 +358,7 @@ internal void OnActivityStop(HttpResponseMessage response, HttpRequestMessage re } else { - Exception exception; - if (this.pendingExceptions.TryRemove(currentActivity.Id, out exception)) + if (this.pendingExceptions.TryRemove(currentActivity.Id, out Exception exception)) { telemetry.Context.Properties[DependencyErrorPropertyKey] = exception.GetBaseException().Message; } @@ -412,8 +407,7 @@ internal void OnResponse(HttpResponseMessage response, Guid loggingRequestId) { DependencyCollectorEventSource.Log.HttpCoreDiagnosticSourceListenerResponse(loggingRequestId); var request = response.RequestMessage; - IOperationHolder dependency; - if (request != null && this.pendingTelemetry.TryGetValue(request, out dependency)) + if (request != null && this.pendingTelemetry.TryGetValue(request, out IOperationHolder dependency)) { this.ParseResponse(response, dependency.Telemetry); this.client.StopOperation(dependency); @@ -433,13 +427,12 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta { try { - if (!string.IsNullOrEmpty(instrumentationKey) && !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey)) + string sourceApplicationId = null; + if (!string.IsNullOrEmpty(instrumentationKey) + && !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey) + && (configuration.ApplicationIdProvider?.TryGetApplicationId(instrumentationKey, out sourceApplicationId) ?? false)) { - string sourceApplicationId; - if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(instrumentationKey, out sourceApplicationId)) - { - HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId); - } + HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId); } } catch (Exception e) @@ -501,12 +494,14 @@ private void ParseResponse(HttpResponseMessage response, DependencyTelemetry tel if (!string.IsNullOrEmpty(targetApplicationId) && !string.IsNullOrEmpty(telemetry.Context.InstrumentationKey)) { // We only add the cross component correlation key if the key does not represent the current component. - string sourceApplicationId; - if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) && - targetApplicationId != sourceApplicationId) + string sourceApplicationId = null; + if (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) ?? false) { - telemetry.Type = RemoteDependencyConstants.AI; - telemetry.Target += " | " + targetApplicationId; + if (targetApplicationId != sourceApplicationId) + { + telemetry.Type = RemoteDependencyConstants.AI; + telemetry.Target += " | " + targetApplicationId; + } } } } diff --git a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs index b8819e2c7..aae2f0d48 100644 --- a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs @@ -9,7 +9,6 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using System.Web; using Extensibility.Implementation.Tracing; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector.Implementation.Operation; using Microsoft.ApplicationInsights.Extensibility; @@ -25,25 +24,18 @@ internal abstract class HttpProcessing private readonly ApplicationInsightsUrlFilter applicationInsightsUrlFilter; private ICollection correlationDomainExclusionList; private bool setCorrelationHeaders; + private readonly TelemetryConfiguration configuration; /// /// Initializes a new instance of the class. /// public HttpProcessing(TelemetryConfiguration configuration, string sdkVersion, string agentVersion, bool setCorrelationHeaders, ICollection correlationDomainExclusionList) { - if (configuration == null) - { - throw new ArgumentNullException("configuration"); - } - - if (correlationDomainExclusionList == null) - { - throw new ArgumentNullException("correlationDomainExclusionList"); - } - + this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this.applicationInsightsUrlFilter = new ApplicationInsightsUrlFilter(configuration); this.telemetryClient = new TelemetryClient(configuration); - this.correlationDomainExclusionList = correlationDomainExclusionList; + + this.correlationDomainExclusionList = correlationDomainExclusionList ?? throw new ArgumentNullException(nameof(correlationDomainExclusionList)); this.setCorrelationHeaders = setCorrelationHeaders; this.telemetryClient.Context.GetInternalContext().SdkVersion = sdkVersion; @@ -153,19 +145,16 @@ internal object OnBegin(object thisObj, bool injectCorrelationHeaders = true) telemetry.Data = url.OriginalString; // Add the source instrumentation key header if collection is enabled, the request host is not in the excluded list and the same header doesn't already exist - if (this.setCorrelationHeaders - && !this.correlationDomainExclusionList.Contains(url.Host)) + if (this.setCorrelationHeaders && !this.correlationDomainExclusionList.Contains(url.Host)) { try { + string applicationId = null; if (!string.IsNullOrEmpty(telemetry.Context.InstrumentationKey) - && webRequest.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey) == null) + && webRequest.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey) == null + && (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out applicationId) ?? false)) { - string appId; - if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out appId)) - { - webRequest.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey, appId); - } + webRequest.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey, applicationId); } } catch (Exception ex) @@ -228,11 +217,9 @@ internal void OnEndResponse(object request, object response) { try { - DependencyTelemetry telemetry; - if (this.TryGetPendingTelemetry(request, out telemetry)) + if (this.TryGetPendingTelemetry(request, out DependencyTelemetry telemetry)) { - var responseObj = response as HttpWebResponse; - if (responseObj != null) + if (response is HttpWebResponse responseObj) { int statusCode = -1; @@ -268,13 +255,11 @@ internal void OnEndException(object exception, object request) { try { - DependencyTelemetry telemetry; - if (this.TryGetPendingTelemetry(request, out telemetry)) + if (this.TryGetPendingTelemetry(request, out DependencyTelemetry telemetry)) { var webException = exception as WebException; - HttpWebResponse responseObj = webException?.Response as HttpWebResponse; - if (responseObj != null) + if (webException?.Response is HttpWebResponse responseObj) { int statusCode = -1; @@ -320,8 +305,7 @@ internal void OnEndResponse(object request, object statusCode, object responseHe { try { - DependencyTelemetry telemetry; - if (this.TryGetPendingTelemetry(request, out telemetry)) + if (this.TryGetPendingTelemetry(request, out DependencyTelemetry telemetry)) { if (statusCode != null) { @@ -420,8 +404,8 @@ private void SetTarget(DependencyTelemetry telemetry, WebHeaderCollection respon AppMapCorrelationEventSource.Log.GetCrossComponentCorrelationHeaderFailed(ex.ToInvariantString()); } - string currentComponentAppId; - if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(telemetry.Context.InstrumentationKey, out currentComponentAppId)) + string currentComponentAppId = null; + if (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false) { // We only add the cross component correlation key if the key does not remain the current component. if (!string.IsNullOrEmpty(targetAppId) && targetAppId != currentComponentAppId) diff --git a/Src/TestFramework/Shared/MockApplicationIdProvider.cs b/Src/TestFramework/Shared/MockApplicationIdProvider.cs new file mode 100644 index 000000000..e8f1dc3f5 --- /dev/null +++ b/Src/TestFramework/Shared/MockApplicationIdProvider.cs @@ -0,0 +1,31 @@ +namespace Microsoft.ApplicationInsights.Web.TestFramework +{ + using Microsoft.ApplicationInsights.Extensibility; + + /// + /// Use this to have TryGet return the instrumentation key as the correlation id. + /// + internal class MockApplicationIdProvider : IApplicationIdProvider + { + private readonly string expectedInstrumentationKey; + private readonly string applicationId; + + public MockApplicationIdProvider(string expectedInstrumentationKey, string applicationId) + { + this.expectedInstrumentationKey = expectedInstrumentationKey; + this.applicationId = applicationId; + } + + public bool TryGetApplicationId(string instrumentationKey, out string applicationId) + { + if(this.expectedInstrumentationKey == instrumentationKey) + { + applicationId = this.applicationId; + return true; + } + + applicationId = null; + return false; + } + } +} diff --git a/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs b/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs deleted file mode 100644 index 3f2dfafd7..000000000 --- a/Src/TestFramework/Shared/MockCorrelationIdLookupHelper.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace Microsoft.ApplicationInsights.Web.TestFramework -{ - using System.Globalization; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; - - /// - /// Use this to have TryGet return the instrumentation key as the correlation id. - /// - internal class MockCorrelationIdLookupHelper : ICorrelationIdLookupHelper - { - public static string GetCorrelationIdValue(string instrumentationKey) - { - return string.Format(CultureInfo.InvariantCulture, "cid-v1:{0}-appId", instrumentationKey); - } - - public bool TryGetXComponentCorrelationId(string instrumentationKey, out string correlationId) - { - correlationId = GetCorrelationIdValue(instrumentationKey); - return true; - } - } -} diff --git a/Src/TestFramework/Shared/TestFramework.Shared.projitems b/Src/TestFramework/Shared/TestFramework.Shared.projitems index bd39371d2..a8c2bd79b 100644 --- a/Src/TestFramework/Shared/TestFramework.Shared.projitems +++ b/Src/TestFramework/Shared/TestFramework.Shared.projitems @@ -10,7 +10,7 @@ - + diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index dfdfc2795..1a6cd64fe 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -9,7 +9,6 @@ using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.TestFramework; @@ -26,10 +25,15 @@ [TestClass] public partial class RequestTrackingTelemetryModuleTest { + private const string testInstrumentationKey1 = nameof(testInstrumentationKey1); + private const string testInstrumentationKey2 = nameof(testInstrumentationKey2); + private const string testApplicationId1 = nameof(testApplicationId1); + private const string testApplicationId2 = nameof(testApplicationId2); + [TestInitialize] public void TestInitialize() { - CorrelationIdLookupSingleton.Instance = new MockCorrelationIdLookupHelper(); + //this.configuration.ApplicationIdProvider = new MockApplicationIdProvider(); } [TestCleanup] @@ -323,15 +327,13 @@ public void SdkVersionHasCorrectFormat() public void OnEndDoesNotAddSourceFieldForRequestForSameComponent() { // ARRANGE - string ikey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf"; - string requestContextContainingCorrelationId = this.GetCorrelationIdHeaderValue(ikey); // since per our mock appId = ikey - Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, requestContextContainingCorrelationId); + headers.Add(RequestResponseHeaders.RequestContextHeader, testApplicationId2); var context = HttpModuleHelper.GetFakeHttpContext(headers); - var config = this.CreateDefaultConfig(context, instrumentationKey: ikey); + var config = this.CreateDefaultConfig(context, instrumentationKey: testInstrumentationKey1); + config.ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey1, testApplicationId1); var module = this.RequestTrackingTelemetryModuleFactory(config); // ACT @@ -346,17 +348,15 @@ public void OnEndDoesNotAddSourceFieldForRequestForSameComponent() public void OnEndAddsSourceFieldForRequestWithCorrelationId() { // ARRANGE - string instrumentationKey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf"; - string correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(instrumentationKey); - Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(instrumentationKey)); + headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(testApplicationId2)); var context = HttpModuleHelper.GetFakeHttpContext(headers); // My instrumentation key and hence app id is random / newly generated. The appId header is different - hence a different component. var config = TelemetryConfiguration.CreateDefault(); - config.InstrumentationKey = Guid.NewGuid().ToString(); + config.InstrumentationKey = testInstrumentationKey1; + config.ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey1, testApplicationId1); var module = this.RequestTrackingTelemetryModuleFactory(null /*use default*/); @@ -366,7 +366,7 @@ public void OnEndAddsSourceFieldForRequestWithCorrelationId() module.OnEndRequest(context); // VALIDATE - Assert.Equal(correlationId, context.GetRequestTelemetry().Source); + Assert.Equal(testApplicationId2, context.GetRequestTelemetry().Source); } [TestMethod] @@ -395,26 +395,20 @@ public void OnEndDoesNotAddSourceFieldForRequestWithOutSourceIkeyHeader() public void OnEndDoesNotOverrideSourceField() { // ARRANGE - string instrumentationKey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf"; - - string appIdInHeader = this.GetCorrelationIdHeaderValue(instrumentationKey); - string someFieldHeader = "SomeField=SomeNameHere"; - string appIdInSourceField = "9AB8EDCB-21D2-44BB-A64A-C33BB4515F20"; - Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, appIdInHeader + "," + someFieldHeader); + headers.Add(RequestResponseHeaders.RequestContextHeader, testApplicationId1); var context = HttpModuleHelper.GetFakeHttpContext(headers); var module = this.RequestTrackingTelemetryModuleFactory(); module.OnBeginRequest(context); - context.GetRequestTelemetry().Source = appIdInSourceField; + context.GetRequestTelemetry().Source = testApplicationId2; // ACT module.OnEndRequest(context); // VALIDATE - Assert.Equal(appIdInSourceField, context.GetRequestTelemetry().Source); + Assert.Equal(testApplicationId2, context.GetRequestTelemetry().Source); } private TelemetryConfiguration CreateDefaultConfig(HttpContext fakeContext, string rootIdHeaderName = null, string parentIdHeaderName = null, string instrumentationKey = null) @@ -452,11 +446,10 @@ private RequestTrackingTelemetryModule RequestTrackingTelemetryModuleFactory(Tel module.Initialize(config ?? this.CreateDefaultConfig(HttpModuleHelper.GetFakeHttpContext())); return module; } - - private string GetCorrelationIdHeaderValue(string ikey) + + private string GetCorrelationIdHeaderValue(string applicationId) { - var correlationId = MockCorrelationIdLookupHelper.GetCorrelationIdValue(ikey); - return string.Format(CultureInfo.InvariantCulture, "{0}={1}", RequestResponseHeaders.RequestContextCorrelationSourceKey, correlationId); + return string.Format(CultureInfo.InvariantCulture, "{0}={1}", RequestResponseHeaders.RequestContextCorrelationSourceKey, applicationId); } internal class FakeHttpHandler : IHttpHandler diff --git a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs index 479a50262..71741e555 100644 --- a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs +++ b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs @@ -8,7 +8,6 @@ using Extensibility.Implementation.Tracing; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.Common.CorrelationLookup; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Web.Implementation; @@ -20,9 +19,9 @@ public class RequestTrackingTelemetryModule : ITelemetryModule { private readonly IList handlersToFilter = new List(); private TelemetryClient telemetryClient; + private TelemetryConfiguration telemetryConfiguration; private bool initializationErrorReported; private bool correlationHeadersEnabled = true; - private string telemetryChannelEnpoint; private ChildRequestTrackingSuppressionModule childRequestTrackingSuppressionModule = null; /// @@ -187,22 +186,18 @@ public void OnEndRequest(HttpContext context) { AppMapCorrelationEventSource.Log.GetCrossComponentCorrelationHeaderFailed(ex.ToInvariantString()); } - - string currentComponentAppId = string.Empty; - bool foundMyAppId = false; - if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey)) - { - foundMyAppId = CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId); - } - // If the source header is present on the incoming request, - // and it is an external component (not the same ikey as the one used by the current component), - // then populate the source field. - if (!string.IsNullOrEmpty(sourceAppId) - && foundMyAppId - && sourceAppId != currentComponentAppId) + string currentComponentAppId = null; + if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) + && (telemetryConfiguration?.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false)) { - requestTelemetry.Source = sourceAppId; + // If the source header is present on the incoming request, + // and it is an external component (not the same ikey as the one used by the current component), + // then populate the source field. + if (!string.IsNullOrEmpty(sourceAppId) && sourceAppId != currentComponentAppId) + { + requestTelemetry.Source = sourceAppId; + } } } @@ -240,16 +235,15 @@ public void AddTargetHashForResponseHeader(HttpContext context) if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && context.Response.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey) == null) { - string correlationId; - - if (CorrelationIdLookupSingleton.Instance.TryGetXComponentCorrelationId(requestTelemetry.Context.InstrumentationKey, out correlationId)) + string applicationId = null; + if (telemetryConfiguration.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) { - context.Response.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey, correlationId); + context.Response.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey, applicationId); if (this.EnableAccessControlExposeHeader) { // set additional header that allows to read this Request-Context from Javascript SDK - // append this header with additional value to the potential ones defined by customer and they will be concatenated on cliend-side + // append this header with additional value to the potential ones defined by customer and they will be concatenated on client-side context.Response.AppendHeader(RequestResponseHeaders.AccessControlExposeHeadersHeader, RequestResponseHeaders.RequestContextHeader); } } @@ -267,14 +261,10 @@ public void AddTargetHashForResponseHeader(HttpContext context) /// Telemetry configuration to use for initialization. public void Initialize(TelemetryConfiguration configuration) { + this.telemetryConfiguration = configuration; this.telemetryClient = new TelemetryClient(configuration); this.telemetryClient.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("web:"); - if (configuration != null && configuration.TelemetryChannel != null) - { - this.telemetryChannelEnpoint = configuration.TelemetryChannel.EndpointAddress; - } - // Headers will be read-only in a classic iis pipeline // Exception System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode. if (HttpRuntime.UsingIntegratedPipeline && this.EnableChildRequestTrackingSuppression) From 9c34877dbaa367c36747e060e36f8849157ac5c5 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 3 Apr 2018 09:47:32 -0700 Subject: [PATCH 05/19] remove commented code --- ...dencyCollectorDiagnosticListenerTests.Netstandard16.cs | 2 -- .../RequestTrackingTelemetryModuleTest.cs | 8 +------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs index ad6622532..52b52c0b4 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs @@ -28,8 +28,6 @@ public partial class DependencyCollectorDiagnosticListenerTests private const string RequestUrlWithScheme = "https://" + RequestUrl; private const string HttpOkResultCode = "200"; private const string NotFoundResultCode = "404"; - //private const string MockAppId = "MOCK_APP_ID"; - //private const string MockAppId2 = "MOCK_APP_ID_2"; private readonly List sentTelemetry = new List(); diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index 1a6cd64fe..82cc6ad5f 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -29,13 +29,7 @@ public partial class RequestTrackingTelemetryModuleTest private const string testInstrumentationKey2 = nameof(testInstrumentationKey2); private const string testApplicationId1 = nameof(testApplicationId1); private const string testApplicationId2 = nameof(testApplicationId2); - - [TestInitialize] - public void TestInitialize() - { - //this.configuration.ApplicationIdProvider = new MockApplicationIdProvider(); - } - + [TestCleanup] public void Cleanup() { From a8dd9e7452807613050511ba07fecc49b9e46e87 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 3 Apr 2018 10:21:52 -0700 Subject: [PATCH 06/19] added nuget install scripts --- .../NuGet/ApplicationInsights.config.install.xdt | 1 + .../NuGet/ApplicationInsights.config.uninstall.xdt | 3 +++ .../Resources/net45/ApplicationInsights.config.install.xdt | 1 + .../Resources/net45/ApplicationInsights.config.uninstall.xdt | 2 ++ 4 files changed, 7 insertions(+) diff --git a/Src/DependencyCollector/NuGet/ApplicationInsights.config.install.xdt b/Src/DependencyCollector/NuGet/ApplicationInsights.config.install.xdt index 07d4a8656..2939c9a5d 100644 --- a/Src/DependencyCollector/NuGet/ApplicationInsights.config.install.xdt +++ b/Src/DependencyCollector/NuGet/ApplicationInsights.config.install.xdt @@ -23,4 +23,5 @@ + \ No newline at end of file diff --git a/Src/DependencyCollector/NuGet/ApplicationInsights.config.uninstall.xdt b/Src/DependencyCollector/NuGet/ApplicationInsights.config.uninstall.xdt index 6dffbb49b..be9cbd175 100644 --- a/Src/DependencyCollector/NuGet/ApplicationInsights.config.uninstall.xdt +++ b/Src/DependencyCollector/NuGet/ApplicationInsights.config.uninstall.xdt @@ -7,4 +7,7 @@ + + + \ No newline at end of file diff --git a/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.install.xdt b/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.install.xdt index 8532a9c2d..b7a8f457d 100644 --- a/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.install.xdt +++ b/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.install.xdt @@ -37,4 +37,5 @@ + \ No newline at end of file diff --git a/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.uninstall.xdt b/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.uninstall.xdt index f83543f28..0290401f1 100644 --- a/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.uninstall.xdt +++ b/Src/Web/Web.Nuget/Resources/net45/ApplicationInsights.config.uninstall.xdt @@ -19,5 +19,7 @@ + + \ No newline at end of file From b882b9739065b9b81e53b477035472f6fbdcf7f9 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 3 Apr 2018 14:05:52 -0700 Subject: [PATCH 07/19] marked property as obsolete --- .../Shared/DependencyTrackingTelemetryModule.cs | 6 ++++++ Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs index b42b9ec92..d426099d7 100644 --- a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs +++ b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs @@ -95,6 +95,12 @@ public bool SetComponentCorrelationHttpHeaders } } + /// + /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). + /// + [Obsolete("This field has been deprecated. Please set TelemetryConfiguration.Active.ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider() and customize ApplicationInsightsApplicationIdProvider.ProfileQueryEndpoint.")] + public string ProfileQueryEndpoint { get; set; } + /// /// IDisposable implementation. /// diff --git a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs index 71741e555..63d15f5b9 100644 --- a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs +++ b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs @@ -79,6 +79,12 @@ public bool SetComponentCorrelationHttpHeaders } } + /// + /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). + /// + [Obsolete("This field has been deprecated. Please set TelemetryConfiguration.Active.ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider() and customize ApplicationInsightsApplicationIdProvider.ProfileQueryEndpoint.")] + public string ProfileQueryEndpoint { get; set; } + /// /// Implements on begin callback of http module. /// From 8e74714e375de72cb3575ad0dffd783f914a8dc6 Mon Sep 17 00:00:00 2001 From: MS-TimothyMothra Date: Mon, 9 Apr 2018 14:45:40 -0700 Subject: [PATCH 08/19] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d3586ec..be8b81165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Version 2.6.0-beta4 +- [Remove CorrelationIdLookupHelper. Use TelemetryConfiguration.ApplicationIdProvider instead.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/880) + This change also fixes [ProfileQueryEndpoint is not overridable](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/853) + + ## Version 2.6.0-beta3 - [Ignore Deprecated events if running under netcore20](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/848) - [Implement unhandled exception auto-tracking (500 requests) for MVC 5 and WebAPI 2 applications.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/847) From 09b7643728425c0198b38c37e2d117471ceaf23c Mon Sep 17 00:00:00 2001 From: MS-TimothyMothra Date: Mon, 9 Apr 2018 15:32:49 -0700 Subject: [PATCH 09/19] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8db562b16..b30b18578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,7 @@ # Changelog ## Version 2.6.0-beta4 -- [Remove CorrelationIdLookupHelper. Use TelemetryConfiguration.ApplicationIdProvider instead.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/880) - This change also fixes [ProfileQueryEndpoint is not overridable](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/853) +- [Remove CorrelationIdLookupHelper. Use TelemetryConfiguration.ApplicationIdProvider instead.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/880) With this change you can update URL to query application ID from which enables environments with reverse proxy configuration to access Application Insights ednpoints. ## Version 2.6.0-beta3 From e4c51676c125bf51b5c8a8ce095b94a26bfbe333 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 9 Apr 2018 16:20:41 -0700 Subject: [PATCH 10/19] fix stylecop --- .../Shared/DependencyTrackingTelemetryModule.cs | 6 +++--- .../Shared/HttpCoreDiagnosticSourceListener.cs | 4 ++-- Src/TestFramework/Shared/MockApplicationIdProvider.cs | 2 +- Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs index d426099d7..e5d1aede9 100644 --- a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs +++ b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs @@ -95,9 +95,9 @@ public bool SetComponentCorrelationHttpHeaders } } - /// - /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). - /// + /// + /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). + /// [Obsolete("This field has been deprecated. Please set TelemetryConfiguration.Active.ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider() and customize ApplicationInsightsApplicationIdProvider.ProfileQueryEndpoint.")] public string ProfileQueryEndpoint { get; set; } diff --git a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs index bb636d7be..f2842cfdf 100644 --- a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs +++ b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs @@ -430,7 +430,7 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta string sourceApplicationId = null; if (!string.IsNullOrEmpty(instrumentationKey) && !HttpHeadersUtilities.ContainsRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey) - && (configuration.ApplicationIdProvider?.TryGetApplicationId(instrumentationKey, out sourceApplicationId) ?? false)) + && (this.configuration.ApplicationIdProvider?.TryGetApplicationId(instrumentationKey, out sourceApplicationId) ?? false)) { HttpHeadersUtilities.SetRequestContextKeyValue(requestHeaders, RequestResponseHeaders.RequestContextCorrelationSourceKey, sourceApplicationId); } @@ -495,7 +495,7 @@ private void ParseResponse(HttpResponseMessage response, DependencyTelemetry tel { // We only add the cross component correlation key if the key does not represent the current component. string sourceApplicationId = null; - if (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) ?? false) + if (this.configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out sourceApplicationId) ?? false) { if (targetApplicationId != sourceApplicationId) { diff --git a/Src/TestFramework/Shared/MockApplicationIdProvider.cs b/Src/TestFramework/Shared/MockApplicationIdProvider.cs index e8f1dc3f5..9a601b751 100644 --- a/Src/TestFramework/Shared/MockApplicationIdProvider.cs +++ b/Src/TestFramework/Shared/MockApplicationIdProvider.cs @@ -18,7 +18,7 @@ public MockApplicationIdProvider(string expectedInstrumentationKey, string appli public bool TryGetApplicationId(string instrumentationKey, out string applicationId) { - if(this.expectedInstrumentationKey == instrumentationKey) + if (this.expectedInstrumentationKey == instrumentationKey) { applicationId = this.applicationId; return true; diff --git a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs index 63d15f5b9..adcbade3f 100644 --- a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs +++ b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs @@ -79,9 +79,9 @@ public bool SetComponentCorrelationHttpHeaders } } - /// - /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). - /// + /// + /// Gets or sets the endpoint that is to be used to get the application insights resource's profile (appId etc.). + /// [Obsolete("This field has been deprecated. Please set TelemetryConfiguration.Active.ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider() and customize ApplicationInsightsApplicationIdProvider.ProfileQueryEndpoint.")] public string ProfileQueryEndpoint { get; set; } @@ -195,7 +195,7 @@ public void OnEndRequest(HttpContext context) string currentComponentAppId = null; if (!string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) - && (telemetryConfiguration?.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false)) + && (this.telemetryConfiguration?.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false)) { // If the source header is present on the incoming request, // and it is an external component (not the same ikey as the one used by the current component), From b5dcee01c29ebd3b9e58fb404357a0a5fb36b75d Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 9 Apr 2018 16:25:38 -0700 Subject: [PATCH 11/19] fix stylecop issues --- ...orDiagnosticListenerTests.Netstandard16.cs | 8 +++--- ...ktopDiagnosticSourceHttpProcessingTests.cs | 8 +++--- .../FrameworkHttpProcessingTest.cs | 8 +++--- .../ProfilerHttpProcessingTest.cs | 10 +++---- .../Shared/Implementation/HttpProcessing.cs | 6 ++-- .../RequestTrackingTelemetryModuleTest.cs | 28 +++++++++---------- .../RequestTrackingTelemetryModule.cs | 2 +- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs index 52b52c0b4..bdfebed01 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard16.cs @@ -329,7 +329,7 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndSameTarge RequestMessage = request }; - response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, testApplicationId1); + response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, this.testApplicationId1); this.listener.OnResponse(response, loggingRequestId); Assert.IsFalse(this.listener.PendingDependencyTelemetry.TryGetValue(request, out dependency)); @@ -364,7 +364,7 @@ public void OnResponseWithFailedResponseEventWithMatchingRequestAndSameTargetIns RequestMessage = request }; - response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, testApplicationId1); + response.Headers.Add(RequestResponseHeaders.RequestContextCorrelationTargetKey, this.testApplicationId1); this.listener.OnResponse(response, loggingRequestId); Assert.IsFalse(this.listener.PendingDependencyTelemetry.TryGetValue(request, out dependency)); @@ -399,7 +399,7 @@ public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndDifferent RequestMessage = request }; - string targetApplicationId = testApplicationId2; + string targetApplicationId = this.testApplicationId2; HttpHeadersUtilities.SetRequestContextKeyValue(response.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey, targetApplicationId); this.listener.OnResponse(response, loggingRequestId); @@ -435,7 +435,7 @@ public void OnResponseWithFailedResponseEventWithMatchingRequestAndDifferentTarg RequestMessage = request }; - string targetApplicationId = testApplicationId2; + string targetApplicationId = this.testApplicationId2; HttpHeadersUtilities.SetRequestContextKeyValue(response.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey, targetApplicationId); this.listener.OnResponse(response, loggingRequestId); diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs index fa775d68b..1687ce33c 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DesktopDiagnosticSourceHttpProcessingTests.cs @@ -25,13 +25,13 @@ public class DesktopDiagnosticSourceHttpProcessingTests { #region Fields private const int TimeAccuracyMilliseconds = 50; + private const string TestInstrumentationKey = nameof(TestInstrumentationKey); + private const string TestApplicationId = nameof(TestApplicationId); private Uri testUrl = new Uri("http://www.microsoft.com/"); private int sleepTimeMsecBetweenBeginAndEnd = 100; private TelemetryConfiguration configuration; private List sendItems = new List(); private DesktopDiagnosticSourceHttpProcessing httpDesktopProcessingFramework; - private const string testInstrumentationKey = nameof(testInstrumentationKey); - private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -42,8 +42,8 @@ public void TestInitialize() this.configuration = new TelemetryConfiguration() { TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, - InstrumentationKey = testInstrumentationKey, - ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + InstrumentationKey = TestInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(TestInstrumentationKey, TestApplicationId) }; this.httpDesktopProcessingFramework = new DesktopDiagnosticSourceHttpProcessing(this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), /*setCorrelationHeaders*/ true, new List()); diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs index 61686bfaa..548789034 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs @@ -26,6 +26,8 @@ public sealed class FrameworkHttpProcessingTest : IDisposable #region Fields private const string RandomAppIdEndpoint = "http://app.id.endpoint"; // appIdEndpoint - this really won't be used for tests because of the app id provider override. private const int TimeAccuracyMilliseconds = 50; + private const string TestInstrumentationKey = nameof(TestInstrumentationKey); + private const string TestApplicationId = nameof(TestApplicationId); private Uri testUrl = new Uri("http://www.microsoft.com/"); private Uri testUrlNonStandardPort = new Uri("http://www.microsoft.com:911/"); private int sleepTimeMsecBetweenBeginAndEnd = 100; @@ -33,8 +35,6 @@ public sealed class FrameworkHttpProcessingTest : IDisposable private List sendItems = new List(); private FrameworkHttpProcessing httpProcessingFramework; private CacheBasedOperationHolder cache = new CacheBasedOperationHolder("testCache", 100 * 1000); - private const string testInstrumentationKey = nameof(testInstrumentationKey); - private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -45,8 +45,8 @@ public void TestInitialize() this.configuration = new TelemetryConfiguration() { TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, - InstrumentationKey = testInstrumentationKey, - ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + InstrumentationKey = TestInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(TestInstrumentationKey, TestApplicationId) }; this.httpProcessingFramework = new FrameworkHttpProcessing(this.configuration, this.cache, /*setCorrelationHeaders*/ true, new List()); DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false; diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs index aa61ebc34..24e37dee1 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs @@ -32,6 +32,8 @@ public sealed class ProfilerHttpProcessingTest : IDisposable { #region Fields private const int TimeAccuracyMilliseconds = 150; // this may be big number when under debugger + private const string TestInstrumentationKey = nameof(TestInstrumentationKey); + private const string TestApplicationId = nameof(TestApplicationId); private TelemetryConfiguration configuration; private Uri testUrl = new Uri("http://www.microsoft.com/"); private Uri testUrlNonStandardPort = new Uri("http://www.microsoft.com:911/"); @@ -39,8 +41,6 @@ public sealed class ProfilerHttpProcessingTest : IDisposable private int sleepTimeMsecBetweenBeginAndEnd = 100; private Exception ex = new Exception(); private ProfilerHttpProcessing httpProcessingProfiler; - private const string testInstrumentationKey = nameof(testInstrumentationKey); - private const string testApplicationId = nameof(testApplicationId); #endregion //Fields #region TestInitialize @@ -51,8 +51,8 @@ public void TestInitialize() this.configuration = new TelemetryConfiguration() { TelemetryChannel = new StubTelemetryChannel { OnSend = item => this.sendItems.Add(item) }, - InstrumentationKey = testInstrumentationKey, - ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey, testApplicationId) + InstrumentationKey = TestInstrumentationKey, + ApplicationIdProvider = new MockApplicationIdProvider(TestInstrumentationKey, TestApplicationId) }; this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); @@ -140,7 +140,7 @@ public void RddTestHttpProcessingProfilerOnEndAddsAppIdToTargetField() [Description("Validates DependencyTelemetry does not send correlation ID if the IKey is from the same component")] public void RddTestHttpProcessingProfilerOnEndDoesNotAddAppIdToTargetFieldForInternalComponents() { - this.SimulateWebRequestResponseWithAppId(testApplicationId); + this.SimulateWebRequestResponseWithAppId(TestApplicationId); Assert.AreEqual(1, this.sendItems.Count, "Only one telemetry item should be sent"); diff --git a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs index aae2f0d48..c928e422a 100644 --- a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs @@ -22,9 +22,9 @@ internal abstract class HttpProcessing { protected TelemetryClient telemetryClient; private readonly ApplicationInsightsUrlFilter applicationInsightsUrlFilter; + private readonly TelemetryConfiguration configuration; private ICollection correlationDomainExclusionList; private bool setCorrelationHeaders; - private readonly TelemetryConfiguration configuration; /// /// Initializes a new instance of the class. @@ -152,7 +152,7 @@ internal object OnBegin(object thisObj, bool injectCorrelationHeaders = true) string applicationId = null; if (!string.IsNullOrEmpty(telemetry.Context.InstrumentationKey) && webRequest.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey) == null - && (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out applicationId) ?? false)) + && (this.configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out applicationId) ?? false)) { webRequest.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationSourceKey, applicationId); } @@ -405,7 +405,7 @@ private void SetTarget(DependencyTelemetry telemetry, WebHeaderCollection respon } string currentComponentAppId = null; - if (configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false) + if (this.configuration.ApplicationIdProvider?.TryGetApplicationId(telemetry.Context.InstrumentationKey, out currentComponentAppId) ?? false) { // We only add the cross component correlation key if the key does not remain the current component. if (!string.IsNullOrEmpty(targetAppId) && targetAppId != currentComponentAppId) diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index 82cc6ad5f..50ac81b03 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -25,10 +25,10 @@ [TestClass] public partial class RequestTrackingTelemetryModuleTest { - private const string testInstrumentationKey1 = nameof(testInstrumentationKey1); - private const string testInstrumentationKey2 = nameof(testInstrumentationKey2); - private const string testApplicationId1 = nameof(testApplicationId1); - private const string testApplicationId2 = nameof(testApplicationId2); + private const string TestInstrumentationKey1 = nameof(TestInstrumentationKey1); + private const string TestInstrumentationKey2 = nameof(TestInstrumentationKey2); + private const string TestApplicationId1 = nameof(TestApplicationId1); + private const string TestApplicationId2 = nameof(TestApplicationId2); [TestCleanup] public void Cleanup() @@ -322,12 +322,12 @@ public void OnEndDoesNotAddSourceFieldForRequestForSameComponent() { // ARRANGE Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, testApplicationId2); + headers.Add(RequestResponseHeaders.RequestContextHeader, TestApplicationId2); var context = HttpModuleHelper.GetFakeHttpContext(headers); - var config = this.CreateDefaultConfig(context, instrumentationKey: testInstrumentationKey1); - config.ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey1, testApplicationId1); + var config = this.CreateDefaultConfig(context, instrumentationKey: TestInstrumentationKey1); + config.ApplicationIdProvider = new MockApplicationIdProvider(TestInstrumentationKey1, TestApplicationId1); var module = this.RequestTrackingTelemetryModuleFactory(config); // ACT @@ -343,14 +343,14 @@ public void OnEndAddsSourceFieldForRequestWithCorrelationId() { // ARRANGE Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(testApplicationId2)); + headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(TestApplicationId2)); var context = HttpModuleHelper.GetFakeHttpContext(headers); // My instrumentation key and hence app id is random / newly generated. The appId header is different - hence a different component. var config = TelemetryConfiguration.CreateDefault(); - config.InstrumentationKey = testInstrumentationKey1; - config.ApplicationIdProvider = new MockApplicationIdProvider(testInstrumentationKey1, testApplicationId1); + config.InstrumentationKey = TestInstrumentationKey1; + config.ApplicationIdProvider = new MockApplicationIdProvider(TestInstrumentationKey1, TestApplicationId1); var module = this.RequestTrackingTelemetryModuleFactory(null /*use default*/); @@ -360,7 +360,7 @@ public void OnEndAddsSourceFieldForRequestWithCorrelationId() module.OnEndRequest(context); // VALIDATE - Assert.Equal(testApplicationId2, context.GetRequestTelemetry().Source); + Assert.Equal(TestApplicationId2, context.GetRequestTelemetry().Source); } [TestMethod] @@ -390,19 +390,19 @@ public void OnEndDoesNotOverrideSourceField() { // ARRANGE Dictionary headers = new Dictionary(); - headers.Add(RequestResponseHeaders.RequestContextHeader, testApplicationId1); + headers.Add(RequestResponseHeaders.RequestContextHeader, TestApplicationId1); var context = HttpModuleHelper.GetFakeHttpContext(headers); var module = this.RequestTrackingTelemetryModuleFactory(); module.OnBeginRequest(context); - context.GetRequestTelemetry().Source = testApplicationId2; + context.GetRequestTelemetry().Source = TestApplicationId2; // ACT module.OnEndRequest(context); // VALIDATE - Assert.Equal(testApplicationId2, context.GetRequestTelemetry().Source); + Assert.Equal(TestApplicationId2, context.GetRequestTelemetry().Source); } private TelemetryConfiguration CreateDefaultConfig(HttpContext fakeContext, string rootIdHeaderName = null, string parentIdHeaderName = null, string instrumentationKey = null) diff --git a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs index adcbade3f..473f71092 100644 --- a/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs +++ b/Src/Web/Web.Shared.Net/RequestTrackingTelemetryModule.cs @@ -242,7 +242,7 @@ public void AddTargetHashForResponseHeader(HttpContext context) && context.Response.Headers.GetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey) == null) { string applicationId = null; - if (telemetryConfiguration.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) + if (this.telemetryConfiguration.ApplicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) { context.Response.Headers.SetNameValueHeaderValue(RequestResponseHeaders.RequestContextHeader, RequestResponseHeaders.RequestContextCorrelationTargetKey, applicationId); From a1347974638cd32e0f30b7811a9ef2affaefd090 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 9 Apr 2018 16:27:08 -0700 Subject: [PATCH 12/19] cleanup usings --- .../Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs | 3 --- .../Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs | 2 -- .../Shared/Implementation/HttpProcessing.cs | 2 -- .../Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs | 1 - 4 files changed, 8 deletions(-) diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs index 548789034..6d1989505 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/FrameworkHttpProcessingTest.cs @@ -4,12 +4,9 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; - using System.Linq; using System.Net; using System.Threading; - using System.Threading.Tasks; using Microsoft.ApplicationInsights.Channel; - using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.DependencyCollector.Implementation; diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs index 24e37dee1..f5697ecae 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs @@ -10,8 +10,6 @@ using System.Linq; using System.Net; using System.Threading; - using System.Threading.Tasks; - using System.Web; using Common; using Microsoft.ApplicationInsights.Channel; diff --git a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs index c928e422a..fbb163ef1 100644 --- a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs @@ -6,11 +6,9 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using System.Globalization; using System.Linq; using System.Net; - using System.Web; using Extensibility.Implementation.Tracing; using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.DependencyCollector.Implementation.Operation; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index 50ac81b03..c03d660c9 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; - using System.Threading.Tasks; using System.Web; using Microsoft.ApplicationInsights.Channel; From 52c56cc9426df0e2f421ad4e6b1ed2fa43698437 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 9 Apr 2018 16:28:39 -0700 Subject: [PATCH 13/19] cleanup usings --- .../Shared/DependencyTrackingTelemetryModule.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs index e5d1aede9..2a6fea72e 100644 --- a/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs +++ b/Src/DependencyCollector/Shared/DependencyTrackingTelemetryModule.cs @@ -10,7 +10,6 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; #if NETSTANDARD1_6 - using Microsoft.Extensions.PlatformAbstractions; using System.Reflection; using System.Runtime.Versioning; #else From 479015f421d5c50f3a2676bc85c27c95adc47260 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Tue, 10 Apr 2018 08:28:34 -0700 Subject: [PATCH 14/19] update test app configs --- .../TestApps/Net452/E2ETestApp/ApplicationInsights.config | 1 + .../TestApps/Net452/E2ETestWebApi/ApplicationInsights.config | 3 ++- .../TestApps/TestApp45/ApplicationInsights.config | 1 + .../TestApps/AspNetDiagnostics/ApplicationInsights.config | 1 + .../FunctionalTests/TestApps/Aspx45/ApplicationInsights.config | 1 + .../TestApps/ConsoleAppFW45/ApplicationInsights.config | 1 + .../TestApps/Mvc4_MediumTrust/ApplicationInsights.config | 1 + .../TestApps/Wa45Aspx/ApplicationInsights.config | 1 + .../TestApps/Wcf45Tests/ApplicationInsights.config | 1 + .../TestApps/WebAppFW45/ApplicationInsights.config | 1 + .../TestApps/WebAppFW45Sampled/ApplicationInsights.config | 1 + 11 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config b/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config index df631e99a..5e7babfa6 100644 --- a/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config +++ b/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config @@ -83,6 +83,7 @@ http://e2etests_ingestionservice_1/api/Data/PushItem true + + diff --git a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config index c62019073..7aec76410 100644 --- a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config @@ -57,4 +57,5 @@ Event + diff --git a/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config index 61bba6c0b..cd9f80ca4 100644 --- a/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config @@ -53,4 +53,5 @@ Event + diff --git a/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config index a886c9a0b..2066c6e48 100644 --- a/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config @@ -29,4 +29,5 @@ Event + \ No newline at end of file diff --git a/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config index 6c565d5ad..c2d817ef7 100644 --- a/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config @@ -50,4 +50,5 @@ 5 + diff --git a/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config index ebd65af88..948302294 100644 --- a/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config @@ -52,4 +52,5 @@ Event + diff --git a/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config index 5b7bedb6b..3de02c25d 100644 --- a/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config @@ -55,4 +55,5 @@ Event + diff --git a/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config index 83680baa5..d5e7f1ffc 100644 --- a/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config @@ -57,4 +57,5 @@ Event + diff --git a/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config index 5d0f214d6..59bead469 100644 --- a/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config @@ -58,4 +58,5 @@ Event + From 927fb699471dfd6bb5f0a700e33dc8403f0aaacc Mon Sep 17 00:00:00 2001 From: MS-TimothyMothra Date: Tue, 10 Apr 2018 09:02:46 -0700 Subject: [PATCH 15/19] removed incorrect comment --- Src/TestFramework/Shared/MockApplicationIdProvider.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Src/TestFramework/Shared/MockApplicationIdProvider.cs b/Src/TestFramework/Shared/MockApplicationIdProvider.cs index 9a601b751..730a87f93 100644 --- a/Src/TestFramework/Shared/MockApplicationIdProvider.cs +++ b/Src/TestFramework/Shared/MockApplicationIdProvider.cs @@ -2,9 +2,6 @@ { using Microsoft.ApplicationInsights.Extensibility; - /// - /// Use this to have TryGet return the instrumentation key as the correlation id. - /// internal class MockApplicationIdProvider : IApplicationIdProvider { private readonly string expectedInstrumentationKey; From 9a53f198533602e49a1d62bd3e6af67b8e8afae9 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 16 Apr 2018 10:36:01 -0700 Subject: [PATCH 16/19] fix ai.config and update test to be more meaningful --- .../FunctionalTests/FunctionalTests/DiagnosticsTest.cs | 8 ++++++-- .../TestApps/AspNetDiagnostics/ApplicationInsights.config | 4 +++- .../TestApps/AspNetDiagnostics/BuildInfo.config | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Test/Web/FunctionalTests/FunctionalTests/DiagnosticsTest.cs b/Test/Web/FunctionalTests/FunctionalTests/DiagnosticsTest.cs index aa3cb1187..6ee4f2f33 100644 --- a/Test/Web/FunctionalTests/FunctionalTests/DiagnosticsTest.cs +++ b/Test/Web/FunctionalTests/FunctionalTests/DiagnosticsTest.cs @@ -49,7 +49,7 @@ public void TestCleanUp() } [TestMethod] - [Description("Validates that diagnostics module sends trace data to the Portal")] + [Description("Validates that diagnostics module sends trace data to the Portal. Expects an exception about incorrect xml structure of 'BuildInfo.config'.")] public void TestDiagnosticsFW45() { var responseTask = this.HttpClient.GetStringAsync("/"); @@ -59,12 +59,16 @@ public void TestDiagnosticsFW45() Assert.IsTrue(responseTask.Result.Contains("Home Page - My ASP.NET Application"), "Incorrect response returned: " + responseTask.Result); var items = Listener.ReceiveAllItemsDuringTimeOfType>(TestListenerTimeoutInMs); + var itemCount = items.Count(); // Check that instrumentation key is correct Assert.AreEqual(0, items.Count(i => !i.iKey.Equals(DiagnosticsInstrumentationKey)), "Some item does not have DiagnosticsInstrumentationKey"); // There should be one custom actionable event about incorrect timeout of session expiration - Assert.IsTrue(items.Count(i => i.data.baseData.message.StartsWith("AI: ")) == 1, "AI actionable event was not recieved"); + var actionableEventCount = items.Count(i => i.data.baseData.message.StartsWith("AI: ")); + Assert.IsTrue(actionableEventCount >= 1, "AI actionable event was not received"); + + Assert.AreEqual(1, actionableEventCount, $"Test project possibly mis-configured. Expected 1 actionable event. Received '{actionableEventCount}'"); } } } diff --git a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config index 7aec76410..3af96b824 100644 --- a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/ApplicationInsights.config @@ -57,5 +57,7 @@ Event - + + http://localhost:4000/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/BuildInfo.config b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/BuildInfo.config index 715ea64d0..b313b9fc1 100644 --- a/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/BuildInfo.config +++ b/Test/Web/FunctionalTests/TestApps/AspNetDiagnostics/BuildInfo.config @@ -1,3 +1,3 @@ This is clearly not a valid XML. File is used so user actionable diagnostics message will be generated by BuildInfoConfigComponentVersionTelemetryInitializer. - Test verifies that diagnositcs works. \ No newline at end of file + Test verifies that diagnostics works. \ No newline at end of file From c64617f1acfa4ee73834f2144b0e60e821352382 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 16 Apr 2018 10:44:24 -0700 Subject: [PATCH 17/19] correct ai.config --- .../TestApps/Aspx45/ApplicationInsights.config | 4 +++- .../TestApps/ConsoleAppFW45/ApplicationInsights.config | 4 +++- .../TestApps/Mvc4_MediumTrust/ApplicationInsights.config | 4 +++- .../TestApps/Wa45Aspx/ApplicationInsights.config | 4 +++- .../TestApps/Wcf45Tests/ApplicationInsights.config | 4 +++- .../TestApps/WebAppFW45/ApplicationInsights.config | 4 +++- .../TestApps/WebAppFW45Sampled/ApplicationInsights.config | 4 +++- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config index cd9f80ca4..bae0edad7 100644 --- a/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Aspx45/ApplicationInsights.config @@ -53,5 +53,7 @@ Event - + + http://localhost:4001/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config index 2066c6e48..9a10b0e94 100644 --- a/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/ConsoleAppFW45/ApplicationInsights.config @@ -29,5 +29,7 @@ Event - + + http://localhost:4002/api/profiles/{0}/appId + \ No newline at end of file diff --git a/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config index c2d817ef7..b30c7e59f 100644 --- a/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Mvc4_MediumTrust/ApplicationInsights.config @@ -50,5 +50,7 @@ 5 - + + http://localhost:4004/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config index 948302294..d1b678b7c 100644 --- a/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Wa45Aspx/ApplicationInsights.config @@ -52,5 +52,7 @@ Event - + + http://localhost:4005/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config index 3de02c25d..181a78aa4 100644 --- a/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/Wcf45Tests/ApplicationInsights.config @@ -55,5 +55,7 @@ Event - + + http://localhost:4006/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config index d5e7f1ffc..4493ca96e 100644 --- a/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/WebAppFW45/ApplicationInsights.config @@ -57,5 +57,7 @@ Event - + + http://localhost:4017/api/profiles/{0}/appId + diff --git a/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config b/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config index 59bead469..3d6ed0fba 100644 --- a/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config +++ b/Test/Web/FunctionalTests/TestApps/WebAppFW45Sampled/ApplicationInsights.config @@ -58,5 +58,7 @@ Event - + + http://localhost:4008/api/profiles/{0}/appId + From 2f72f86b7b0fb0023e7fea09e8d1d22961b5950d Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 16 Apr 2018 10:51:41 -0700 Subject: [PATCH 18/19] update ai.config --- .../TestApps/TestApp45/ApplicationInsights.config | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Test/PerformanceCollector/FunctionalTests/TestApps/TestApp45/ApplicationInsights.config b/Test/PerformanceCollector/FunctionalTests/TestApps/TestApp45/ApplicationInsights.config index f14af6d86..b23e75163 100644 --- a/Test/PerformanceCollector/FunctionalTests/TestApps/TestApp45/ApplicationInsights.config +++ b/Test/PerformanceCollector/FunctionalTests/TestApps/TestApp45/ApplicationInsights.config @@ -39,5 +39,7 @@ 5 --> - + + http://LocalHost:{TelemetryEndpointPort}/api/profiles/{0}/appId + From 0008364fc6ff6a4041e4bdc398597fb271c4345d Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 16 Apr 2018 11:54:29 -0700 Subject: [PATCH 19/19] update config --- .../TestApps/Net452/E2ETestApp/ApplicationInsights.config | 4 +++- .../TestApps/Net452/E2ETestWebApi/ApplicationInsights.config | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config b/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config index 5e7babfa6..7ab13dd96 100644 --- a/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config +++ b/Test/E2ETests/TestApps/Net452/E2ETestApp/ApplicationInsights.config @@ -83,7 +83,9 @@ http://e2etests_ingestionservice_1/api/Data/PushItem true - + + http://e2etests_ingestionservice_1/api/profiles/{0}/appId +