[AspNetCore] Optimize HttpInListener#4090
[AspNetCore] Optimize HttpInListener#4090martincostello wants to merge 2 commits intoopen-telemetry:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4090 +/- ##
==========================================
+ Coverage 75.62% 75.69% +0.06%
==========================================
Files 455 455
Lines 18238 18278 +40
==========================================
+ Hits 13793 13836 +43
+ Misses 4445 4442 -3 Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
4cd813b to
204d65f
Compare
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Pull request overview
Optimizes ASP.NET Core inbound instrumentation hot paths (notably HttpInListener.OnStopActivity) to reduce allocations and redundant work, including improved gRPC handling and display-name caching.
Changes:
- Adds caching for HTTP route-based activity display names and consolidates HTTP method display-name/tag setting.
- Reworks gRPC method parsing and gRPC attribute enrichment to avoid regex/enum reflection and reduce tag lookups.
- Adds/updates tests covering display name caching and gRPC attribute behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs |
Allocation-reducing refactor for request path handling, enrichment guards, gRPC tag extraction, and gRPC-derived attribute caching. |
src/Shared/RequestDataHelper.cs |
Adds route display-name caching and a helper to set both display name and HTTP method tags together. |
src/Shared/GrpcTagHelper.cs |
Replaces regex parsing with span-based parsing and avoids enum reflection for status validation. |
src/Shared/GrpcStatusCanonicalCode.cs |
Introduces a max-value sentinel for status validation. |
test/OpenTelemetry.Instrumentation.AspNetCore.Tests/GrpcTests.cs |
New tests validating gRPC attribute enrichment behavior in HttpInListener.OnStopActivity. |
test/OpenTelemetry.Contrib.Shared.Tests/RequestDataHelperTests.cs |
Adds tests for display name generation, HTTP method tagging, and caching behavior. |
test/OpenTelemetry.Contrib.Shared.Tests/GrpcTagHelperTests.cs |
Expands coverage to include invalid negative gRPC status codes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add optimizations, recommended by Copilot, to reduce allocations in `HttpInListener.OnStopActivity()`: - Cache activity display names. - Uses slices instead of `Regex`. - Avoid enum reflection. - Null check delegates to avoid try-catch blocks. - Use SetCustomProperty to check for who made the instrumentation. - Avoid running gRPC-related code for HTTP 1.1. - Avoid additional check for the normalized HTTP method. - Use pattern matching on nullable.
213c8a4 to
2629aa6
Compare
|
Will do a final round of benchmark comparisons shortly. |
Benchmark ResultsCopilot Summary
By workload:
The biggest gain is plain BenchmarkDotNet ResultsClick to expandmain
This PR
|
Changes
While looking at some profiles for an OTel instrumented application of mine, I noticed that
HttpInListener.OnStopActivity()came up in the top 5 OTel-related samples.Add optimizations, recommended by Copilot, to reduce allocations in
HttpInListener.OnStopActivity():Regex.See #4090 (comment) for benchmark results.
Merge requirement checklist
Unit tests added/updatedAppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)