-
Notifications
You must be signed in to change notification settings - Fork 870
[OpenTelemetry] Use Regex source generator #6850
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?
[OpenTelemetry] Use Regex source generator #6850
Conversation
Use `[GeneratedRegex]`, where supported, for statically-known regular expressions. See open-telemetry#6849. Co-Authored-By: Mihail Golubev <211329375+tetolv@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6850 +/- ##
==========================================
+ Coverage 86.90% 86.92% +0.01%
==========================================
Files 263 263
Lines 12382 12378 -4
==========================================
- Hits 10761 10760 -1
+ Misses 1621 1618 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
Using sizoscopeX, using the Regex source generator increases the code size in the test app compared to :
<PropertyGroup>
<IlcGenerateDgmlFile>true</IlcGenerateDgmlFile>
<IlcGenerateMstatFile>true</IlcGenerateMstatFile>
</PropertyGroup>However, that project roots all the dependencies for OTel with Will look at a comparison with a different app. |
|
Using this Lambda of mine (which just has traces), the size is increased from using the source generator:
Will try one more app that also has metrics, but I expect that will similarly be larger. |
|
Similar story with my personal website, code size goes up, mostly caused by the JSON paring Regexes. The metrics one adds 1kb.
|
|
Opening for review/consideration purely from the analyzer resolution perspective, disregarding any native AoT code size impact. |
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.
Pull request overview
This PR modernizes OpenTelemetry's regular expression usage by adopting the [GeneratedRegex] attribute for .NET 8.0+ targets, while maintaining backward compatibility with older frameworks through conditional compilation.
Changes:
- Replaced runtime-compiled regex instances with source-generated regex using
[GeneratedRegex]for .NET 8.0+ targets - Extracted regex patterns into const strings for better maintainability
- Applied code modernization (collection expressions, expression-bodied members)
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderSdk.cs | Updated instrument name validation regex to use source generation on .NET 8.0+, modernized collection initializers and method syntax |
| src/OpenTelemetry/Internal/SelfDiagnosticsConfigParser.cs | Converted all four config parsing regex patterns to use source generation on .NET 8.0+, preserving fallback behavior for older frameworks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.



See #6849.
Changes
[GeneratedRegex], where supported, for statically-known regular expressions.Merge requirement checklist
Unit tests added/updatedAppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)