Skip to content

[Exporter.Prometheus] Add allow-utf-8 escaping#7440

Merged
Kielek merged 35 commits into
open-telemetry:mainfrom
martincostello:gh-7246-allow-utf-8
Jul 3, 2026
Merged

[Exporter.Prometheus] Add allow-utf-8 escaping#7440
Kielek merged 35 commits into
open-telemetry:mainfrom
martincostello:gh-7246-allow-utf-8

Conversation

@martincostello

@martincostello martincostello commented Jun 21, 2026

Copy link
Copy Markdown
Member

Fixes #7246

Changes

Add support for the allow-utf-8 escaping scheme.

Benchmark Results

Summary (generated by Claude 🐙)

Prometheus serializer allocation improvements (this branch vs gh-7246)

PrometheusSerializerBenchmarks (Prometheus text v1.0.0 / underscores), BenchmarkDotNet Default job + memory diagnoser, .NET 10.0.9, i7-13700H. Both refs benchmarked in the same session (gh-7246 from a fresh worktree; this branch = allow-utf-8 + label-key allocation optimizations). Allocation normalised to KB per single op.

Benchmark Metric gh-7246 This branch Δ
WriteMetric Allocated 4.12 KB 3.28 KB −20.4%
Mean (×1) 2,582 ns 2,485 ns −3.7%
Mean (×10000) 26.30 ms 25.24 ms −4.0%
WriteHistogramMetric Allocated 1.26 KB 1.10 KB −12.5%
Mean (×10000) 14.41 ms 14.05 ms −2.5%
WriteMetricWithTypedLabels Allocated 1.16 KB 0.73 KB −37.3%
Mean (×10000) 4.47 ms 3.90 ms −12.7%

Allocation scales linearly across the 1 / 1000 / 10000 call-count params, confirming the per-op figures.

Summary: allocation is down 13–37% in core serialization with neutral-to-faster throughput (StdDevs ~1% this run). The reduction comes from no longer allocating a StringBuilder + string for every output label key — already-valid keys are now returned/written with zero allocation. The allow-utf-8 feature itself is allocation-neutral on the common path.

Detailed Results

Expand to view

#7439

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8655/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.301
  [Host]     : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
Method NumberOfSerializeCalls Mean Error StdDev Gen0 Allocated
WriteMetric 1 2,581.5 ns 27.57 ns 23.03 ns 0.3357 4.12 KB
WriteHistogramMetric 1 1,481.5 ns 29.18 ns 28.66 ns 0.1011 1.26 KB
WriteMetricWithTypedLabels 1 441.4 ns 6.30 ns 5.89 ns 0.0939 1.16 KB
WriteMetric 1000 2,572,046.6 ns 37,995.08 ns 31,727.61 ns 335.9375 4117.19 KB
WriteHistogramMetric 1000 1,475,238.5 ns 25,244.04 ns 23,613.29 ns 101.5625 1257.81 KB
WriteMetricWithTypedLabels 1000 433,982.6 ns 4,025.90 ns 3,143.16 ns 94.2383 1156.25 KB
WriteMetric 10000 26,298,293.8 ns 523,925.09 ns 643,427.12 ns 3343.7500 41171.88 KB
WriteHistogramMetric 10000 14,412,322.4 ns 259,791.33 ns 243,008.97 ns 1015.6250 12578.13 KB
WriteMetricWithTypedLabels 10000 4,467,305.4 ns 78,161.99 ns 69,288.56 ns 937.5000 11562.5 KB

This PR

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8655/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.301
  [Host]     : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
Method NumberOfSerializeCalls Mean Error StdDev Gen0 Allocated
WriteMetric 1 2,485.1 ns 29.39 ns 24.54 ns 0.2670 3360 B
WriteHistogramMetric 1 1,417.2 ns 23.14 ns 21.65 ns 0.0896 1128 B
WriteMetricWithTypedLabels 1 397.9 ns 7.65 ns 6.38 ns 0.0591 744 B
WriteMetric 1000 2,519,263.3 ns 43,087.04 ns 40,303.65 ns 265.6250 3360000 B
WriteHistogramMetric 1000 1,361,864.6 ns 17,776.48 ns 16,628.13 ns 89.8438 1128000 B
WriteMetricWithTypedLabels 1000 391,383.2 ns 6,740.23 ns 8,023.77 ns 59.0820 744000 B
WriteMetric 10000 25,242,336.2 ns 329,968.13 ns 308,652.39 ns 2656.2500 33600000 B
WriteHistogramMetric 10000 14,048,737.1 ns 273,508.46 ns 242,458.12 ns 890.6250 11280000 B
WriteMetricWithTypedLabels 10000 3,901,919.2 ns 58,909.57 ns 55,104.05 ns 589.8438 7440000 B

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)

Add missing `in` modifiers on methods accepting parameters of type `PrometheusProtocol`.
Add support for the `dots` and `values` escaping schemes.

Contributes to open-telemetry#7246.
Fix incorrectly sorted `[InlineData]` attributes.
- Update comments.
- Add `Debug.Assert()`.
- Add PR number to CHANGELOGs.
- Add missing coverage.
- Remove unused code.
Add support for the `allow-utf-8` escaping scheme.

Fixes open-telemetry#7246.
@github-actions github-actions Bot added pkg:OpenTelemetry.Exporter.Prometheus.AspNetCore Issues related to OpenTelemetry.Exporter.Prometheus.AspNetCore NuGet package pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package labels Jun 21, 2026
Comment thread src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md Outdated
@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.31933% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.41%. Comparing base (f4fd940) to head (5bfff05).
⚠️ Report is 11 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ernal/Shared/Serialization/TextFormatSerializer.cs 97.74% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7440      +/-   ##
==========================================
+ Coverage   90.12%   90.41%   +0.28%     
==========================================
  Files         285      281       -4     
  Lines       15343    15414      +71     
==========================================
+ Hits        13828    13936     +108     
+ Misses       1515     1478      -37     
Flag Coverage Δ
unittests-PowerShellScripts ?
unittests-Project-Experimental 90.10% <98.31%> (+0.02%) ⬆️
unittests-Project-Stable 90.21% <98.31%> (+0.07%) ⬆️
unittests-UnstableCoreLibraries-Experimental 50.55% <98.31%> (+0.87%) ⬆️

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

Files with missing lines Coverage Δ
...metheus.AspNetCore/PrometheusExporterMiddleware.cs 100.00% <100.00%> (ø)
...s.HttpListener/Internal/PrometheusHeadersParser.cs 98.79% <100.00%> (-0.06%) ⬇️
...HttpListener/Internal/Shared/PrometheusEscaping.cs 100.00% <100.00%> (ø)
...s.HttpListener/Internal/Shared/PrometheusMetric.cs 99.58% <100.00%> (+0.04%) ⬆️
...HttpListener/Internal/Shared/PrometheusProtocol.cs 98.07% <100.00%> (+0.03%) ⬆️
...rnal/Shared/Serialization/OpenMetricsSerializer.cs 100.00% <100.00%> (ø)
...al/Shared/Serialization/OpenMetricsV1Serializer.cs 100.00% <100.00%> (ø)
...Shared/Serialization/PrometheusTextV1Serializer.cs 100.00% <100.00%> (ø)
...ernal/Shared/Serialization/TextFormatSerializer.cs 99.48% <97.74%> (-0.52%) ⬇️

... and 16 files with indirect coverage changes

Add a new benchmark that compares the different text formats and escaping schemes.
@github-actions github-actions Bot added the perf Performance related label Jun 21, 2026
@martincostello martincostello added the keep-open Prevents issues and pull requests being closed as stale label Jun 28, 2026
This was referenced Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance related pkg:OpenTelemetry.Exporter.Prometheus.AspNetCore Issues related to OpenTelemetry.Exporter.Prometheus.AspNetCore NuGet package pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package spec-compliance Issues related to compliance with the OpenTelemetry Specification

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support additional Prometheus UTF-8 escaping schemes

3 participants