Skip to content

[Exporter.Prometheus] Update Accept/Content-Type handling to consider escaping#7209

Merged
martincostello merged 10 commits into
open-telemetry:mainfrom
martincostello:prometheus-content-type-escaping
May 5, 2026
Merged

[Exporter.Prometheus] Update Accept/Content-Type handling to consider escaping#7209
martincostello merged 10 commits into
open-telemetry:mainfrom
martincostello:prometheus-content-type-escaping

Conversation

@martincostello

Copy link
Copy Markdown
Member

Builds on top of #7208.

Changes

  • Add missing SHOULD requirement to specify the escaping value for 1.0.0 protocols.

    For PrometheusText1.0.0 and OpenMetricsText1.0.0 protocols, the Accept header SHOULD include an escaping scheme parameter: escaping=<scheme>

  • Update PrometheusSerializer to be compliant with escaping=underscores when using OpenMetrics.

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)

@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 Apr 29, 2026
@codecov

codecov Bot commented Apr 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.87%. Comparing base (0189770) to head (0115430).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7209      +/-   ##
==========================================
+ Coverage   89.86%   89.87%   +0.01%     
==========================================
  Files         272      272              
  Lines       13489    13533      +44     
==========================================
+ Hits        12122    12163      +41     
- Misses       1367     1370       +3     
Flag Coverage Δ
unittests-Project-Experimental 89.73% <100.00%> (-0.09%) ⬇️
unittests-Project-Stable 89.63% <100.00%> (-0.15%) ⬇️
unittests-UnstableCoreLibraries-Experimental 45.29% <100.00%> (+0.26%) ⬆️

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

Files with missing lines Coverage Δ
...metheus.AspNetCore/PrometheusExporterMiddleware.cs 98.36% <100.00%> (+0.11%) ⬆️
...tpListener/Internal/PrometheusCollectionManager.cs 87.58% <100.00%> (ø)
...s.HttpListener/Internal/PrometheusHeadersParser.cs 98.33% <100.00%> (+0.18%) ⬆️
...ometheus.HttpListener/Internal/PrometheusMetric.cs 99.43% <100.00%> (+0.06%) ⬆️
...heus.HttpListener/Internal/PrometheusSerializer.cs 98.73% <100.00%> (+0.08%) ⬆️
...s.HttpListener/Internal/PrometheusSerializerExt.cs 98.82% <100.00%> (ø)
....Prometheus.HttpListener/PrometheusHttpListener.cs 88.11% <100.00%> (ø)

... and 3 files with indirect coverage changes

@martincostello martincostello force-pushed the prometheus-content-type-escaping branch from 40e1403 to 1dd4291 Compare April 29, 2026 12:17
Comment thread src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md Outdated
- Add missing SHOULD requirement to specify the `escaping` value for 1.0.0 protocols.
- Update `PrometheusSerializer` to be compliant with `escaping=underscores` when using OpenMetrics.
@martincostello martincostello force-pushed the prometheus-content-type-escaping branch from c3ebfae to 0ba8e0d Compare April 30, 2026 14:43
@martincostello martincostello marked this pull request as ready for review April 30, 2026 14:44
@martincostello martincostello requested a review from a team as a code owner April 30, 2026 14:44
Copilot AI review requested due to automatic review settings April 30, 2026 14:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Prometheus exporters’ content negotiation and serialization to align with Prometheus/OpenMetrics “escaping” requirements, particularly escaping=underscores for OpenMetrics 1.0.0.

Changes:

  • Emit escaping=underscores in OpenMetrics Content-Type responses and require escaping=underscores when an escaping parameter is explicitly provided in Accept.
  • Update serializer label/metric name handling to better match underscore-based escaping rules (including collapsing runs of invalid characters).
  • Expand unit + fuzz + integration test coverage for the new behavior and update changelogs.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs Adds coverage for label-key normalization differences between Prometheus vs OpenMetrics paths.
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusMetricTests.cs Adds OpenMetrics metric-name escaping/unit normalization test cases (leading digits, collapsing, unsupported chars).
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs Updates expected OpenMetrics Content-Type to include escaping=underscores.
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHeadersParserTests.cs Adds Accept header parsing test cases for escaping=underscores and rejects unsupported schemes.
test/OpenTelemetry.Exporter.Prometheus.HttpListener.FuzzTests/PrometheusSerializerFuzzTests.cs Splits fuzz coverage for Prometheus vs OpenMetrics label-key writers and updates reference implementations.
test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs Updates Accept negotiation and Content-Type expectations for escaping parameter handling.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs Emits OpenMetrics Content-Type with escaping=underscores.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializerExt.cs Threads openMetricsRequested through tag writing so label-key escaping matches negotiated format.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs Adds OpenMetrics-aware label-key writing and passes openMetricsRequested through label/tag/scope/target-info writers.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusMetric.cs Introduces EscapeOpenMetricsName and adjusts OpenMetrics naming/unit suffix logic for underscore escaping.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusHeadersParser.cs Extends HttpListener-side Accept parsing to validate escaping=underscores when present.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs Updates internal calls to serializer APIs that now require openMetricsRequested.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md Documents escaping/serialization behavior changes.
src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs Emits OpenMetrics Content-Type with escaping=underscores and validates escaping parameter in Accept.
src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md Documents escaping/serialization behavior changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Fix missing prefixing for metrics that start with a digit.
martincostello and others added 2 commits May 5, 2026 10:14
- Fix escaping behaviour for unit suffixes.
- Fix incorrect label escaping for `:`.
@martincostello

Copy link
Copy Markdown
Member Author

Need to patch the fuzz tests.

Update generator after changes in 2a588a0.
This was referenced Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants