Skip to content

Commit a48d200

Browse files
Copilotzeitlinger
andcommitted
Replace UNIT_NAME_PATTERN with manual check for consistency
Co-authored-by: zeitlinger <[email protected]>
1 parent 490aa6d commit a48d200

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
*/
1919
public class PrometheusNaming {
2020

21-
/** Legal characters for unit names, including dot. */
21+
/**
22+
* Legal characters for unit names, including dot.
23+
*
24+
* @deprecated Not used anymore. Kept for backward compatibility. The validation is now done
25+
* without regex for better performance.
26+
*/
27+
@Deprecated
28+
@SuppressWarnings("UnusedVariable")
2229
private static final Pattern UNIT_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_.:]+$");
2330

2431
/**
@@ -165,8 +172,17 @@ public static String validateUnitName(String name) {
165172
return suffixName + " is a reserved suffix in Prometheus";
166173
}
167174
}
168-
if (!UNIT_NAME_PATTERN.matcher(name).matches()) {
169-
return "The unit name contains unsupported characters";
175+
// Check if all characters are [a-zA-Z0-9_.:]+
176+
for (int i = 0; i < name.length(); i++) {
177+
char c = name.charAt(i);
178+
if (!((c >= 'a' && c <= 'z')
179+
|| (c >= 'A' && c <= 'Z')
180+
|| (c >= '0' && c <= '9')
181+
|| c == '_'
182+
|| c == '.'
183+
|| c == ':')) {
184+
return "The unit name contains unsupported characters";
185+
}
170186
}
171187
return null;
172188
}
@@ -282,7 +298,7 @@ public static String sanitizeUnitName(String unitName) {
282298
return sanitizedName;
283299
}
284300

285-
/** Returns a string that matches {@link #UNIT_NAME_PATTERN}. */
301+
/** Returns a string with only valid unit name characters [a-zA-Z0-9_.:]. */
286302
private static String replaceIllegalCharsInUnitName(String name) {
287303
int length = name.length();
288304
char[] sanitized = new char[length];

0 commit comments

Comments
 (0)