[Exporter.Prometheus] Add ResourceConstantLabels#7471
Conversation
Add support for emitting resource attributes as metric labels based on a user-supplied predicate. Resolves open-telemetry#7437.
Add PR number.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7471 +/- ##
==========================================
+ Coverage 90.18% 90.41% +0.22%
==========================================
Files 287 287
Lines 15553 16172 +619
==========================================
+ Hits 14027 14622 +595
- Misses 1526 1550 +24
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Pull request overview
Adds support in the Prometheus exporters (ASP.NET Core scraping endpoint + HttpListener) to emit selected OpenTelemetry Resource attributes as constant metric labels, controlled by a user-supplied predicate, aligning with the resource_constant_labels option described in the Prometheus exporter spec.
Changes:
- Introduces
ResourceConstantLabelson bothPrometheusAspNetCoreOptionsandPrometheusHttpListenerOptionsand wires it into exporter serialization. - Extends the text format serializer pipeline to merge configured resource labels with per-metric tag labels (handling key collisions via existing label-merge logic).
- Adds integration tests + snapshots covering “all / one key / none” resource-label selection.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunWithResourceConstantLabelsConfigured_filter=service_name.verified.text | New snapshot validating resource labels added to metrics using a key-filter predicate. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunWithResourceConstantLabelsConfigured_filter=none.verified.text | New snapshot validating no resource labels are added when predicate excludes all. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunWithResourceConstantLabelsConfigured_filter=all.verified.text | New snapshot validating all resource attributes become constant metric labels. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunHttpServerWithResourceConstantLabelsConfigured_filter=service_name.verified.text | New snapshot validating HttpListener server output with filtered resource labels. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunHttpServerWithResourceConstantLabelsConfigured_filter=none.verified.text | New snapshot validating HttpListener server output with no resource labels. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/snapshots/PrometheusSerializer.RunHttpServerWithResourceConstantLabelsConfigured_filter=all.verified.text | New snapshot validating HttpListener server output with all resource labels. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusSerializerTests.cs | Updates serializer options construction to include the new resource-labels option parameter. |
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs | Adds integration test coverage for HttpListener ResourceConstantLabels behavior. |
| test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs | Adds integration test coverage for ASP.NET Core ResourceConstantLabels behavior. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/README.md | Documents ResourceConstantLabels for HttpListener exporter configuration. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerOptions.cs | Adds public ResourceConstantLabels option (predicate) to HttpListener options. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListenerMeterProviderBuilderExtensions.cs | Wires PrometheusHttpListenerOptions.ResourceConstantLabels into exporter construction. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/Serialization/TextFormatSerializerOptions.cs | Extends serializer options to carry computed resource constant labels into serialization. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/Serialization/TextFormatSerializer.cs | Emits resource constant labels on each metric and ensures correct collision/merge behavior. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusExporterOptions.cs | Adds internal shared option ResourceConstantLabels used by both hosting models. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusExporter.cs | Stores ResourceConstantLabels on the exporter for use during collection/serialization. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/PrometheusCollectionManager.cs | Computes and caches filtered resource labels and passes them into serialization. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md | Adds changelog entry for the new PrometheusHttpListenerOptions.ResourceConstantLabels behavior. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/.publicApi/PublicAPI.Unshipped.txt | Tracks new public API surface for HttpListener options property. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/README.md | Documents ResourceConstantLabels for ASP.NET Core exporter configuration. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/PrometheusAspNetCoreOptions.cs | Adds public ResourceConstantLabels option (predicate) to ASP.NET Core options. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md | Adds changelog entry for the new PrometheusAspNetCoreOptions.ResourceConstantLabels behavior. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/.publicApi/PublicAPI.Unshipped.txt | Tracks new public API surface for ASP.NET Core options property. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Keep scope-label emission conditional without also making point-tag serialization conditional. The final merge from main accidentally nested TryWritePointTags under the SuppressScopeInfo check, forcing scrapes with ScopeInfoEnabled disabled and no resource constant labels through the allocation-heavy collision and merge path. This restores direct point-tag serialization while retaining the slow path whenever resource constant labels are present and require collision handling. Validated the Release net10.0 test suites for both Prometheus exporters: HttpListener passed 629 tests with 4 skipped, and ASP.NET Core passed 426 tests with 4 skipped. Assisted-by: OpenAI Codex
Kielek
left a comment
There was a problem hiding this comment.
LGTM with some direct feedback.
Fixes #7437
Changes
Add support for emitting resource attributes as metric labels based on a user-supplied predicate.
Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changes