Skip to content

Commit b7658fb

Browse files
committed
fix : escaping
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 189d937 commit b7658fb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static void writeLabels(
123123
writer.write("\"");
124124
}
125125
if (additionalLabelName != null) {
126-
if (!labels.isEmpty()) {
126+
if (!labels.isEmpty() || metricInsideBraces) {
127127
writer.write(",");
128128
}
129129
writer.write(additionalLabelName);

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,37 @@ public void testClassicHistogramMinimal() throws Exception {
14261426
assertPrometheusProtobuf(prometheusProtobuf, histogram);
14271427
}
14281428

1429+
@Test
1430+
public void testClassicHistogramMinimalWithDots() throws Exception {
1431+
String openMetricsText =
1432+
"""
1433+
# TYPE "request.latency_seconds" histogram
1434+
{"request.latency_seconds_bucket",le="+Inf"} 2
1435+
# EOF
1436+
""";
1437+
String prometheusText =
1438+
"""
1439+
# TYPE request_latency_seconds histogram
1440+
request_latency_seconds_bucket{le="+Inf"} 2
1441+
request_latency_seconds_count 2
1442+
""";
1443+
HistogramSnapshot histogram =
1444+
HistogramSnapshot.builder()
1445+
.name("request.latency_seconds")
1446+
.dataPoint(
1447+
HistogramSnapshot.HistogramDataPointSnapshot.builder()
1448+
.classicHistogramBuckets(
1449+
ClassicHistogramBuckets.builder()
1450+
.bucket(Double.POSITIVE_INFINITY, 2)
1451+
.build())
1452+
.build())
1453+
.build();
1454+
// test that the name and label are separated with commas
1455+
assertOpenMetricsText(openMetricsText, histogram, EscapingScheme.ALLOW_UTF8);
1456+
assertPrometheusText(prometheusText, histogram);
1457+
assertPrometheusTextWithoutCreated(prometheusText, histogram);
1458+
}
1459+
14291460
@Test
14301461
public void testClassicHistogramCountAndSum() throws Exception {
14311462
String openMetricsText =
@@ -2822,10 +2853,15 @@ public void testWrite() throws IOException {
28222853
}
28232854

28242855
private void assertOpenMetricsText(String expected, MetricSnapshot snapshot) throws IOException {
2856+
assertOpenMetricsText(expected, snapshot, EscapingScheme.VALUE_ENCODING_ESCAPING);
2857+
}
2858+
2859+
private void assertOpenMetricsText(
2860+
String expected, MetricSnapshot snapshot, EscapingScheme escapingScheme) throws IOException {
28252861
ByteArrayOutputStream out = new ByteArrayOutputStream();
28262862
OpenMetricsTextFormatWriter writer =
28272863
OpenMetricsTextFormatWriter.builder().setCreatedTimestampsEnabled(true).build();
2828-
writer.write(out, MetricSnapshots.of(snapshot), EscapingScheme.VALUE_ENCODING_ESCAPING);
2864+
writer.write(out, MetricSnapshots.of(snapshot), escapingScheme);
28292865
assertThat(out).hasToString(expected);
28302866
}
28312867

0 commit comments

Comments
 (0)