diff --git a/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java b/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java index f400ecce0..f7d930491 100644 --- a/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java +++ b/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java @@ -41,8 +41,8 @@ /** * Write the OpenMetrics 2.0 text format. Unlike the OM1 writer, this writer outputs metric names as - * provided by the user, without appending {@code _total} or unit suffixes. The {@code _info} suffix - * is enforced per the OM2 spec (MUST). This is experimental and subject to change as the OpenMetrics * 2.0 specification evolves. */ @@ -550,9 +550,7 @@ private void writeCompositeSummaryDataPoint( private void writeInfo(Writer writer, InfoSnapshot snapshot, EscapingScheme scheme) throws IOException { MetricMetadata metadata = snapshot.getMetadata(); - // OM2 spec: Info MetricFamily name MUST end in _info. - // In OM2, TYPE/HELP use the same name as the data lines. - String infoName = ensureSuffix(getOriginalMetadataName(metadata, scheme), "_info"); + String infoName = getOriginalMetadataName(metadata, scheme); writeMetadataWithName(writer, infoName, "info", metadata); for (InfoSnapshot.InfoDataPointSnapshot data : snapshot.getDataPoints()) { writeNameAndLabels(writer, infoName, null, data.getLabels(), scheme); @@ -713,11 +711,4 @@ private void writeMetadataWithName( writer.write('\n'); } } - - private static String ensureSuffix(String name, String suffix) { - if (name.endsWith(suffix)) { - return name; - } - return name + suffix; - } } diff --git a/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriterTest.java b/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriterTest.java index 83c815c00..b7a0470ea 100644 --- a/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriterTest.java +++ b/prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriterTest.java @@ -181,7 +181,7 @@ void testOutputIdenticalToOM1ForSummary() throws IOException { } @Test - void testInfoHelpNameMatchesMeterName() throws IOException { + void testInfoPreservesOriginalName() throws IOException { MetricSnapshots snapshots = MetricSnapshots.of( InfoSnapshot.builder() @@ -195,12 +195,35 @@ void testInfoHelpNameMatchesMeterName() throws IOException { String om2Output = writeWithOM2(snapshots); - // OM2: TYPE/HELP use the full name including _info (help name == meter name) + // OM2: preserve the original metric name without appending _info. + assertThat(om2Output) + .isEqualTo( + "# TYPE my info\n" + + "# HELP my Test info\n" + + "my{platform=\"linux\",version=\"1.0\"} 1\n" + + "# EOF\n"); + } + + @Test + void testInfoKeepsExistingInfoSuffix() throws IOException { + MetricSnapshots snapshots = + MetricSnapshots.of( + InfoSnapshot.builder() + .name("my_info") + .help("Test info") + .dataPoint( + InfoSnapshot.InfoDataPointSnapshot.builder() + .labels(Labels.of("version", "1.0")) + .build()) + .build()); + + String om2Output = writeWithOM2(snapshots); + assertThat(om2Output) .isEqualTo( "# TYPE my_info info\n" + "# HELP my_info Test info\n" - + "my_info{platform=\"linux\",version=\"1.0\"} 1\n" + + "my_info{version=\"1.0\"} 1\n" + "# EOF\n"); }