-
Notifications
You must be signed in to change notification settings - Fork 376
[AspNetCore] Avoid adding tags for traces #3993
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
5d40e60
4423e89
1ea52fd
2409aa4
e83d19c
b5abf77
22c1885
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ internal class HttpInListener : ListenerHandler | |||||||||||||
| #pragma warning restore IDE0370 // Suppression is unnecessary | ||||||||||||||
| internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString()); | ||||||||||||||
| internal static readonly bool Net7OrGreater = Environment.Version.Major >= 7; | ||||||||||||||
| internal static readonly bool Net10OrGreater = Environment.Version.Major >= 10; | ||||||||||||||
|
|
||||||||||||||
| private const string DiagnosticSourceName = "Microsoft.AspNetCore"; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -47,13 +48,15 @@ internal class HttpInListener : ListenerHandler | |||||||||||||
| private static readonly PropertyFetcher<Exception> ExceptionPropertyFetcher = new("Exception"); | ||||||||||||||
|
|
||||||||||||||
| private readonly AspNetCoreTraceInstrumentationOptions options; | ||||||||||||||
| private readonly bool nativeAspNetCoreOpenTelemetrySuppressed; | ||||||||||||||
|
|
||||||||||||||
| public HttpInListener(AspNetCoreTraceInstrumentationOptions options) | ||||||||||||||
| : base(DiagnosticSourceName) | ||||||||||||||
| { | ||||||||||||||
| Guard.ThrowIfNull(options); | ||||||||||||||
|
|
||||||||||||||
| this.options = options; | ||||||||||||||
| this.nativeAspNetCoreOpenTelemetrySuppressed = IsOpenTelemetryActivityDataSuppressed(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public override void OnEventWritten(string name, object? payload) | ||||||||||||||
|
|
@@ -63,24 +66,18 @@ public override void OnEventWritten(string name, object? payload) | |||||||||||||
| switch (name) | ||||||||||||||
| { | ||||||||||||||
| case OnStartEvent: | ||||||||||||||
| { | ||||||||||||||
| this.OnStartActivity(activity, payload); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| this.OnStartActivity(activity, payload); | ||||||||||||||
| break; | ||||||||||||||
| case OnStopEvent: | ||||||||||||||
| { | ||||||||||||||
| this.OnStopActivity(activity, payload); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| case OnStopEvent: | ||||||||||||||
| this.OnStopActivity(activity, payload); | ||||||||||||||
| break; | ||||||||||||||
|
|
||||||||||||||
| case OnUnhandledHostingExceptionEvent: | ||||||||||||||
| case OnUnHandledDiagnosticsExceptionEvent: | ||||||||||||||
| { | ||||||||||||||
| this.OnException(activity, payload); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| this.OnException(activity, payload); | ||||||||||||||
| break; | ||||||||||||||
|
|
||||||||||||||
| default: | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -176,19 +173,38 @@ public void OnStartActivity(Activity activity, object? payload) | |||||||||||||
| ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // See the spec: https://github.com/open-telemetry/semantic-conventions/blob/v1.40.0/docs/http/http-spans.md | ||||||||||||||
| var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; | ||||||||||||||
|
|
||||||||||||||
| TelemetryHelper.RequestDataHelper.SetActivityDisplayName(activity, request.Method); | ||||||||||||||
|
|
||||||||||||||
| // see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md | ||||||||||||||
| // ASP.NET Core 10 does not support OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS so we | ||||||||||||||
| // still need to set the HTTP method tag so that any override by the user is honoured. | ||||||||||||||
| TelemetryHelper.RequestDataHelper.SetHttpMethodTag(activity, request.Method); | ||||||||||||||
|
|
||||||||||||||
| if (request.Host.HasValue) | ||||||||||||||
| if (!Net10OrGreater || this.nativeAspNetCoreOpenTelemetrySuppressed) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeServerAddress, request.Host.Host); | ||||||||||||||
| if (request.Host is { } host) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeServerAddress, host.Host); | ||||||||||||||
|
|
||||||||||||||
| if (request.Host.Port is { } port) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeServerPort, port); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| if (request.Host.Port.HasValue) | ||||||||||||||
| if (request.Headers.TryGetValue("User-Agent", out var values)) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeServerPort, request.Host.Port.Value); | ||||||||||||||
| var userAgent = values.Count > 0 ? values[0] : null; | ||||||||||||||
| if (!string.IsNullOrEmpty(userAgent)) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUserAgentOriginal, userAgent); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUrlScheme, request.Scheme); | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUrlPath, path); | ||||||||||||||
| } | ||||||||||||||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| if (request.QueryString.HasValue) | ||||||||||||||
|
|
@@ -203,21 +219,8 @@ public void OnStartActivity(Activity activity, object? payload) | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| TelemetryHelper.RequestDataHelper.SetHttpMethodTag(activity, request.Method); | ||||||||||||||
|
|
||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUrlScheme, request.Scheme); | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUrlPath, path); | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeNetworkProtocolVersion, RequestDataHelper.GetHttpProtocolVersion(request.Protocol)); | ||||||||||||||
|
|
||||||||||||||
| if (request.Headers.TryGetValue("User-Agent", out var values)) | ||||||||||||||
| { | ||||||||||||||
| var userAgent = values.Count > 0 ? values[0] : null; | ||||||||||||||
| if (!string.IsNullOrEmpty(userAgent)) | ||||||||||||||
| { | ||||||||||||||
| activity.SetTag(SemanticConventions.AttributeUserAgentOriginal, userAgent); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
| this.options.EnrichWithHttpRequest?.Invoke(activity, request); | ||||||||||||||
|
|
@@ -394,4 +397,15 @@ private static void AddGrpcAttributes(Activity activity, string grpcMethod, Http | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // ASP.NET Core 10 does not generate OpenTelemetry tags by default so we can only take the optimal | ||||||||||||||
| // path if the user has not explicitly opened into ASP.NET Core 10's native OpenTelemetry support. | ||||||||||||||
|
||||||||||||||
| // path if the user has not explicitly opened into ASP.NET Core 10's native OpenTelemetry support. | |
| // path if the user has not explicitly opted into ASP.NET Core 10's native OpenTelemetry support. |
Outdated
Copilot
AI
Mar 27, 2026
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.
The comment about when the "optimal path" is taken appears inverted. The code skips manual tag population only when native OpenTelemetry data is not suppressed (i.e., the suppress switch is set to false), so the comment should reflect that this path is only valid when the user has explicitly enabled ASP.NET Core's native OpenTelemetry tags.
| // ASP.NET Core 10 does not generate OpenTelemetry tags by default so we can only take the optimal | |
| // path if the user has not explicitly opened into ASP.NET Core 10's native OpenTelemetry support. | |
| // ASP.NET Core 10 does not generate OpenTelemetry tags by default, so we can only take the optimal | |
| // path if the user has explicitly opted into ASP.NET Core 10's native OpenTelemetry support. |
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.
This isn't _OR_GREATER as .NET 11 swaps the default to false.
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.
I'll (try to) add tests to validate this scenario if everyone is happy with the change in principle.
Uh oh!
There was an error while loading. Please reload this page.