public async Task Stop_Restored_Activity_Fires_Event() { var context = HttpContextHelper.GetFakeHttpContext(); var root = new Activity("root"); await Task.Run(() => { root.Start(); context.Items[ActivityHelper.ActivityKey] = root; }); ActivityHelper.RestoreActivityIfNeeded(context.Items); Activity restored = Activity.Current; var events = new ConcurrentQueue <KeyValuePair <string, object> >(); this.EnableAll((kvp) => events.Enqueue(kvp)); ActivityHelper.StopRestoredActivity(restored, context); Assert.Single(events); string eventName = events.Single().Key; object eventPayload = events.Single().Value; Assert.Equal(ActivityHelper.AspNetActivityRestoredStopName, eventName); Assert.Same(restored, eventPayload.GetProperty("Activity")); }
public void Do_Not_Restore_Activity_When_There_Is_No_Activity_In_Context() { this.EnableAll(); ActivityHelper.RestoreActivityIfNeeded(HttpContextHelper.GetFakeHttpContext().Items); 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 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_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_Start_Activity() { var context = HttpContextHelper.GetFakeHttpContext(); EnableAspNetListenerAndActivity(); var rootActivity = ActivityHelper.CreateRootActivity(context); Assert.NotNull(rootActivity); Assert.True(!string.IsNullOrEmpty(rootActivity.Id)); }
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_Stop_Activity_Without_AspNetListener_Enabled() { var context = HttpContextHelper.GetFakeHttpContext(); var rootActivity = CreateActivity(); rootActivity.Start(); Thread.Sleep(100); ActivityHelper.StopAspNetActivity(rootActivity, context); Assert.True(rootActivity.Duration != TimeSpan.Zero); Assert.Null(rootActivity.Parent); Assert.Null(context.Items[ActivityHelper.ActivityKey]); }
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 Do_Not_Restore_Activity_When_It_Is_Not_Lost() { var root = new Activity("root").Start(); var context = HttpContextHelper.GetFakeHttpContext(); context.Items[ActivityHelper.ActivityKey] = root; var module = new TelemetryCorrelationHttpModule(); ActivityHelper.RestoreActivityIfNeeded(context.Items); Assert.Equal(root, Activity.Current); }
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() { var context = HttpContextHelper.GetFakeHttpContext(); var rootActivity = CreateActivity(); rootActivity.Start(); new Activity("child").Start(); new Activity("grandchild").Start(); ActivityHelper.StopAspNetActivity(rootActivity, context); Assert.True(rootActivity.Duration != TimeSpan.Zero); Assert.Null(rootActivity.Parent); Assert.Null(context.Items[ActivityHelper.ActivityKey]); }
public void Can_Stop_Child_Activity_With_All_Children() { var context = HttpContextHelper.GetFakeHttpContext(); var rootActivity = this.CreateActivity(); rootActivity.Start(); var child = new Activity("child").Start(); new Activity("grandchild").Start(); ActivityHelper.StopAspNetActivity(child, context.Items); Assert.True(child.Duration != TimeSpan.Zero); Assert.Equal(rootActivity, Activity.Current); Assert.Null(context.Items[ActivityHelper.ActivityKey]); }
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]); }
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 Stop_Root_Activity_With_129_Nesting_Depth() { var context = HttpContextHelper.GetFakeHttpContext(); var root = new Activity("root").Start(); context.Items[ActivityHelper.ActivityKey] = root; for (int i = 0; i < 129; i++) { new Activity("child" + i).Start(); } // we do not allow more than 128 nested activities here // only to protect from hypothetical cycles in Activity stack Assert.False(ActivityHelper.StopAspNetActivity(root, context.Items)); Assert.NotNull(context.Items[ActivityHelper.ActivityKey]); Assert.Null(Activity.Current); }
public async Task Can_Restore_Activity() { var rootActivity = this.CreateActivity(); rootActivity.AddTag("k1", "v1"); rootActivity.AddTag("k2", "v2"); var context = HttpContextHelper.GetFakeHttpContext(); await Task.Run(() => { rootActivity.Start(); context.Items[ActivityHelper.ActivityKey] = rootActivity; }); Assert.Null(Activity.Current); ActivityHelper.RestoreActivityIfNeeded(context.Items); this.AssertIsRestoredActivity(rootActivity, Activity.Current); }
public async Task Stop_Restored_Activity_Deletes_It_From_Items() { var context = HttpContextHelper.GetFakeHttpContext(); var root = new Activity("root"); await Task.Run(() => { root.Start(); context.Items[ActivityHelper.ActivityKey] = root; }); ActivityHelper.RestoreActivityIfNeeded(context.Items); var child = Activity.Current; ActivityHelper.StopRestoredActivity(child, context); Assert.NotNull(context.Items[ActivityHelper.ActivityKey]); Assert.Null(context.Items[ActivityHelper.RestoredActivityKey]); }
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 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 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 async Task Can_Restore_Activity() { var rootActivity = CreateActivity(); var context = HttpContextHelper.GetFakeHttpContext(); await Task.Run(() => { rootActivity.Start(); context.Items[ActivityHelper.ActivityKey] = rootActivity; }); Assert.Null(Activity.Current); var restoredActivity = ActivityHelper.RestoreCurrentActivity(rootActivity); Assert.NotNull(restoredActivity); Assert.True(rootActivity.Id == restoredActivity.ParentId); Assert.True(!string.IsNullOrEmpty(restoredActivity.Id)); 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); }