public void RequestIdBecomesParentAndRootIfCompatibleWhenThereAreNoW3CHeaders_ExceptionInScopeOfRequest()
        {
            this.aspNetDiagnosticsSource.FakeContext =
                HttpModuleHelper.GetFakeHttpContext(new Dictionary <string, string>
            {
                ["Request-Id"] = "|4bf92f3577b34da6a3ce929d0e0e4736.",
            });
            this.module = this.CreateModule();

            var activity = new Activity(FakeAspNetDiagnosticSource.IncomingRequestEventName);

            Assert.IsTrue(this.aspNetDiagnosticsSource.IsEnabled(FakeAspNetDiagnosticSource.IncomingRequestEventName, activity));
            this.aspNetDiagnosticsSource.StartActivityWithoutChecks(activity);

            // Activity is overwritten to match new W3C-compatible id

            var appInsightsActivity = Activity.Current;

            Assert.AreNotEqual(activity, appInsightsActivity);

            // here is where OnError happens
            this.aspNetDiagnosticsSource.FakeContext.AddError(new ArgumentNullException());

            // and then activity ends
            this.aspNetDiagnosticsSource.StopActivity();

            Assert.AreEqual(2, this.sendItems.Count);

            var requestTelemetry   = this.sendItems.OfType <RequestTelemetry>().FirstOrDefault();
            var exceptionTelemetry = this.sendItems.OfType <ExceptionTelemetry>().FirstOrDefault();

            Assert.IsNotNull(requestTelemetry);
            Assert.AreEqual("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
            Assert.AreEqual("|4bf92f3577b34da6a3ce929d0e0e4736.", requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(appInsightsActivity.SpanId.ToHexString(), requestTelemetry.Id);

            Assert.IsNotNull(exceptionTelemetry);
            Assert.AreEqual("4bf92f3577b34da6a3ce929d0e0e4736", exceptionTelemetry.Context.Operation.Id);
            Assert.AreEqual(requestTelemetry.Id, exceptionTelemetry.Context.Operation.ParentId);

            Assert.IsFalse(requestTelemetry.Properties.ContainsKey("ai_legacyRootId"));
        }
Example #2
0
        public void TestActivityIdGenerationWithW3CEnabled()
        {
            this.module = this.CreateModule(enableW3cSupport: true);

            this.aspNetDiagnosticsSource.StartActivity();
            Activity activity = Activity.Current;

            this.aspNetDiagnosticsSource.StopActivity();

            var request = this.sendItems.OfType <RequestTelemetry>().Single();

            Assert.AreEqual(32, request.Context.Operation.Id.Length);
            Assert.IsTrue(Regex.Match(request.Context.Operation.Id, @"[a-z][0-9]").Success);

            Assert.AreEqual(request.Context.Operation.Id, activity.RootId);
            Assert.AreEqual(request.Context.Operation.ParentId, activity.GetParentSpanId());
            Assert.AreEqual(request.Id, $"|{activity.GetTraceId()}.{activity.GetSpanId()}.");

            Assert.IsFalse(request.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty));
        }
Example #3
0
        public void DoubleBeginEndRequestReportsOneTelemetry()
        {
            this.module = this.CreateModule();

            Assert.IsTrue(this.aspNetDiagnosticsSource.StartActivity());
            var activity = Activity.Current;

            Assert.IsTrue(this.aspNetDiagnosticsSource.StartActivity());

            this.aspNetDiagnosticsSource.StopActivity();
            this.aspNetDiagnosticsSource.StopActivity();
            Assert.AreEqual(1, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.IsNull(requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(activity.RootId, requestTelemetry.Context.Operation.Id);
            Assert.AreEqual(activity.Id, requestTelemetry.Id);
        }
Example #4
0
        private AspNetDiagnosticTelemetryModule CreateModule(string rootIdHeaderName = null, string parentIdHeaderName = null, bool enableW3cSupport = false)
        {
            var initializer = new Web.OperationCorrelationTelemetryInitializer();

            if (rootIdHeaderName != null)
            {
                initializer.RootOperationIdHeaderName = rootIdHeaderName;
            }

            if (parentIdHeaderName != null)
            {
                initializer.ParentOperationIdHeaderName = parentIdHeaderName;
            }

            this.configuration.TelemetryInitializers.Add(new Extensibility.OperationCorrelationTelemetryInitializer());

            if (enableW3cSupport)
            {
                this.configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            }

            AspNetDiagnosticTelemetryModule result = new AspNetDiagnosticTelemetryModule();

            var requestModule = new RequestTrackingTelemetryModule()
            {
                EnableChildRequestTrackingSuppression = false,
                EnableW3CHeadersExtraction            = enableW3cSupport
            };

            var exceptionModule = new ExceptionTrackingTelemetryModule();

            requestModule.Initialize(this.configuration);
            exceptionModule.Initialize(this.configuration);

            TelemetryModules.Instance.Modules.Add(requestModule);
            TelemetryModules.Instance.Modules.Add(exceptionModule);

            result.Initialize(this.configuration);

            return(result);
        }
        public void ChildTelemetryIsReportedProperlyBetweenBeginEndRequest()
        {
            this.module = this.CreateModule();

            this.aspNetDiagnosticsSource.StartActivity();

            var trace  = new TraceTelemetry();
            var client = new TelemetryClient(this.configuration);

            client.TrackTrace(trace);

            this.aspNetDiagnosticsSource.StopActivity();
            Assert.AreEqual(2, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry ?? this.sendItems[1] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);

            Assert.AreEqual(requestTelemetry.Id, trace.Context.Operation.ParentId);
            Assert.AreEqual(requestTelemetry.Context.Operation.Id, trace.Context.Operation.Id);
        }
        public void TestActivityIdGeneratioWithEmptyHeaders()
        {
            this.module = this.CreateModule();

            var activities = new Activity[5];

            for (int i = 0; i < activities.Length; i++)
            {
                this.aspNetDiagnosticsSource.StartActivity();
                activities[i] = Activity.Current;
                this.aspNetDiagnosticsSource.StopActivity();

                // clean up
                HttpContext.Current = HttpModuleHelper.GetFakeHttpContext();
            }

            Assert.AreEqual(activities.Length, this.sendItems.Count);

            var ids = this.sendItems.Select(i => ((RequestTelemetry)i).Context.Operation.Id);

            // This code should go away when Activity is fixed: https://github.com/dotnet/corefx/issues/18418
            // check that Ids are not generated by Activity
            // so they look like OperationTelemetry.Id
            foreach (var operationId in ids)
            {
                // length is like default RequestTelemetry.Id length
                Assert.AreEqual(new RequestTelemetry().Id.Length, operationId.Length);

                // operationId is ulong base64 encoded
                byte[] data = Convert.FromBase64String(operationId);
                Assert.AreEqual(8, data.Length);
                BitConverter.ToUInt64(data, 0);

                // does not look like root Id generated by Activity
                Assert.AreEqual(1, operationId.Split('-').Length);
            }

            //// end of workaround test
        }
        public void RequestIdBecomesParentAndRootIfCompatibleWhenThereAreNoW3CHeaders()
        {
            this.aspNetDiagnosticsSource.FakeContext =
                HttpModuleHelper.GetFakeHttpContext(new Dictionary <string, string>
            {
                ["Request-Id"]          = "|4bf92f3577b34da6a3ce929d0e0e4736.",
                ["Correlation-Context"] = "k=v",
            });
            this.module = this.CreateModule();

            var activity = new Activity(FakeAspNetDiagnosticSource.IncomingRequestEventName);

            Assert.IsTrue(this.aspNetDiagnosticsSource.IsEnabled(FakeAspNetDiagnosticSource.IncomingRequestEventName, activity));
            this.aspNetDiagnosticsSource.StartActivityWithoutChecks(activity);
            var currentActivity = Activity.Current;

            // Activity is overwritten to match new W3C-compatible id
            Assert.AreNotEqual(activity, currentActivity);

            this.aspNetDiagnosticsSource.StopActivity();

            Assert.AreEqual("4bf92f3577b34da6a3ce929d0e0e4736", currentActivity.TraceId.ToHexString());
            Assert.AreEqual("0000000000000000", currentActivity.ParentSpanId.ToHexString());

            Assert.AreEqual(1, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.AreEqual("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
            Assert.AreEqual("|4bf92f3577b34da6a3ce929d0e0e4736.", requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(currentActivity.SpanId.ToHexString(), requestTelemetry.Id);

            Assert.IsFalse(requestTelemetry.Properties.ContainsKey("ai_legacyRootId"));
            Assert.AreEqual(1, requestTelemetry.Properties.Count);
            Assert.IsTrue(requestTelemetry.Properties.TryGetValue("k", out var v));
            Assert.AreEqual("v", v);
        }
Example #8
0
        public void ReportLostActivityReportsTelemetry()
        {
            this.module = this.CreateModule();

            var activity = new Activity(FakeAspNetDiagnosticSource.IncomingRequestEventName).SetParentId("|guid.").AddBaggage("k", "v");

            Assert.IsTrue(this.aspNetDiagnosticsSource.StartActivity(activity));

            Activity.Current.Stop();
            Assert.IsNull(Activity.Current);

            this.aspNetDiagnosticsSource.StopLostActivity(activity);

            Assert.AreEqual(1, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.AreEqual("guid", requestTelemetry.Context.Operation.Id);
            Assert.AreEqual("|guid.", requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(activity.Id, requestTelemetry.Id);
            Assert.AreEqual("v", requestTelemetry.Properties["k"]);
        }
Example #9
0
        public void CustomHeadersBecomeParentWhenThereAreNoW3CHeaders()
        {
            this.aspNetDiagnosticsSource.FakeContext =
                HttpModuleHelper.GetFakeHttpContext(new Dictionary <string, string>
            {
                ["rootHeaderName"]   = "root",
                ["parentHeaderName"] = "parent"
            });
            this.module = this.CreateModule("rootHeaderName", "parentHeaderName", enableW3cSupport: true);

            var activity = new Activity(FakeAspNetDiagnosticSource.IncomingRequestEventName);

            activity.Extract(HttpContext.Current.Request.Headers);

            Assert.IsTrue(this.aspNetDiagnosticsSource.IsEnabled(FakeAspNetDiagnosticSource.IncomingRequestEventName, activity));
            this.aspNetDiagnosticsSource.StartActivityWithoutChecks(activity);
            this.aspNetDiagnosticsSource.StopActivity();

            Assert.AreEqual(32, activity.GetTraceId().Length);
            Assert.AreEqual(16, activity.GetSpanId().Length);
            Assert.IsNull(activity.GetParentSpanId());

            Assert.AreEqual(1, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.AreEqual(activity.GetTraceId(), requestTelemetry.Context.Operation.Id);
            Assert.AreEqual("parent", requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual($"|{activity.GetTraceId()}.{activity.GetSpanId()}.", requestTelemetry.Id);

            Assert.IsTrue(requestTelemetry.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty));
            Assert.AreEqual("root", requestTelemetry.Properties[W3CConstants.LegacyRootIdProperty]);

            Assert.IsTrue(requestTelemetry.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty));
            Assert.IsTrue(requestTelemetry.Properties[W3CConstants.LegacyRequestIdProperty].StartsWith("|root."));
        }
        public void CorrelationContextIsReadWithoutTraceparentAndRequestIdW3COff()
        {
            Activity.DefaultIdFormat                 = ActivityIdFormat.Hierarchical;
            Activity.ForceDefaultIdFormat            = true;
            this.aspNetDiagnosticsSource.FakeContext =
                HttpModuleHelper.GetFakeHttpContext(new Dictionary <string, string>
            {
                ["Correlation-Context"] = "k=v",
            });
            this.module = this.CreateModule();

            var activity = new Activity(FakeAspNetDiagnosticSource.IncomingRequestEventName);

            Assert.IsTrue(this.aspNetDiagnosticsSource.IsEnabled(FakeAspNetDiagnosticSource.IncomingRequestEventName, activity));
            this.aspNetDiagnosticsSource.StartActivityWithoutChecks(activity);
            var currentActivity = Activity.Current;

            Assert.AreEqual(1, activity.Baggage.Count());
            Assert.AreEqual("k", activity.Baggage.Single().Key);
            Assert.AreEqual("v", activity.Baggage.Single().Value);

            this.aspNetDiagnosticsSource.StopActivity();

            Assert.AreEqual(1, this.sendItems.Count);

            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.AreEqual(currentActivity.RootId, requestTelemetry.Context.Operation.Id);
            Assert.IsNull(requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(currentActivity.Id, requestTelemetry.Id);

            Assert.AreEqual(1, requestTelemetry.Properties.Count);
            Assert.IsTrue(requestTelemetry.Properties.TryGetValue("k", out var v));
            Assert.AreEqual("v", v);
        }
        public void DoubleBeginEndRequestReportsOneTelemetry()
        {
            this.module = this.CreateModule();

            Assert.IsTrue(this.aspNetDiagnosticsSource.StartActivity());
            var activity = Activity.Current;

            Assert.IsTrue(this.aspNetDiagnosticsSource.StartActivity());

            // second Activity is ignored
            Assert.AreEqual(activity, Activity.Current);
            this.aspNetDiagnosticsSource.StopActivity();

            Assert.IsNull(Activity.Current);
            this.aspNetDiagnosticsSource.StopActivity(activity);

            Assert.AreEqual(1, this.sendItems.Count);
            var requestTelemetry = this.sendItems[0] as RequestTelemetry;

            Assert.IsNotNull(requestTelemetry);
            Assert.IsNull(requestTelemetry.Context.Operation.ParentId);
            Assert.AreEqual(activity.TraceId.ToHexString(), requestTelemetry.Context.Operation.Id);
            Assert.AreEqual(activity.SpanId.ToHexString(), requestTelemetry.Id);
        }