Skip to content

Commit f63d8b7

Browse files
committed
fix: OM2 Info TYPE/HELP lines use full name including _info
OM2 spec requires metric name to match MetricFamily name. The Info writer was stripping _info for TYPE/HELP lines (OM1 convention) while keeping it on data lines.
1 parent d1b0517 commit f63d8b7

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ private void writeSummary(Writer writer, SummarySnapshot snapshot, EscapingSchem
296296
private void writeInfo(Writer writer, InfoSnapshot snapshot, EscapingScheme scheme)
297297
throws IOException {
298298
MetricMetadata metadata = snapshot.getMetadata();
299-
// OM2 spec: Info MetricFamily name MUST end in _info
299+
// OM2 spec: Info MetricFamily name MUST end in _info.
300+
// In OM2, TYPE/HELP use the same name as the data lines.
300301
String infoName = ensureSuffix(getExpositionBaseMetadataName(metadata, scheme), "_info");
301-
String baseName = removeSuffix(infoName, "_info");
302-
writeMetadataWithName(writer, baseName, "info", metadata);
302+
writeMetadataWithName(writer, infoName, "info", metadata);
303303
for (InfoSnapshot.InfoDataPointSnapshot data : snapshot.getDataPoints()) {
304304
writeNameAndLabels(writer, infoName, null, data.getLabels(), scheme);
305305
writer.write("1");
@@ -488,11 +488,4 @@ private static String ensureSuffix(String name, String suffix) {
488488
}
489489
return name + suffix;
490490
}
491-
492-
private static String removeSuffix(String name, String suffix) {
493-
if (name.endsWith(suffix)) {
494-
return name.substring(0, name.length() - suffix.length());
495-
}
496-
return name;
497-
}
498491
}

prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriterTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void testOutputIdenticalToOM1ForSummary() throws IOException {
207207
}
208208

209209
@Test
210-
void testOutputIdenticalToOM1ForInfo() throws IOException {
210+
void testInfoHelpNameMatchesMeterName() throws IOException {
211211
MetricSnapshots snapshots =
212212
MetricSnapshots.of(
213213
InfoSnapshot.builder()
@@ -219,10 +219,15 @@ void testOutputIdenticalToOM1ForInfo() throws IOException {
219219
.build())
220220
.build());
221221

222-
String om1Output = writeWithOM1(snapshots);
223222
String om2Output = writeWithOM2(snapshots);
224223

225-
assertThat(om2Output).isEqualTo(om1Output);
224+
// OM2: TYPE/HELP use the full name including _info (help name == meter name)
225+
assertThat(om2Output)
226+
.isEqualTo(
227+
"# TYPE my_info info\n"
228+
+ "# HELP my_info Test info\n"
229+
+ "my_info{platform=\"linux\",version=\"1.0\"} 1\n"
230+
+ "# EOF\n");
226231
}
227232

228233
@Test

0 commit comments

Comments
 (0)