You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Units may not have illegal characters, and they may not end with a reserved suffix like 'total'.
93
+
*/
94
+
publicstaticbooleanisValidUnitName(Stringname) {
95
+
returnvalidateUnitName(name) == null;
96
+
}
97
+
98
+
/**
99
+
* Same as {@link #isValidUnitName(String)} but returns an error message.
100
+
*/
101
+
publicstaticStringvalidateUnitName(Stringname) {
102
+
if (name.isEmpty()) {
103
+
return"The unit name must not be empty.";
104
+
}
105
+
for (StringreservedSuffix : RESERVED_METRIC_NAME_SUFFIXES) {
106
+
StringsuffixName = reservedSuffix.substring(1);
107
+
if (name.endsWith(suffixName)) {
108
+
returnsuffixName + " is a reserved suffix in Prometheus";
109
+
}
110
+
}
111
+
if (!UNIT_NAME_PATTERN.matcher(name).matches()) {
112
+
return"The unit name contains unsupported characters";
113
+
}
114
+
returnnull;
115
+
}
116
+
86
117
/**
87
118
* Get the metric or label name that is used in Prometheus exposition format.
88
119
*
@@ -149,6 +180,42 @@ public static String sanitizeLabelName(String labelName) {
149
180
returnsanitizedName;
150
181
}
151
182
183
+
/**
184
+
* Convert an arbitrary string to a name where {@link #isValidUnitName(String) isValidUnitName(name)} is true.
185
+
*
186
+
* @throws IllegalArgumentException if the {@code unitName} cannot be converted, for example if you call {@code sanitizeUnitName("total")} or {@code sanitizeUnitName("")}.
187
+
* @throws NullPointerException if {@code unitName} is null.
0 commit comments