[Exporter.Prometheus] Improve performance
Draft
MA
6mo ago+1043+1043+0123456789012345678901234567890123456789-227-227-012345678901234567890123456789
Changes
- Improve the performance of
PrometheusSerializerby:- Using
SearchValues<T>on .NET 8+ - Writing common label value types directly instead of relying on
ToString() - Formatting numeric values directly as UTF-8 on .NET 8+
- Caching serialized metric names, metadata names, units, and static tag prefixes
- Reusing serialized tags across histogram bucket/sum/count output
- Use
stackallocfor small temporary tag buffers on .NET 8+
- Using
Benchmark Results
See https://github.com/open-telemetry/opentelemetry-dotnet/pull/7170#issuecomment-4337589404 for latest results.
Copilot Summary
- Overall duration:
PrometheusSerializer-perfis 0.833x geometric mean versusmain. - Allocations: unchanged in all 3 cases (0.0% / 1.00x).
Per-config summary
| Config | Method | Duration change | Allocation change |
|---|---|---|---|
NumberOfSerializeCalls=1 | WriteMetric | -16.1% / 0.839x | 0.0% / 1.00x |
NumberOfSerializeCalls=1000 | WriteMetric | -20.1% / 0.799x | 0.0% / 1.00x |
NumberOfSerializeCalls=10000 | WriteMetric | -13.9% / 0.861x | 0.0% / 1.00x |
Takeaways
PrometheusSerializer-perf shows consistent runtime improvement.
Detailed Results
Expand to view
main
BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8246/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
.NET 10.0 : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
Job=.NET 10.0 Runtime=.NET 10.0 Toolchain=net10.0
| Method | NumberOfSerializeCalls | Mean | Error | StdDev | Allocated |
|---|---|---|---|---|---|
| WriteMetric | 1 | 3.365 μs | 0.0672 μs | 0.1262 μs | - |
| WriteMetric | 1000 | 3,559.816 μs | 69.3908 μs | 121.5324 μs | - |
| WriteMetric | 10000 | 32,891.695 μs | 582.1268 μs | 940.0263 μs | - |
This PR
BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8246/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.203
[Host] : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
.NET 10.0 : .NET 10.0.7 (10.0.7, 10.0.726.21808), X64 RyuJIT x86-64-v3
Job=.NET 10.0 Runtime=.NET 10.0 Toolchain=net10.0
| Method | NumberOfSerializeCalls | Mean | Error | StdDev | Allocated |
|---|---|---|---|---|---|
| WriteMetric | 1 | 2.823 μs | 0.0561 μs | 0.0873 μs | - |
| WriteMetric | 1000 | 2,844.279 μs | 55.7913 μs | 97.7139 μs | - |
| WriteMetric | 10000 | 28,306.433 μs | 561.5490 μs | 922.6411 μs | - |
Merge requirement checklist
- CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
- Unit tests added/updated
- Appropriate
CHANGELOG.mdfiles updated for non-trivial changes - Changes in public API reviewed (if applicable)