[Exporter.Prometheus] Use dictionary to key views#7371
Conversation
- Instead of maintaining separate fields for reviews and timestamps for scrape responses, key on the `PrometheusProtocol` to support multiple formats and simplify passing options that affect the serializer around. - Rename version properties for consistency.
Consolidate the buffer and view into a single state object, rather than storing across four dictionaries.
Remove conditional `using`.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7371 +/- ##
==========================================
- Coverage 90.38% 89.89% -0.50%
==========================================
Files 284 276 -8
Lines 15429 14521 -908
==========================================
- Hits 13946 13054 -892
+ Misses 1483 1467 -16
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Ensure that the response received is for the same protocol the collection is for.
Add tests for `PrometheusProtocol`'s `IEquatable<PrometheusProtocol>` implementation.
Refactor collection to batch multiple protocol writes within the same lock to avoid queuing to serialize different protocols.
Fix deadlock during concurrent collections found in CI.
Fix test failing in CI.
There was a problem hiding this comment.
Pull request overview
This PR refactors the Prometheus exporter scrape response caching/buffering so state is keyed by a richer PrometheusProtocol (media type, escaping, version, etc.), enabling concurrent support for multiple output formats and simplifying future option propagation.
Changes:
- Refactors
PrometheusCollectionManagerto maintain per-protocol state in a dictionary keyed byPrometheusProtocol, returning a singleViewper negotiated protocol. - Extends
PrometheusProtocolwithIEquatable,GetHashCode,ToString, and adds debug-time validation to constrain key cardinality. - Updates HttpListener + ASP.NET Core endpoints and test suites to use the protocol-based collection APIs; adds new protocol-focused tests and links them into the ASP.NET Core test project.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusProtocolTests.cs | Adds unit tests for PrometheusProtocol equality/hash behavior and dictionary-key usage. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusCollectionManagerTests.cs | Updates tests to pass/track PrometheusProtocol and adds coverage for cross-protocol shared collection behavior. |
| test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests.csproj | Links the new protocol test file into the ASP.NET Core test project. |
| src/Shared/Shims/Lock.cs | Simplifies the Lock shim declaration used for pre-.NET9 locking compatibility. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs | Switches scrape path to call EnterCollect(protocol) and write collectionResponse.View. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusProtocol.cs | Adds IEquatable, hashing, debug validation, and renames version constants to V{n}. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusExporter.cs | Removes OpenMetricsRequested mutable state in favor of protocol-driven collection. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusCollectionManager.cs | Major refactor: protocol-keyed per-format state, shared active collection across protocols, and new response/result plumbing. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusHeadersParser.cs | Updates version constant names and adds protocol.Validate() after negotiation. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusExporterMiddleware.cs | Switches scrape path to EnterCollect(protocol) and validates protocol in parsing. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/OpenTelemetry.Exporter.Prometheus.AspNetCore.csproj | Links Lock shim into the ASP.NET Core project to support shared code using Lock. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Changes
PrometheusProtocolto support multiple formats and simplify passing options that affect the serializer around in the future (e.g. for [Exporter.Prometheus] Support translation strategy configuration #7156 and [Exporter.Prometheus] Interaction with Translation Strategy #7159).V{number}.Changes are about ~2% regression for the scrape endpoint, but that's still massively improved compared to before #7279.
Merge requirement checklist
Unit tests added/updatedAppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)