Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
* provided by the user, without appending {@code _total}, unit, or {@code _info} suffixes. This is
* experimental and subject to change as the <a
* href="https://github.com/prometheus/docs/blob/main/docs/specs/om/open_metrics_spec_2_0.md">OpenMetrics
* 2.0 specification</a> evolves.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void testOutputIdenticalToOM1ForSummary() throws IOException {
}

@Test
void testInfoHelpNameMatchesMeterName() throws IOException {
void testInfoPreservesOriginalName() throws IOException {
MetricSnapshots snapshots =
MetricSnapshots.of(
InfoSnapshot.builder()
Expand All @@ -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");
}

Expand Down
Loading