@@ -74,6 +74,16 @@ public class PrometheusNaming {
7474 ".total" , ".created" , ".bucket" , ".info"
7575 };
7676
77+ static ValidationScheme initValidationScheme () {
78+ if (PrometheusProperties .get () != null && PrometheusProperties .get ().getNamingProperties () != null ) {
79+ String validationScheme = PrometheusProperties .get ().getNamingProperties ().getValidationScheme ();
80+ if (validationScheme != null && validationScheme .equals ("utf-8" )) {
81+ return ValidationScheme .UTF_8_VALIDATION ;
82+ }
83+ }
84+ return ValidationScheme .LEGACY_VALIDATION ;
85+ }
86+
7787 /**
7888 * Test if a metric name is valid. Rules:
7989 * <ul>
@@ -96,16 +106,6 @@ public static boolean isValidMetricName(String name) {
96106 return validateMetricName (name ) == null ;
97107 }
98108
99- static ValidationScheme initValidationScheme () {
100- if (PrometheusProperties .get () != null && PrometheusProperties .get ().getNamingProperties () != null ) {
101- String validationScheme = PrometheusProperties .get ().getNamingProperties ().getValidationScheme ();
102- if (validationScheme != null && validationScheme .equals ("utf-8" )) {
103- return ValidationScheme .UTF_8_VALIDATION ;
104- }
105- }
106- return ValidationScheme .LEGACY_VALIDATION ;
107- }
108-
109109 static String validateMetricName (String name ) {
110110 switch (nameValidationScheme ) {
111111 case LEGACY_VALIDATION :
@@ -151,8 +151,7 @@ public static boolean isValidLegacyMetricName(String name) {
151151 public static boolean isValidLabelName (String name ) {
152152 switch (nameValidationScheme ) {
153153 case LEGACY_VALIDATION :
154- return isValidLegacyLabelName (name ) &&
155- !(name .startsWith ("__" ) || name .startsWith ("._" ) || name .startsWith (".." ) || name .startsWith ("_." ));
154+ return isValidLegacyLabelName (name );
156155 case UTF_8_VALIDATION :
157156 return StandardCharsets .UTF_8 .newEncoder ().canEncode (name );
158157 default :
@@ -161,14 +160,8 @@ public static boolean isValidLabelName(String name) {
161160 }
162161
163162 public static boolean isValidLegacyLabelName (String name ) {
164- switch (nameValidationScheme ) {
165- case LEGACY_VALIDATION :
166- return LEGACY_LABEL_NAME_PATTERN .matcher (name ).matches ();
167- case UTF_8_VALIDATION :
168- return LABEL_NAME_PATTERN .matcher (name ).matches ();
169- default :
170- throw new RuntimeException ("Invalid name validation scheme requested: " + nameValidationScheme );
171- }
163+ return LEGACY_LABEL_NAME_PATTERN .matcher (name ).matches () &&
164+ !(name .startsWith ("__" ) || name .startsWith ("._" ) || name .startsWith (".." ) || name .startsWith ("_." ));
172165 }
173166
174167 /**
0 commit comments