Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instrumentation.AspNet] Fix activity being closed before processing completes #1388

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ public static void StopAspNetActivity(TextMapPropagator textMapPropagator, Activ
Debug.Assert(context.Items[ContextKey] is ContextHolder, "Context item is not an ContextHolder instance.");

var currentActivity = Activity.Current;

aspNetActivity.Stop();
context.Items[ContextKey] = null;

try
Expand All @@ -144,6 +142,7 @@ public static void StopAspNetActivity(TextMapPropagator textMapPropagator, Activ
AspNetTelemetryEventSource.Log.CallbackException(aspNetActivity, "OnStopped", callbackEx);
}

aspNetActivity.Stop();
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
AspNetTelemetryEventSource.Log.ActivityStopped(aspNetActivity);

if (textMapPropagator is not TraceContextPropagator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fixed an issue where activities were stopped incorrectly before processing completed.
Activity processor's `OnEnd` will now happen after `AspNetInstrumentationOptions.Enrich`.
([#1388](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1388))

* Update `OpenTelemetry.Api` to `1.6.0`.
([#1344](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1344))

Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Release together with `OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line. These two packages are always released together.

Fixed an issue where activities were stopped incorrectly before processing completed.
Activity processor's `OnEnd` will now happen after `AspNetInstrumentationOptions.Enrich`.
([#1388](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1388))

## 1.0.0-rc9.9

Released 2023-Jun-09
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,45 @@ public void Fire_Exception_Events()
Assert.Equal(1, callbacksFired);
}

[Fact]
public void Should_Handle_Activity_Events_In_Correct_Order()
{
var eventOrder = new List<string>();
const string ActivityOnStarted = "ActivityOnStarted";
const string ActivityOnStopped = "ActivityOnStarted";
const string OnStartCallback = "OnStartCallback";
const string OnStopCallback = "OnStopCallback";

this.EnableListener(_ => eventOrder.Add(ActivityOnStarted), _ => eventOrder.Add(ActivityOnStopped));

var context = HttpContextHelper.GetFakeHttpContext();
using var rootActivity = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, (_, _) => eventOrder.Add(OnStartCallback));
ActivityHelper.StopAspNetActivity(this.noopTextMapPropagator, rootActivity, context, (_, _) => eventOrder.Add(OnStopCallback));

var expectedOrder = new List<string>()
{
ActivityOnStarted,
OnStartCallback,
OnStopCallback,
ActivityOnStopped,
};

Assert.Equal(expectedOrder, eventOrder);
}

[Fact]
public void Should_Not_Pass_Stopped_Activity_To_Callbacks()
{
this.EnableListener();

var wasStopped = false;
var context = HttpContextHelper.GetFakeHttpContext();
using var rootActivity = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, (activity, _) => wasStopped = activity.IsStopped || wasStopped);
ActivityHelper.StopAspNetActivity(this.noopTextMapPropagator, rootActivity, context, (activity, _) => wasStopped = activity.IsStopped || wasStopped);

Assert.False(wasStopped);
}

private void EnableListener(Action<Activity>? onStarted = null, Action<Activity>? onStopped = null, Func<ActivityContext, ActivitySamplingResult>? onSample = null)
{
Debug.Assert(this.activitySourceListener == null, "Cannot attach multiple listeners in tests.");
Expand Down
Loading