-
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 all 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.HasValue) | ||
| { | ||
| activity.SetTag(SemanticConventions.AttributeServerAddress, request.Host.Value); | ||
|
|
||
| if (request.Host.Port is { } port) | ||
| { | ||
| activity.SetTag(SemanticConventions.AttributeServerPort, port); | ||
| } | ||
| } | ||
|
|
||
| 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 opted into ASP.NET Core 10's native OpenTelemetry support. | ||
| // https://github.com/dotnet/aspnetcore/blob/7387de91234d3ef751fa50b3d1bfede4130213ff/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L58-L67 | ||
|
|
||
| private static bool IsOpenTelemetryActivityDataSuppressed() => | ||
| #if NET10_0 | ||
|
Member
Author
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. This isn't |
||
| !AppContext.TryGetSwitch("Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData", out var enabled) || enabled; | ||
|
Member
Author
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. I'll (try to) add tests to validate this scenario if everyone is happy with the change in principle. |
||
| #else | ||
| false; | ||
| #endif | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.