Skip to content

Commit d4d4516

Browse files
committed
unescape is not used
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent a57b4e6 commit d4d4516

File tree

2 files changed

+25
-203
lines changed

2 files changed

+25
-203
lines changed

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

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.regex.Matcher;
10-
import java.util.regex.Pattern;
119
import javax.annotation.Nullable;
1210

1311
public class SnapshotEscaper {
@@ -344,92 +342,6 @@ public static boolean needsEscaping(String name, EscapingScheme scheme) {
344342
|| (scheme == EscapingScheme.DOTS_ESCAPING && (name.contains(".") || name.contains("_")));
345343
}
346344

347-
/**
348-
* Unescapes the incoming name according to the provided escaping scheme if possible. Some schemes
349-
* are partially or totally non-roundtripable. If any error is encountered, returns the original
350-
* input.
351-
*/
352-
@SuppressWarnings("IncrementInForLoopAndHeader")
353-
static String unescapeName(String name, EscapingScheme scheme) {
354-
if (name.isEmpty()) {
355-
return name;
356-
}
357-
switch (scheme) {
358-
case NO_ESCAPING:
359-
return name;
360-
case UNDERSCORE_ESCAPING:
361-
// It is not possible to unescape from underscore replacement.
362-
return name;
363-
case DOTS_ESCAPING:
364-
name = name.replaceAll("_dot_", ".");
365-
name = name.replaceAll("__", "_");
366-
return name;
367-
case VALUE_ENCODING_ESCAPING:
368-
Matcher matcher = Pattern.compile("U__").matcher(name);
369-
if (matcher.find()) {
370-
String escapedName = name.substring(matcher.end());
371-
StringBuilder unescaped = new StringBuilder();
372-
for (int i = 0; i < escapedName.length(); ) {
373-
// All non-underscores are treated normally.
374-
int c = escapedName.codePointAt(i);
375-
if (c != '_') {
376-
unescaped.appendCodePoint(c);
377-
i += Character.charCount(c);
378-
continue;
379-
}
380-
i++;
381-
if (i >= escapedName.length()) {
382-
return name;
383-
}
384-
// A double underscore is a single underscore.
385-
if (escapedName.codePointAt(i) == '_') {
386-
unescaped.append('_');
387-
i++;
388-
continue;
389-
}
390-
// We think we are in a UTF-8 code, process it.
391-
int utf8Val = 0;
392-
boolean foundClosingUnderscore = false;
393-
for (int j = 0; i < escapedName.length(); j++) {
394-
// This is too many characters for a UTF-8 value.
395-
if (j >= 6) {
396-
return name;
397-
}
398-
// Found a closing underscore, convert to a char, check validity, and append.
399-
if (escapedName.codePointAt(i) == '_') {
400-
// char utf8Char = (char) utf8Val;
401-
foundClosingUnderscore = true;
402-
if (!isValidUtf8Char(utf8Val)) {
403-
return name;
404-
}
405-
unescaped.appendCodePoint(utf8Val);
406-
i++;
407-
break;
408-
}
409-
char r = Character.toLowerCase(escapedName.charAt(i));
410-
utf8Val *= 16;
411-
if (r >= '0' && r <= '9') {
412-
utf8Val += r - '0';
413-
} else if (r >= 'a' && r <= 'f') {
414-
utf8Val += r - 'a' + 10;
415-
} else {
416-
return name;
417-
}
418-
i++;
419-
}
420-
if (!foundClosingUnderscore) {
421-
return name;
422-
}
423-
}
424-
return unescaped.toString();
425-
} else {
426-
return name;
427-
}
428-
default:
429-
throw new IllegalArgumentException("Invalid escaping scheme " + scheme);
430-
}
431-
}
432-
433345
static boolean isValidLegacyChar(int c, int i) {
434346
return (c >= 'a' && c <= 'z')
435347
|| (c >= 'A' && c <= 'Z')

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

Lines changed: 25 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -3,169 +3,79 @@
33
import static io.prometheus.metrics.model.snapshots.SnapshotEscaper.escapeMetricSnapshot;
44
import static io.prometheus.metrics.model.snapshots.SnapshotEscaper.escapeName;
55
import static io.prometheus.metrics.model.snapshots.SnapshotEscaper.getSnapshotLabelName;
6-
import static io.prometheus.metrics.model.snapshots.SnapshotEscaper.unescapeName;
76
import static org.assertj.core.api.Assertions.assertThat;
87

98
import java.util.stream.Stream;
109
import org.junit.jupiter.api.Test;
1110
import org.junit.jupiter.params.ParameterizedTest;
1211
import org.junit.jupiter.params.provider.Arguments;
13-
import org.junit.jupiter.params.provider.CsvSource;
1412
import org.junit.jupiter.params.provider.MethodSource;
1513

1614
class SnapshotEscaperTest {
1715

1816
@ParameterizedTest
1917
@MethodSource("escapeNameLegacyTestCases")
20-
public void testEscapeNameLegacy(
21-
String input, EscapingScheme escapingScheme, String expected, String unescapeExpected) {
22-
assertEscape(input, escapingScheme, expected, unescapeExpected);
23-
}
24-
25-
@ParameterizedTest
26-
@MethodSource("escapeNameUtf8TestCases")
27-
public void testEscapeNameUtf8(
28-
String input, EscapingScheme escapingScheme, String expected, String unescapeExpected) {
29-
assertEscape(input, escapingScheme, expected, unescapeExpected);
30-
}
31-
32-
private static void assertEscape(
33-
String input, EscapingScheme escapingScheme, String expected, String unescapeExpected) {
34-
String escaped = escapeName(input, escapingScheme);
35-
assertThat(escaped).isEqualTo(expected);
36-
assertThat(unescapeName(escaped, escapingScheme)).isEqualTo(unescapeExpected);
18+
public void testEscapeName(String input, EscapingScheme escapingScheme, String expected) {
19+
assertThat(escapeName(input, escapingScheme)).isEqualTo(expected);
3720
}
3821

3922
static Stream<Arguments> escapeNameLegacyTestCases() {
4023
return Stream.of(
41-
Arguments.of("", EscapingScheme.UNDERSCORE_ESCAPING, "", ""),
42-
Arguments.of("", EscapingScheme.DOTS_ESCAPING, "", ""),
43-
Arguments.of("", EscapingScheme.VALUE_ENCODING_ESCAPING, "", ""),
24+
Arguments.of("", EscapingScheme.UNDERSCORE_ESCAPING, ""),
25+
Arguments.of("", EscapingScheme.DOTS_ESCAPING, ""),
26+
Arguments.of("", EscapingScheme.VALUE_ENCODING_ESCAPING, ""),
4427
Arguments.of(
45-
"no:escaping_required",
46-
EscapingScheme.UNDERSCORE_ESCAPING,
47-
"no:escaping_required",
48-
"no:escaping_required"),
28+
"no:escaping_required", EscapingScheme.UNDERSCORE_ESCAPING, "no:escaping_required"),
4929
// Dots escaping will escape underscores even though it's not strictly
5030
// necessary for compatibility.
31+
Arguments.of("no:escaping_required", EscapingScheme.DOTS_ESCAPING, "no:escaping__required"),
5132
Arguments.of(
52-
"no:escaping_required",
53-
EscapingScheme.DOTS_ESCAPING,
54-
"no:escaping__required",
55-
"no:escaping_required"),
56-
Arguments.of(
57-
"no:escaping_required",
58-
EscapingScheme.VALUE_ENCODING_ESCAPING,
59-
"no:escaping_required",
60-
"no:escaping_required"),
33+
"no:escaping_required", EscapingScheme.VALUE_ENCODING_ESCAPING, "no:escaping_required"),
6134
Arguments.of(
62-
"no:escaping_required",
63-
EscapingScheme.UNDERSCORE_ESCAPING,
64-
"no:escaping_required",
65-
"no:escaping_required"),
35+
"no:escaping_required", EscapingScheme.UNDERSCORE_ESCAPING, "no:escaping_required"),
6636
Arguments.of(
6737
"mysystem.prod.west.cpu.load",
6838
EscapingScheme.DOTS_ESCAPING,
69-
"mysystem_dot_prod_dot_west_dot_cpu_dot_load",
70-
"mysystem.prod.west.cpu.load"),
39+
"mysystem_dot_prod_dot_west_dot_cpu_dot_load"),
7140
Arguments.of(
7241
"mysystem.prod.west.cpu.load_total",
7342
EscapingScheme.DOTS_ESCAPING,
74-
"mysystem_dot_prod_dot_west_dot_cpu_dot_load__total",
75-
"mysystem.prod.west.cpu.load_total"),
76-
Arguments.of(
77-
"http.status:sum",
78-
EscapingScheme.DOTS_ESCAPING,
79-
"http_dot_status:sum",
80-
"http.status:sum"),
81-
Arguments.of(
82-
"label with 😱", EscapingScheme.UNDERSCORE_ESCAPING, "label_with__", "label_with__"),
83-
Arguments.of(
84-
"label with 😱", EscapingScheme.DOTS_ESCAPING, "label__with____", "label_with__"),
43+
"mysystem_dot_prod_dot_west_dot_cpu_dot_load__total"),
44+
Arguments.of("http.status:sum", EscapingScheme.DOTS_ESCAPING, "http_dot_status:sum"),
45+
Arguments.of("label with 😱", EscapingScheme.UNDERSCORE_ESCAPING, "label_with__"),
46+
Arguments.of("label with 😱", EscapingScheme.DOTS_ESCAPING, "label__with____"),
8547
Arguments.of(
86-
"label with 😱",
87-
EscapingScheme.VALUE_ENCODING_ESCAPING,
88-
"U__label_20_with_20__1f631_",
89-
"label with 😱"),
48+
"label with 😱", EscapingScheme.VALUE_ENCODING_ESCAPING, "U__label_20_with_20__1f631_"),
9049
// name with unicode characters > 0x100
91-
Arguments.of("花火", EscapingScheme.UNDERSCORE_ESCAPING, "__", "__"),
50+
Arguments.of("花火", EscapingScheme.UNDERSCORE_ESCAPING, "__"),
9251
// Dots-replacement does not know the difference between two replaced
93-
Arguments.of("花火", EscapingScheme.DOTS_ESCAPING, "____", "__"),
94-
Arguments.of("花火", EscapingScheme.VALUE_ENCODING_ESCAPING, "U___82b1__706b_", "花火"),
52+
Arguments.of("花火", EscapingScheme.DOTS_ESCAPING, "____"),
53+
Arguments.of("花火", EscapingScheme.VALUE_ENCODING_ESCAPING, "U___82b1__706b_"),
9554
// name with spaces and edge-case value
55+
Arguments.of("label with Ā", EscapingScheme.UNDERSCORE_ESCAPING, "label_with__"),
56+
Arguments.of("label with Ā", EscapingScheme.DOTS_ESCAPING, "label__with____"),
9657
Arguments.of(
97-
"label with Ā", EscapingScheme.UNDERSCORE_ESCAPING, "label_with__", "label_with__"),
98-
Arguments.of(
99-
"label with Ā", EscapingScheme.DOTS_ESCAPING, "label__with____", "label_with__"),
100-
Arguments.of(
101-
"label with Ā",
102-
EscapingScheme.VALUE_ENCODING_ESCAPING,
103-
"U__label_20_with_20__100_",
104-
"label with Ā"));
105-
}
106-
107-
static Stream<Arguments> escapeNameUtf8TestCases() {
108-
return Stream.of(
58+
"label with Ā", EscapingScheme.VALUE_ENCODING_ESCAPING, "U__label_20_with_20__100_"),
10959
// name with dots - needs UTF-8 validation for escaping to occur
11060
Arguments.of(
11161
"mysystem.prod.west.cpu.load",
11262
EscapingScheme.UNDERSCORE_ESCAPING,
113-
"mysystem_prod_west_cpu_load",
11463
"mysystem_prod_west_cpu_load"),
11564
Arguments.of(
11665
"mysystem.prod.west.cpu.load",
11766
EscapingScheme.VALUE_ENCODING_ESCAPING,
118-
"U__mysystem_2e_prod_2e_west_2e_cpu_2e_load",
119-
"mysystem.prod.west.cpu.load"),
67+
"U__mysystem_2e_prod_2e_west_2e_cpu_2e_load"),
12068
Arguments.of(
12169
"mysystem.prod.west.cpu.load_total",
12270
EscapingScheme.UNDERSCORE_ESCAPING,
123-
"mysystem_prod_west_cpu_load_total",
12471
"mysystem_prod_west_cpu_load_total"),
12572
Arguments.of(
12673
"mysystem.prod.west.cpu.load_total",
12774
EscapingScheme.VALUE_ENCODING_ESCAPING,
128-
"U__mysystem_2e_prod_2e_west_2e_cpu_2e_load__total",
129-
"mysystem.prod.west.cpu.load_total"),
75+
"U__mysystem_2e_prod_2e_west_2e_cpu_2e_load__total"),
76+
Arguments.of("http.status:sum", EscapingScheme.UNDERSCORE_ESCAPING, "http_status:sum"),
13077
Arguments.of(
131-
"http.status:sum",
132-
EscapingScheme.UNDERSCORE_ESCAPING,
133-
"http_status:sum",
134-
"http_status:sum"),
135-
Arguments.of(
136-
"http.status:sum",
137-
EscapingScheme.VALUE_ENCODING_ESCAPING,
138-
"U__http_2e_status:sum",
139-
"http.status:sum"));
140-
}
141-
142-
@ParameterizedTest
143-
@CsvSource(
144-
value = {
145-
// empty string
146-
"'',''",
147-
// basic case, no error
148-
"U__no:unescapingrequired,no:unescapingrequired",
149-
// capitals ok, no error
150-
"U__capitals_2E_ok,capitals.ok",
151-
// underscores, no error
152-
"U__underscores__doubled__,underscores_doubled_",
153-
// invalid single underscore
154-
"U__underscores_doubled_,U__underscores_doubled_",
155-
// invalid single underscore, 2
156-
"U__underscores__doubled_,U__underscores__doubled_",
157-
// giant fake UTF-8 code
158-
"U__my__hack_2e_attempt_872348732fabdabbab_,U__my__hack_2e_attempt_872348732fabdabbab_",
159-
// trailing UTF-8
160-
"U__my__hack_2e,U__my__hack_2e",
161-
// invalid UTF-8 value
162-
"U__bad__utf_2eg_,U__bad__utf_2eg_",
163-
// surrogate UTF-8 value
164-
"U__bad__utf_D900_,U__bad__utf_D900_",
165-
})
166-
public void testValueUnescapeErrors(String escapedName, String expectedUnescapedName) {
167-
assertThat(unescapeName(escapedName, EscapingScheme.VALUE_ENCODING_ESCAPING))
168-
.isEqualTo(expectedUnescapedName);
78+
"http.status:sum", EscapingScheme.VALUE_ENCODING_ESCAPING, "U__http_2e_status:sum"));
16979
}
17080

17181
@Test

0 commit comments

Comments
 (0)