Skip to content

[AspNetCore] Avoid adding tags for traces#3993

Open
martincostello wants to merge 7 commits intoopen-telemetry:mainfrom
martincostello:aspnetcore-avoid-tags-where-possible
Open

[AspNetCore] Avoid adding tags for traces#3993
martincostello wants to merge 7 commits intoopen-telemetry:mainfrom
martincostello:aspnetcore-avoid-tags-where-possible

Conversation

@martincostello
Copy link
Copy Markdown
Member

See martincostello#2 (review).

Relates to #3808.

Changes

Avoid adding tags for ASP.NET Core activities where ASP.NET Core has native support to add them itself.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

Avoid adding tags for ASP.NET Core activities where ASP.NET Core has native support to add them itself.
@github-actions github-actions bot added the comp:instrumentation.aspnetcore Things related to OpenTelemetry.Instrumentation.AspNetCore label Mar 19, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.06%. Comparing base (da86069) to head (22c1885).
⚠️ Report is 26 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3993      +/-   ##
==========================================
+ Coverage   71.94%   72.06%   +0.11%     
==========================================
  Files         458      448      -10     
  Lines       17872    17760     -112     
==========================================
- Hits        12858    12798      -60     
+ Misses       5014     4962      -52     
Flag Coverage Δ
unittests-Instrumentation.AspNet 77.37% <ø> (ø)
unittests-Instrumentation.AspNetCore 70.62% <100.00%> (+0.17%) ⬆️
unittests-Instrumentation.Cassandra ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...umentation.AspNetCore/AspNetCoreInstrumentation.cs 100.00% <100.00%> (ø)
...eInstrumentationTracerProviderBuilderExtensions.cs 95.55% <100.00%> (-0.19%) ⬇️
...tation.AspNetCore/Implementation/HttpInListener.cs 74.17% <100.00%> (+0.88%) ⬆️

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add CHANGELOG entry.
// https://github.com/dotnet/aspnetcore/blob/7387de91234d3ef751fa50b3d1bfede4130213ff/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L58-L67

private static bool IsOpenTelemetryActivityDataSuppressed() =>
#if NET10_0
Copy link
Copy Markdown
Member Author

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.

Fix markdown lint warnings.

private static bool IsOpenTelemetryActivityDataSuppressed() =>
#if NET10_0
!AppContext.TryGetSwitch("Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData", out var enabled) || enabled;
Copy link
Copy Markdown
Member Author

@martincostello martincostello Mar 19, 2026

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.

@martincostello martincostello requested a review from a team March 19, 2026 14:13
@martincostello martincostello marked this pull request as ready for review March 27, 2026 15:03
Copilot AI review requested due to automatic review settings March 27, 2026 15:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates ASP.NET Core tracing instrumentation to avoid setting certain HTTP semantic-convention tags when ASP.NET Core can provide them natively (targeting improved performance on newer runtimes).

Changes:

  • Add runtime/app-context gating to skip setting selected HTTP tags when ASP.NET Core native OpenTelemetry data is enabled.
  • Refactor event handling and small code-style cleanups in instrumentation wiring.
  • Document the behavior and the relevant AppContext switch in the AspNetCore instrumentation changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs Adds .NET 10 gating + AppContext switch check to conditionally avoid manual tag population.
src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md Documents the new behavior and the AppContext switch guidance.
src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs Minor formatting/comment wording updates.
src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentation.cs Minor lambda/dispose style cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

// 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.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

Typo: "opened into" should be "opted into".

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Comment on lines +401 to +402
// 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.
Copy link

Copilot AI Mar 27, 2026

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.

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Fix CHANGELOG and comment.
Fix incorrect pattern matching on non-nullable `PathString`.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 6, 2026

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Apr 6, 2026
@martincostello martincostello added keep-open Prevents issues and pull requests being closed as stale and removed Stale labels Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:instrumentation.aspnetcore Things related to OpenTelemetry.Instrumentation.AspNetCore keep-open Prevents issues and pull requests being closed as stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants