Skip to content

[Exporter.Prometheus] Add ResourceConstantLabels#7471

Merged
martincostello merged 6 commits into
open-telemetry:mainfrom
martincostello:gh-7437
Jul 7, 2026
Merged

[Exporter.Prometheus] Add ResourceConstantLabels#7471
martincostello merged 6 commits into
open-telemetry:mainfrom
martincostello:gh-7437

Conversation

@martincostello

Copy link
Copy Markdown
Member

Fixes #7437

Changes

Add support for emitting resource attributes as metric labels based on a user-supplied predicate.

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 support for emitting resource attributes as metric labels based on a user-supplied predicate.

Resolves open-telemetry#7437.
Comment thread src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md Outdated
Comment thread src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md Outdated
@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 30, 2026
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.41%. Comparing base (68c29d8) to head (dda4fd1).
⚠️ Report is 4 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ometheus.AspNetCore/PrometheusAspNetCoreOptions.cs 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
unittests-Project-Experimental 90.04% <96.77%> (-0.17%) ⬇️
unittests-Project-Stable 90.09% <96.77%> (-0.14%) ⬇️
unittests-UnstableCoreLibraries-Experimental 50.69% <96.77%> (+0.10%) ⬆️

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

Files with missing lines Coverage Δ
...ner/Internal/Shared/PrometheusCollectionManager.cs 90.73% <100.00%> (+0.35%) ⬆️
...HttpListener/Internal/Shared/PrometheusExporter.cs 100.00% <100.00%> (ø)
...tener/Internal/Shared/PrometheusExporterOptions.cs 100.00% <ø> (ø)
...ernal/Shared/Serialization/TextFormatSerializer.cs 99.65% <100.00%> (+0.16%) ⬆️
...hared/Serialization/TextFormatSerializerOptions.cs 100.00% <100.00%> (ø)
...theusHttpListenerMeterProviderBuilderExtensions.cs 82.85% <100.00%> (+0.50%) ⬆️
...heus.HttpListener/PrometheusHttpListenerOptions.cs 100.00% <ø> (ø)
...ometheus.AspNetCore/PrometheusAspNetCoreOptions.cs 50.00% <50.00%> (ø)

... and 10 files with indirect coverage changes

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

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 ResourceConstantLabels on both PrometheusAspNetCoreOptions and PrometheusHttpListenerOptions and 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.

@martincostello
martincostello marked this pull request as ready for review July 3, 2026 08:11
@martincostello
martincostello requested a review from a team as a code owner July 3, 2026 08:11
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 Kielek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with some direct feedback.

This was referenced Jul 17, 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 spec-compliance Issues related to compliance with the OpenTelemetry Specification

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Exporter.Prometheus] Resource Attributes as Metric Labels

3 participants