public async Task Can_Stop_Root_Activity_If_It_Is_Broken()
        {
            this.EnableAll();
            var context = HttpContextHelper.GetFakeHttpContext();
            var root    = ActivityHelper.CreateRootActivity(context, false);

            new Activity("child").Start();

            for (int i = 0; i < 2; i++)
            {
                await Task.Run(() =>
                {
                    // when we enter this method, Current is 'child' activity
                    Activity.Current.Stop();

                    // here Current is 'parent', but only in this execution context
                });
            }

            // when we return back here, in the 'parent' execution context
            // Current is still 'child' activity - changes in child context (inside Task.Run)
            // do not affect 'parent' context in which Task.Run is called.
            // But 'child' Activity is stopped, thus consequent calls to Stop will
            // not update Current
            ActivityHelper.StopAspNetActivity(context.Items);
            Assert.True(root.Duration != TimeSpan.Zero);
            Assert.Null(context.Items[ActivityHelper.ActivityKey]);
            Assert.Null(Activity.Current);
        }
        public void Should_Not_Create_RootActivity_If_AspNetListener_Not_Enabled()
        {
            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, true);

            Assert.Null(rootActivity);
        }
        public void Should_Not_Create_RootActivity_If_AspNetActivity_Not_Enabled_With_Arguments()
        {
            var context = HttpContextHelper.GetFakeHttpContext();

            this.EnableAspNetListenerAndDisableActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context, true);

            Assert.Null(rootActivity);
        }
        public void Can_Create_RootActivity_And_Saved_In_HttContext()
        {
            var context = HttpContextHelper.GetFakeHttpContext();

            EnableAspNetListenerAndActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context);

            Assert.NotNull(rootActivity);
            Assert.Same(rootActivity, context.Items[ActivityHelper.ActivityKey]);
        }
        public void Can_Create_RootActivity_And_Start_Activity()
        {
            var context = HttpContextHelper.GetFakeHttpContext();

            EnableAspNetListenerAndActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context);

            Assert.NotNull(rootActivity);
            Assert.True(!string.IsNullOrEmpty(rootActivity.Id));
        }
        public void OnImportActivity_Can_Set_Parent()
        {
            this.EnableAll(onImport: (activity, _) =>
            {
                Assert.Null(activity.ParentId);
                activity.SetParentId("|guid.123.");
            });

            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);

            Assert.Equal("|guid.123.", Activity.Current.ParentId);
        }
        public void Can_Stop_Root_While_Child_Is_Current()
        {
            this.EnableAll();
            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);
            var child        = new Activity("child").Start();

            ActivityHelper.StopAspNetActivity(context.Items);

            Assert.True(child.Duration == TimeSpan.Zero);
            Assert.Null(Activity.Current);
            Assert.Null(context.Items[ActivityHelper.ActivityKey]);
        }
        public void Can_Restore_Activity()
        {
            this.EnableAll();
            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);

            rootActivity.AddTag("k1", "v1");
            rootActivity.AddTag("k2", "v2");

            Activity.Current = null;

            ActivityHelper.RestoreActivityIfNeeded(context.Items);

            Assert.Same(Activity.Current, rootActivity);
        }
        public void Can_Stop_Root_Activity_With_All_Children()
        {
            this.EnableAll();
            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);

            var child = new Activity("child").Start();

            new Activity("grandchild").Start();

            ActivityHelper.StopAspNetActivity(context.Items);

            Assert.True(rootActivity.Duration != TimeSpan.Zero);
            Assert.True(child.Duration == TimeSpan.Zero);
            Assert.Null(rootActivity.Parent);
            Assert.Null(context.Items[ActivityHelper.ActivityKey]);
        }
Exemplo n.º 10
0
        public void Can_Create_RootActivity_And_Ignore_Info_From_Request_Header_If_ParseHeaders_Is_False()
        {
            var requestHeaders = new Dictionary <string, string>
            {
                { ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b2cab6.1." },
                { ActivityExtensions.CorrelationContextHeaderName, this.baggageInHeader }
            };

            var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);

            this.EnableAspNetListenerAndActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context, parseHeaders: false);

            Assert.NotNull(rootActivity);
            Assert.Null(rootActivity.ParentId);
            Assert.Empty(rootActivity.Baggage);
        }
        public void Stop_Root_Activity_With_129_Nesting_Depth()
        {
            this.EnableAll();
            var context = HttpContextHelper.GetFakeHttpContext();
            var root    = ActivityHelper.CreateRootActivity(context, false);

            for (int i = 0; i < 129; i++)
            {
                new Activity("child" + i).Start();
            }

            // can stop any activity regardless of the stack depth
            ActivityHelper.StopAspNetActivity(context.Items);

            Assert.True(root.Duration != TimeSpan.Zero);
            Assert.Null(context.Items[ActivityHelper.ActivityKey]);
            Assert.Null(Activity.Current);
        }
        public void Can_Stop_Lost_Activity()
        {
            this.EnableAll(pair =>
            {
                Assert.NotNull(Activity.Current);
                Assert.Equal(ActivityHelper.AspNetActivityName, Activity.Current.OperationName);
            });
            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);

            rootActivity.AddTag("k1", "v1");
            rootActivity.AddTag("k2", "v2");

            Activity.Current = null;

            ActivityHelper.StopAspNetActivity(context.Items);
            Assert.True(rootActivity.Duration != TimeSpan.Zero);
            Assert.Null(Activity.Current);
            Assert.Null(context.Items[ActivityHelper.ActivityKey]);
        }
        public void OnImportActivity_Is_Called()
        {
            bool     onImportIsCalled = false;
            Activity importedActivity = null;

            this.EnableAll(onImport: (activity, _) =>
            {
                onImportIsCalled = true;
                importedActivity = activity;
                Assert.Null(Activity.Current);
            });

            var context      = HttpContextHelper.GetFakeHttpContext();
            var rootActivity = ActivityHelper.CreateRootActivity(context, false);

            Assert.True(onImportIsCalled);
            Assert.NotNull(importedActivity);
            Assert.Equal(importedActivity, Activity.Current);
            Assert.Equal(importedActivity, rootActivity);
        }
        public void Can_Create_RootActivity_And_Restore_Info_From_Request_Header()
        {
            var requestHeaders = new Dictionary <string, string>
            {
                { ActivityExtensions.RequestIDHeaderName, "|aba2f1e978b2cab6.1" },
                { ActivityExtensions.CorrelationContextHeaderName, _baggageInHeader }
            };

            var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);

            EnableAspNetListenerAndActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context);

            Assert.NotNull(rootActivity);
            Assert.True(rootActivity.ParentId == "|aba2f1e978b2cab6.1");
            var expectedBaggage = _baggageItems.OrderBy(item => item.Value);
            var actualBaggage   = rootActivity.Baggage.OrderBy(item => item.Value);

            Assert.Equal(expectedBaggage, actualBaggage);
        }
        public void Can_Create_RootActivity_From_W3C_Traceparent()
        {
            this.EnableAll();
            var requestHeaders = new Dictionary <string, string>
            {
                { ActivityExtensions.TraceparentHeaderName, "00-0123456789abcdef0123456789abcdef-0123456789abcdef-00" },
            };

            var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);

            this.EnableAspNetListenerAndActivity();
            var rootActivity = ActivityHelper.CreateRootActivity(context, true);

            Assert.NotNull(rootActivity);
            Assert.Equal(ActivityIdFormat.W3C, rootActivity.IdFormat);
            Assert.Equal("00-0123456789abcdef0123456789abcdef-0123456789abcdef-00", rootActivity.ParentId);
            Assert.Equal("0123456789abcdef0123456789abcdef", rootActivity.TraceId.ToHexString());
            Assert.Equal("0123456789abcdef", rootActivity.ParentSpanId.ToHexString());
            Assert.False(rootActivity.Recorded);

            Assert.Null(rootActivity.TraceStateString);
            Assert.Empty(rootActivity.Baggage);
        }