[Exporter.Prometheus] Add target_info fallback#7238
Conversation
Add Prometheus text fallback `target_info` output as a gauge so resource metadata is still exposed as Info-typed metrics are unavailable for PrometheusText exposition format.
Add PR number.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7238 +/- ##
==========================================
+ Coverage 89.86% 89.92% +0.05%
==========================================
Files 276 276
Lines 14007 14022 +15
==========================================
+ Hits 12588 12609 +21
+ Misses 1419 1413 -6
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 a Prometheus text/plain fallback for resource metadata by emitting target_info as a gauge (value 1) when Info-typed metric families aren’t available in the Prometheus text exposition format.
Changes:
- Emit
target_infoas# TYPE target_info gauge/# HELP target_info ...for Prometheus text output while keeping OpenMetrics# TYPE target info. - Cache
target_infoserialization separately for plain text vs OpenMetrics responses. - Update HttpListener and AspNetCore integration tests + exporter changelogs to reflect the new plaintext output.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/PrometheusHttpListenerTests.cs | Updates expected scrape output to include plaintext target_info gauge. |
| test/OpenTelemetry.Exporter.Prometheus.AspNetCore.Tests/PrometheusExporterMiddlewareTests.cs | Updates expected scrape output to include plaintext target_info gauge. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs | Makes WriteTargetInfo switch between OpenMetrics info vs plaintext gauge family names. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusCollectionManager.cs | Writes/caches target_info for both formats and splits cached lengths by format. |
| src/OpenTelemetry.Exporter.Prometheus.HttpListener/CHANGELOG.md | Documents the fallback behavior addition. |
| src/OpenTelemetry.Exporter.Prometheus.AspNetCore/CHANGELOG.md | Documents the fallback behavior addition. |
Comments suppressed due to low confidence (1)
src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusSerializer.cs:472
WriteTargetInfoalways appends a comma for each resource attribute and then unconditionally doescursor--to overwrite the last comma. Ifresourceis not reference-equal toResource.Emptybut has zero attributes (e.g.,Resource.Empty.Merge(Resource.Empty)returns a new emptyResource), this will overwrite the{and emit invalid exposition (target_info} 1). Track whether any labels were written (or check for an empty attribute collection) and either omit braces/commas or skip emitting the metric when there are no attributes.
cursor = WriteAsciiStringNoEscape(buffer, cursor, "target_info");
buffer[cursor++] = unchecked((byte)'{');
foreach (var attribute in resource.Attributes)
{
cursor = WriteLabel(buffer, cursor, attribute.Key, attribute.Value);
buffer[cursor++] = unchecked((byte)',');
}
cursor--; // Write over the last written comma
buffer[cursor++] = unchecked((byte)'}');
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
From Codex: |
Fix generated corrupted metrics if no resources are present.
|
Feedback addressed. |
Wait until all metrics are present, not just one.
Fix PR links.
Changes
Add Prometheus text fallback
target_infooutput as a gauge so resource metadata is still exposed as Info-typed metrics are unavailable for PrometheusText exposition format.Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)