-
Notifications
You must be signed in to change notification settings - Fork 773
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
Remove ActivitySourceAdapter from AspNetCore #1654
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,14 +45,12 @@ internal class HttpInListener : ListenerHandler | |
private readonly PropertyFetcher<string> beforeActionTemplateFetcher = new PropertyFetcher<string>("Template"); | ||
private readonly bool hostingSupportsW3C; | ||
private readonly AspNetCoreInstrumentationOptions options; | ||
private readonly ActivitySourceAdapter activitySource; | ||
|
||
public HttpInListener(string name, AspNetCoreInstrumentationOptions options, ActivitySourceAdapter activitySource) | ||
public HttpInListener(string name, AspNetCoreInstrumentationOptions options) | ||
: base(name) | ||
{ | ||
this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3; | ||
this.options = options ?? throw new ArgumentNullException(nameof(options)); | ||
this.activitySource = activitySource; | ||
} | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")] | ||
|
@@ -81,36 +79,37 @@ public override void OnStartActivity(Activity activity, object payload) | |
return; | ||
} | ||
|
||
ActivityContext activityContext = default; | ||
Activity originalActivityCurrent = Activity.Current; | ||
if (activity.ParentSpanId == default) | ||
{ | ||
Activity.Current = null; | ||
} | ||
else | ||
{ | ||
activityContext = new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true); | ||
} | ||
|
||
var request = context.Request; | ||
var textMapPropagator = Propagators.DefaultTextMapPropagator; | ||
if (!this.hostingSupportsW3C || !(textMapPropagator is TraceContextPropagator)) | ||
{ | ||
var ctx = textMapPropagator.Extract(default, request, HttpRequestHeaderValuesGetter); | ||
|
||
if (ctx.ActivityContext.IsValid() | ||
&& ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true)) | ||
{ | ||
// Create a new activity with its parent set from the extracted context. | ||
// This makes the new activity as a "sibling" of the activity created by | ||
// Asp.Net Core. | ||
Activity newOne = new Activity(ActivityNameByHttpInListener); | ||
newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags); | ||
newOne.TraceStateString = ctx.ActivityContext.TraceState; | ||
|
||
// Starting the new activity make it the Activity.Current one. | ||
newOne.Start(); | ||
activity = newOne; | ||
} | ||
|
||
activityContext = ctx.ActivityContext; | ||
if (ctx.Baggage != default) | ||
{ | ||
Baggage.Current = ctx.Baggage; | ||
} | ||
} | ||
|
||
this.activitySource.Start(activity, ActivityKind.Server, ActivitySource); | ||
activity = ActivitySource.StartActivity(ActivityNameByHttpInListener, ActivityKind.Server, activityContext); | ||
|
||
if (activity.IsAllDataRequested) | ||
if (activity == null) | ||
{ | ||
Activity.Current = originalActivityCurrent; | ||
} | ||
|
||
if (activity?.IsAllDataRequested ?? false) | ||
{ | ||
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; | ||
activity.DisplayName = path; | ||
|
@@ -208,8 +207,6 @@ public override void OnStopActivity(Activity activity, object payload) | |
// the one created by the instrumentation. | ||
// And retrieve it here, and set it to Current. | ||
} | ||
|
||
this.activitySource.Stop(activity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like AspNetCore hosting keeps a reference to the Activity it started and that's the one it will stop: So my question is: Does the Activity we create in this approach actually get stopped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CodeBlanch , when you say "this approach", that means the old approach or this new one? For the new one, we are replacing the activity, but, if aspnetcore has a reference there, we might be creating a new one that would never be stopped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eddynaka I meant the new approach sorry 😄
Ya, exactly. That would be a problem! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are stopping our own activity. aspnet core stops their activity. check line 197 for our activity being stopped. (This is a diff. issue in AspNet, as it relies on Activity.Current, so we need to restore it for AspNet, but for AspNetCore we dont. But agree - this is a bit flaky design, and will fall apart if aspnetcore changes their implementation. We'll need to revisit this in .NET6, when they are expected to do some changes.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eddynaka Can you verify the one we're creating is stopped correctly? I see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OnStopActivity gets Activity.Current, which should be the one we created, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya you're right it works fine. Just took it for a spin to verify. |
||
} | ||
|
||
public override void OnCustom(string name, Activity activity, object payload) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to
parentActivityContextOfOriginalActivity
if you don't mind looong names.