Skip to content

Conversation

@martincostello
Copy link
Member

See #6849.

Changes

  • Use [GeneratedRegex], where supported, for statically-known regular expressions.
  • Apply analyzer suggestions to simplify and/or modernise code.

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)

Use `[GeneratedRegex]`, where supported, for statically-known regular expressions.

See open-telemetry#6849.

Co-Authored-By: Mihail Golubev <211329375+tetolv@users.noreply.github.com>
@github-actions github-actions bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Jan 27, 2026
@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.92%. Comparing base (496e4ac) to head (c9ac6c5).
⚠️ Report is 12 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
unittests-Project-Experimental 86.71% <100.00%> (-0.01%) ⬇️
unittests-Project-Stable 86.83% <100.00%> (+0.01%) ⬆️

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

Files with missing lines Coverage Δ
...nTelemetry/Internal/SelfDiagnosticsConfigParser.cs 88.13% <100.00%> (ø)
...lemetry/Metrics/Builder/MeterProviderBuilderSdk.cs 100.00% <100.00%> (+1.63%) ⬆️

... and 1 file with indirect coverage changes

@martincostello
Copy link
Member Author

martincostello commented Jan 27, 2026

Using sizoscopeX, using the Regex source generator increases the code size in the test app compared to :

image

.mstat files generated by adding:

<PropertyGroup>
  <IlcGenerateDgmlFile>true</IlcGenerateDgmlFile>
  <IlcGenerateMstatFile>true</IlcGenerateMstatFile>
</PropertyGroup>

However, that project roots all the dependencies for OTel with TrimmerRootAssembly so all the code is retained, which isn't a fair comparison.

Will look at a comparison with a different app.

@martincostello
Copy link
Member Author

Using this Lambda of mine (which just has traces), the size is increased from using the source generator:

image

Will try one more app that also has metrics, but I expect that will similarly be larger.

@martincostello
Copy link
Member Author

Similar story with my personal website, code size goes up, mostly caused by the JSON paring Regexes. The metrics one adds 1kb.

image

@martincostello
Copy link
Member Author

Opening for review/consideration purely from the analyzer resolution perspective, disregarding any native AoT code size impact.

@martincostello martincostello marked this pull request as ready for review January 28, 2026 09:56
@martincostello martincostello requested a review from a team as a code owner January 28, 2026 09:56
Copilot AI review requested due to automatic review settings January 28, 2026 09:56
Copy link

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants