private void Initialize(string instrumentationKey)
        {
            this.configuration = new TelemetryConfiguration();
            this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
            this.sendItems = new List <ITelemetry>();
            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 <string>(),
                RandomAppIdEndpoint);

            var correlationIdLookupHelper = new CorrelationIdLookupHelper((string ikey) =>
            {
                // Pretend App Id is the same as Ikey
                var tcs = new TaskCompletionSource <string>();
                tcs.SetResult(ikey);
                return(tcs.Task);
            });

            this.httpProcessingProfiler.OverrideCorrelationIdLookupHelper(correlationIdLookupHelper);
            this.ex = new Exception();
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProfilerHttpProcessing"/> class.
        /// </summary>
        public ProfilerHttpProcessing(TelemetryConfiguration configuration, string agentVersion, ObjectInstanceBasedOperationHolder telemetryTupleHolder, bool setCorrelationHeaders, ICollection <string> correlationDomainExclusionList, string appIdEndpoint)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            if (telemetryTupleHolder == null)
            {
                throw new ArgumentNullException("telemetryTupleHolder");
            }

            if (correlationDomainExclusionList == null)
            {
                throw new ArgumentNullException("correlationDomainExclusionList");
            }

            this.applicationInsightsUrlFilter = new ApplicationInsightsUrlFilter(configuration);
            this.TelemetryTable  = telemetryTupleHolder;
            this.telemetryClient = new TelemetryClient(configuration);
            this.correlationDomainExclusionList = correlationDomainExclusionList;
            this.setCorrelationHeaders          = setCorrelationHeaders;
            this.correlationIdLookupHelper      = new CorrelationIdLookupHelper(appIdEndpoint);

            // Since dependencySource is no longer set, sdk version is prepended with information which can identify whether RDD was collected by profiler/framework
            // For directly using TrackDependency(), version will be simply what is set by core
            string prefix = "rdd" + RddSource.Profiler + ":";

            this.telemetryClient.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion(prefix);
            if (!string.IsNullOrEmpty(agentVersion))
            {
                this.telemetryClient.Context.GetInternalContext().AgentVersion = agentVersion;
            }
        }
Esempio n. 3
0
        private void Initialize(string instrumentationKey)
        {
            this.configuration = new TelemetryConfiguration();
            this.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
            this.sendItems = new List <ITelemetry>();
            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 <string>(),
                RandomAppIdEndpoint);

            var correlationIdLookupHelper = new CorrelationIdLookupHelper(new Dictionary <string, string>
            {
                {
                    instrumentationKey,
                    "cid-v1:" + instrumentationKey + "-appId"
                }
            });

            this.httpProcessingProfiler.OverrideCorrelationIdLookupHelper(correlationIdLookupHelper);
            this.ex = new Exception();
        }
        public void CorrelationIdLookupHelperReturnsAppIdOnSecondCall()
        {
            var correlationIdLookupHelper = new CorrelationIdLookupHelper((ikey) =>
            {
                // Pretend App Id is the same as Ikey
                var tcs = new TaskCompletionSource <string>();
                tcs.SetResult(ikey);
                return(tcs.Task);
            });

            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));
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public void TryGetXComponentCorrelationIdShouldReturnEmptyWhenIKeyIsNull()
        {
            TelemetryConfiguration config = new TelemetryConfiguration(TestInstrumentationKey, new FakeTelemetryChannel()
            {
                EndpointAddress = "https://endpoint"
            });
            CorrelationIdLookupHelper target = new CorrelationIdLookupHelper(() => config);
            string result = "Not null value";

            target.TryGetXComponentCorrelationId(null, out result);

            Assert.Equal(string.Empty, result);
        }
Esempio n. 7
0
        public void TryGetXComponentCorrelationIdShouldReturnEmptyWhenBaseAddressIsNotGiven()
        {
            // CorrelationIdLookupHelper should fail gracefully when it can't fetch the base address from the channel.
            TelemetryConfiguration config = new TelemetryConfiguration(TestInstrumentationKey, new FakeTelemetryChannel()
            {
                EndpointAddress = string.Empty
            });
            CorrelationIdLookupHelper target = new CorrelationIdLookupHelper(() => config);
            string result = "Not null value";

            target.TryGetXComponentCorrelationId(null, out result);

            Assert.Equal(string.Empty, result);
        }
Esempio n. 8
0
        public void TryGetXComponentCorrelationIdShouldReturnAppIdWhenHit()
        {
            CorrelationIdLookupHelper target = new CorrelationIdLookupHelper((iKey) =>
            {
                return(Task.FromResult(string.Format(CultureInfo.InvariantCulture, "AppId for {0}", iKey)));
            });

            string actual = null;

            target.TryGetXComponentCorrelationId(TestInstrumentationKey, out actual);
            string expected = string.Format(CultureInfo.InvariantCulture, CorrelationIdLookupHelper.CorrelationIdFormat, "AppId for " + TestInstrumentationKey);

            Assert.Equal(expected, actual);
        }
Esempio n. 9
0
        private bool TryInitializeCorrelationHelperIfNotInitialized()
        {
            try
            {
                if (this.correlationIdLookupHelper == null)
                {
                    this.correlationIdLookupHelper = new CorrelationIdLookupHelper(this.EffectiveProfileQueryEndpoint);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public void TestInitialize()
        {
            this.configuration = new TelemetryConfiguration();
            this.sendItems     = new List <ITelemetry>();
            this.configuration.TelemetryChannel = new StubTelemetryChannel {
                OnSend = item => this.sendItems.Add(item)
            };
            this.configuration.InstrumentationKey = Guid.NewGuid().ToString();
            this.httpProcessingFramework          = new FrameworkHttpProcessing(this.configuration, new CacheBasedOperationHolder("testCache", 100 * 1000), /*setCorrelationHeaders*/ true, new List <string>(), RandomAppIdEndpoint);
            var correlationIdLookupHelper = new CorrelationIdLookupHelper((string ikey) =>
            {
                // Pretend App Id is the same as Ikey
                var tcs = new TaskCompletionSource <string>();
                tcs.SetResult(ikey);
                return(tcs.Task);
            });

            this.httpProcessingFramework.OverrideCorrelationIdLookupHelper(correlationIdLookupHelper);
        }
Esempio n. 11
0
        public void OnEndAddsSourceFieldForRequestWithCorrelationIdAndRoleName()
        {
            // ARRANGE
            string ikey     = "b3eb14d6-bb32-4542-9b93-473cd94aaedf";
            string appId    = ikey + "-appId";
            string roleName = "SomeRoleName";

            Dictionary <string, string> headers = new Dictionary <string, string>();

            // Add Request context With both appId and roleName.
            headers.Add(RequestResponseHeaders.RequestContextHeader, string.Format(CultureInfo.InvariantCulture, "{0}, {1}={2}", this.GetCorrelationIdHeaderValue(appId), RequestResponseHeaders.RequestContextSourceRoleNameKey, roleName));

            var context = HttpModuleHelper.GetFakeHttpContext(headers);

            var config = TelemetryConfiguration.CreateDefault();

            // My instrumentation key and hence app id is random / newly generated. The appId header is different - hence a different component.
            config.InstrumentationKey = Guid.NewGuid().ToString();

            var correlationHelper = new CorrelationIdLookupHelper(new Dictionary <string, string>()
            {
                {
                    config.InstrumentationKey,
                    config.InstrumentationKey + "-appId"
                },
                {
                    ikey,
                    appId
                }
            });

            var module = this.RequestTrackingTelemetryModuleFactory(null /*use default config*/, correlationHelper);

            // ACT
            module.Initialize(config);
            module.OnBeginRequest(context);
            module.OnEndRequest(context);

            // VALIDATE
            Assert.Equal(string.Format(CultureInfo.InvariantCulture, "{0} | {1}", this.GetCorrelationIdValue(appId), "roleName:SomeRoleName"), context.GetRequestTelemetry().Source);
        }
        public void OnEndAddsSourceFieldForRequestWithCorrelationId()
        {
            // ARRANGE
            string instrumentationKey = "b3eb14d6-bb32-4542-9b93-473cd94aaedf";
            string appId = instrumentationKey + "-appId";

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add(RequestResponseHeaders.RequestContextHeader, this.GetCorrelationIdHeaderValue(appId));

            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 <string, string>()
            {
                {
                    config.InstrumentationKey,
                    config.InstrumentationKey + "-appId"
                },
                {
                    instrumentationKey,
                    appId + "-appId"
                }
            });

            var module = this.RequestTrackingTelemetryModuleFactory(null /*use default*/, correlationHelper);

            // ACT
            module.Initialize(config);
            module.OnBeginRequest(context);
            module.OnEndRequest(context);

            // VALIDATE
            Assert.Equal(this.GetCorrelationIdValue(appId), context.GetRequestTelemetry().Source);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpProcessing"/> class.
        /// </summary>
        public HttpProcessing(TelemetryConfiguration configuration, string sdkVersion, string agentVersion, bool setCorrelationHeaders, ICollection <string> correlationDomainExclusionList, string appIdEndpoint)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            if (correlationDomainExclusionList == null)
            {
                throw new ArgumentNullException("correlationDomainExclusionList");
            }

            this.applicationInsightsUrlFilter = new ApplicationInsightsUrlFilter(configuration);
            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))
            {
                this.telemetryClient.Context.GetInternalContext().AgentVersion = agentVersion;
            }
        }
        private RequestTrackingTelemetryModule RequestTrackingTelemetryModuleFactory(TelemetryConfiguration config = null, CorrelationIdLookupHelper correlationHelper = null)
        {
            var module = new RequestTrackingTelemetryModule()
            {
                EnableChildRequestTrackingSuppression = false
            };

            module.OverrideCorrelationIdLookupHelper(correlationHelper ?? this.correlationIdLookupHelper);
            module.Initialize(config ?? this.CreateDefaultConfig(HttpModuleHelper.GetFakeHttpContext()));
            return(module);
        }
Esempio n. 15
0
 /// <summary>
 /// Simple test hook, that allows for using a stub rather than the implementation that calls the original service.
 /// </summary>
 /// <param name="correlationIdLookupHelper">Lookup header to use.</param>
 internal void OverrideCorrelationIdLookupHelper(CorrelationIdLookupHelper correlationIdLookupHelper)
 {
     this.correlationIdLookupHelper = correlationIdLookupHelper;
 }