Skip to content

[Exporter.Prometheus] Fix Accept header handling#7208

Merged
martincostello merged 4 commits into
open-telemetry:mainfrom
martincostello:prometheus-content-negotiation
Apr 30, 2026
Merged

[Exporter.Prometheus] Fix Accept header handling#7208
martincostello merged 4 commits into
open-telemetry:mainfrom
martincostello:prometheus-content-negotiation

Conversation

@martincostello

@martincostello martincostello commented Apr 29, 2026

Copy link
Copy Markdown
Member

Changes

Update the handling of the Accept header to follow the requirements documented in Scrape protocol content negotiation.

The behaviour isn't 100% compliant yet - it requires some tweaks to the serializer to be fully-compliant as I've not included it here and are in #7209.

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)

- Update the handling of the `Accept` header to follow the requirements documented in https://prometheus.io/docs/instrumenting/content_negotiation/.
- Removed unused code.
@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
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusSerializerExt.cs" Link="Includes/PrometheusSerializerExt.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusType.cs" Link="Includes/PrometheusType.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusMetric.cs" Link="Includes/PrometheusMetric.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Prometheus.HttpListener\Internal\PrometheusHeadersParser.cs" Link="Includes/PrometheusHeadersParser.cs" />

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was removed in favour of a native implementation as ASP.NET Core has built-in parsers for the Accept header and we were manually re-parsing it.

@codecov

codecov Bot commented Apr 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.64865% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.68%. Comparing base (24832b1) to head (749cdab).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...s.HttpListener/Internal/PrometheusHeadersParser.cs 97.91% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7208   +/-   ##
=======================================
  Coverage   89.67%   89.68%           
=======================================
  Files         272      272           
  Lines       13378    13434   +56     
=======================================
+ Hits        11997    12048   +51     
- Misses       1381     1386    +5     
Flag Coverage Δ
unittests-Project-Experimental 89.46% <98.64%> (-0.10%) ⬇️
unittests-Project-Stable 89.43% <98.64%> (-0.16%) ⬇️
unittests-UnstableCoreLibraries-Experimental 41.32% <98.64%> (+0.35%) ⬆️

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.24% <100.00%> (+0.74%) ⬆️
...s.HttpListener/Internal/PrometheusHeadersParser.cs 98.14% <97.91%> (-1.86%) ⬇️

... and 5 files with indirect coverage changes

- Add a constant for the OpenMetrics version.
- Update CHANGELOGs with PR number.
@martincostello martincostello marked this pull request as ready for review April 29, 2026 10:28
@martincostello martincostello requested a review from a team as a code owner April 29, 2026 10:28
Copilot AI review requested due to automatic review settings April 29, 2026 10:28

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 Prometheus exporter content negotiation to better follow Prometheus scrape protocol rules by improving Accept header parsing and OpenMetrics vs Prometheus text selection.

Changes:

  • Enhanced Accept header parsing to account for whitespace, q weights, and OpenMetrics version (including quoted values).
  • Updated ASP.NET Core middleware to use typed Accept headers (GetTypedHeaders().Accept) and select the preferred format by quality.
  • Expanded unit test coverage for header parsing/negotiation scenarios and updated changelogs.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHeadersParserTests.cs Adds additional valid/invalid Accept header test cases (whitespace, quoted version, q weights).
test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs Refactors some tests and adds a focused unit test for AcceptsOpenMetrics using typed header parsing.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusHeadersParser.cs Implements Accept parsing with whitespace trimming, version parsing (quoted), and q-based negotiation.
src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md Documents the improved Accept header handling change.
src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs Switches to typed Accept parsing and applies q-based OpenMetrics vs text selection.
src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj Stops linking the shared PrometheusHeadersParser into the AspNetCore project.
src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md Documents the improved Accept header handling change.

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

Comment thread src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs Outdated
Comment thread src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs Outdated
- Parse `Accept` case-insensitively.
- Check `quality` is between 0 and 1.
@martincostello martincostello marked this pull request as ready for review April 29, 2026 12:41
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