Repository navigation
[OpenTelemetry] Reduce redaction allocations - #8102
martincostello merged 2 commits into
Conversation
Refactor redaction of environment variables to avoid allocations by using spans. Assisted-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pull request dashboard statusMerged · refreshed 2026-11-10 01:06 UTC Status above doesn't look right?
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the resource-attribute redaction allocation optimization.
No open issues 🎉
What changed in this PR
Reduces allocations when redacting resource attributes in the SDK's opt-in self-diagnostics output, while preserving the existing redaction policy.
Changes:
- Replaces comma and equals-sign string splitting with span-based parsing.
- Appends keys directly from the original string.
- Uses span-based safe-key lookup on modern .NET, retaining a string fallback for older targets.
| File | Description |
|---|---|
src/OpenTelemetry/Internal/Diagnostics/SelfDiagnosticsEnvironmentVariablePolicy.cs |
Refactors resource-attribute parsing and safe-key lookup to reduce temporary allocations. |
Review effort: Balanced
Avoid using indexer so the code compiles for all TFMs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8102 +/- ##
==========================================
+ Coverage 93.09% 93.11% +0.01%
==========================================
Files 397 397
Lines 23335 23343 +8
Branches 4359 4361 +2
==========================================
+ Hits 21724 21735 +11
+ Misses 1103 1100 -3
Partials 508 508
Flags with carried forward coverage won't be shown. Click here to find out more.
|
| var separator = remaining.IndexOf(','); | ||
| var pair = separator < 0 ? remaining : remaining.Slice(0, separator); | ||
|
|
||
| if (!first) |
There was a problem hiding this comment.
Nit: this block adds the comma without checking whether the pair could be empty or malformed and would end up with a comma without its content.
Input Output
service.name=checkout,broken service.name=checkout,[REDACTED]
service.name=checkout, service.name=checkout,
service.name=checkout,, service.name=checkout,,
Although this is the same behavior as before the change and not in scope of this PR.
Changes
Refactor redaction of environment variables to avoid allocations by using spans.
I had a GitHub Copilot cloud agent do a performance optimisation sweep, and it found this. Not on the hot-path and opt-in, but big enough to consider taking.
Benchmarks
BenchmarkDotNet v0.16.0-preview.2, Linux Ubuntu 24.04.3 LTS, Intel Xeon Platinum 8370C CPU 2.80GHz, .NET 11.0.0 (11.0.0-rc.3.26522.148), DefaultJob:
Benchmarks code
Merge requirement checklist
Tests added/updatedCHANGELOG.mdfile(s) updated for non-trivial changesChanges in public API reviewed (if applicable)Footnotes
Yes, I can answer maintainer questions about the content of this PR without using AI. ↩