-
Notifications
You must be signed in to change notification settings - Fork 863
[.NET7.0] AspNetCore ActivitySource Migration #3391
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
Changes from 6 commits
2012f10
7dcb08f
e6084c3
a338f87
278ba68
f2a8ddd
14f804d
bf9f1b8
fc809ab
cd00842
837ab88
8de7880
8685dce
b766a8b
e7e813f
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 |
|---|---|---|
|
|
@@ -35,6 +35,10 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation | |
| internal class HttpInListener : ListenerHandler | ||
| { | ||
| internal const string ActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn"; | ||
| internal static readonly bool IsNet7OrGreater; | ||
|
|
||
| // https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/WebHostBuilder.cs#L291 | ||
| internal static readonly string FrameworkActivitySourceName = "Microsoft.AspNetCore"; | ||
alanwest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
vishweshbankwar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName(); | ||
| internal static readonly string ActivitySourceName = AssemblyName.Name; | ||
| internal static readonly Version Version = AssemblyName.Version; | ||
|
|
@@ -50,6 +54,18 @@ internal class HttpInListener : ListenerHandler | |
| private readonly PropertyFetcher<string> beforeActionTemplateFetcher = new("Template"); | ||
| private readonly AspNetCoreInstrumentationOptions options; | ||
|
|
||
| static HttpInListener() | ||
| { | ||
| try | ||
| { | ||
| IsNet7OrGreater = typeof(HttpRequest).Assembly.GetName().Version.Major >= 7; | ||
| } | ||
| catch (Exception) | ||
| { | ||
| IsNet7OrGreater = false; | ||
| } | ||
| } | ||
|
|
||
| public HttpInListener(AspNetCoreInstrumentationOptions options) | ||
| : base(DiagnosticSourceName) | ||
| { | ||
|
|
@@ -96,8 +112,15 @@ public override void OnStartActivity(Activity activity, object payload) | |
| // 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. | ||
| #if NET7_0_OR_GREATER | ||
| // For NET7.0 onwards activity is created using ActivitySource so, | ||
| // we will use the source of the activity to create the new one. | ||
| Activity newOne; | ||
| newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext); | ||
vishweshbankwar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #else | ||
| Activity newOne = new Activity(ActivityOperationName); | ||
| newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags); | ||
| #endif | ||
| newOne.TraceStateString = ctx.ActivityContext.TraceState; | ||
|
|
||
| newOne.SetTag("IsCreatedByInstrumentation", bool.TrueString); | ||
|
|
@@ -135,8 +158,11 @@ public override void OnStartActivity(Activity activity, object payload) | |
| return; | ||
| } | ||
|
|
||
| ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource); | ||
| ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server); | ||
| if (!IsNet7OrGreater) | ||
|
||
| { | ||
| ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource); | ||
| ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server); | ||
| } | ||
|
|
||
| var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; | ||
| activity.DisplayName = path; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.