Skip to content

Commit 6c11f86

Browse files
committed
fix escaping
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent fba0594 commit 6c11f86

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,10 +2785,10 @@ public void testLabelValueEscape() throws IOException {
27852785

27862786
@ParameterizedTest
27872787
@CsvSource({
2788-
"'application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited', 'application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=values'",
2789-
"'text/plain;version=0.0.4', 'text/plain; version=0.0.4; charset=utf-8; escaping=values'",
2788+
"'application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited', 'application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=underscores'",
2789+
"'text/plain;version=0.0.4', 'text/plain; version=0.0.4; charset=utf-8; escaping=underscores'",
27902790
"'application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited; escaping=allow-utf-8', 'application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited; escaping=allow-utf-8'",
2791-
"'application/openmetrics-text', 'application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=values'",
2791+
"'application/openmetrics-text', 'application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores'",
27922792
"'application/openmetrics-text;version=0.0.1; escaping=underscores', 'application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=underscores'",
27932793
"'text/plain;version=0.0.4; escaping=allow-utf-8', 'text/plain; version=0.0.4; charset=utf-8; escaping=allow-utf-8'"
27942794
})

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public MetricMetadata(String name, String help, Unit unit) {
6363
* The name does not include the {@code _total} suffix for counter metrics or the {@code _info}
6464
* suffix for Info metrics.
6565
*
66-
* <p>The name may contain dots. Use {@link #getPrometheusName()} to get the name in Prometheus
67-
* format, i.e. with dots replaced by underscores.
66+
* <p>The name may contain any Unicode chars. Use {@link #getPrometheusName()} to get the name in
67+
* legacy Prometheus format, i.e. with all dots and all invalid chars replaced by underscores.
6868
*/
6969
public String getName() {
7070
return name;
7171
}
7272

7373
/**
74-
* Same as {@link #getName()} but with all invalid characters replaced by underscores.
74+
* Same as {@link #getName()} but with all invalid characters and dots replaced by underscores.
7575
*
7676
* <p>This is used by Prometheus exposition formats.
7777
*/

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,11 @@ private static MetricSnapshot createEscapedMetricSnapshot(
576576
* which by definition is a noop). This method does not do any validation of the name.
577577
*/
578578
public static String escapeName(String name, EscapingScheme scheme) {
579-
if (name.isEmpty() || isValidLegacyMetricName(name)) {
579+
boolean noEscapeNeeded =
580+
isValidLegacyMetricName(name)
581+
&& !(scheme == EscapingScheme.DOTS_ESCAPING
582+
&& (name.contains(".") || name.contains("_")));
583+
if (name.isEmpty() || noEscapeNeeded) {
580584
return name;
581585
}
582586

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public void testEscapeMetricSnapshotGaugeEscapingNeeded() {
379379
"some_label",
380380
"label??value",
381381
"unicode_dot_and_dot_dots_dot_____",
382-
"some_label",
382+
"some__label",
383383
"label??value",
384384
EscapingScheme.DOTS_ESCAPING,
385385
GaugeSnapshot.class);

0 commit comments

Comments
 (0)