11package io .prometheus .metrics .model .snapshots ;
22
3- import static io .prometheus .metrics .model .snapshots .PrometheusNaming .*;
3+ import org .junit .jupiter .api .AfterEach ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .junit .jupiter .params .ParameterizedTest ;
6+ import org .junit .jupiter .params .provider .Arguments ;
7+ import org .junit .jupiter .params .provider .MethodSource ;
8+ import org .junitpioneer .jupiter .SetSystemProperty ;
9+ import org .mockito .MockedStatic ;
10+
11+ import java .util .stream .Stream ;
12+
13+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .escapeMetricSnapshot ;
14+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .escapeName ;
15+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .isValidLabelName ;
16+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .prometheusName ;
17+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .sanitizeLabelName ;
18+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .sanitizeMetricName ;
19+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .sanitizeUnitName ;
20+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .unescapeName ;
21+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .validateMetricName ;
22+ import static io .prometheus .metrics .model .snapshots .PrometheusNaming .validateUnitName ;
423import static org .assertj .core .api .Assertions .assertThat ;
524import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
625import static org .mockito .Mockito .CALLS_REAL_METHODS ;
726import static org .mockito .Mockito .mockStatic ;
827
9- import org .junit .jupiter .api .Test ;
10- import org .mockito .MockedStatic ;
11-
1228class PrometheusNamingTest {
1329
30+ @ AfterEach
31+ void tearDown () {
32+ PrometheusNaming .resetForTest ();
33+ }
34+
1435 @ Test
1536 public void testSanitizeMetricName () {
1637 assertThat (prometheusName (sanitizeMetricName ("0abc.def" ))).isEqualTo ("_abc_def" );
@@ -27,7 +48,6 @@ public void testSanitizeMetricName() {
2748
2849 @ Test
2950 public void testSanitizeMetricNameWithUnit () {
30-
3151 assertThat (prometheusName (sanitizeMetricName ("0abc.def" , Unit .RATIO )))
3252 .isEqualTo ("_abc_def_" + Unit .RATIO );
3353 assertThat (prometheusName (sanitizeMetricName ("___ab.:c0" , Unit .RATIO )))
@@ -46,7 +66,6 @@ public void testSanitizeMetricNameWithUnit() {
4666
4767 @ Test
4868 public void testSanitizeLabelName () {
49-
5069 assertThat (prometheusName (sanitizeLabelName ("0abc.def" ))).isEqualTo ("_abc_def" );
5170 assertThat (prometheusName (sanitizeLabelName ("_abc" ))).isEqualTo ("_abc" );
5271 assertThat (prometheusName (sanitizeLabelName ("__abc" ))).isEqualTo ("_abc" );
@@ -102,52 +121,50 @@ public void testEmptyUnitName() {
102121 .isThrownBy (() -> sanitizeUnitName ("" ));
103122 }
104123
105- @ Test
106- public void testMetricNameIsValid () {
107- assertThat (validateMetricName ("Avalid_23name" )).isNull ();
108- assertThat (validateMetricName ("_Avalid_23name" )).isNull ();
109- assertThat (validateMetricName ("1valid_23name" ))
110- .isEqualTo ("The metric name contains unsupported characters" );
111- assertThat (validateMetricName ("avalid_23name" )).isNull ();
112- assertThat (validateMetricName ("Ava:lid_23name" )).isNull ();
113- assertThat (validateMetricName ("a lid_23name" ))
114- .isEqualTo ("The metric name contains unsupported characters" );
115- assertThat (validateMetricName (":leading_colon" )).isNull ();
116- assertThat (validateMetricName ("colon:in:the:middle" )).isNull ();
117- assertThat (validateMetricName ("" )).isEqualTo ("The metric name contains unsupported characters" );
118- assertThat (validateMetricName ("a\ud800 z" ))
119- .isEqualTo ("The metric name contains unsupported characters" );
124+ @ SuppressWarnings ("unused" )
125+ @ SetSystemProperty (key = "io.prometheus.naming.validationScheme" , value = "utf-8" )
126+ @ ParameterizedTest
127+ @ MethodSource ("testLabelNameIsValid" )
128+ public void testLabelNameIsValidUtf8 (
129+ String labelName , boolean legacyValid , boolean utf8Valid , boolean legacyCharsetValid ) {
130+ PrometheusNaming .resetForTest ();
131+ assertMetricName (labelName , utf8Valid );
132+ // for some reason, an empty label name is considered valid in UTF-8 validation
133+ assertLabelName (labelName , utf8Valid || labelName .isEmpty ());
120134 }
121135
122- @ Test
123- public void testLabelNameIsValid () {
124- try (MockedStatic <PrometheusNaming > mock =
125- mockStatic (PrometheusNaming .class , CALLS_REAL_METHODS )) {
126- // Mock the validation scheme to use UTF-8 validation for this test
127- mock .when (PrometheusNaming ::getValidationScheme )
128- .thenReturn (ValidationScheme .UTF_8_VALIDATION );
136+ @ SuppressWarnings ("unused" )
137+ @ ParameterizedTest
138+ @ MethodSource ("testLabelNameIsValid" )
139+ public void testLabelNameIsValidLegacy (
140+ String labelName , boolean legacyValid , boolean utf8Valid , boolean legacyCharsetValid ) {
141+ assertMetricName (labelName , legacyCharsetValid );
142+ assertLabelName (labelName , legacyValid );
143+ }
129144
130- // These assertions now use UTF-8 validation behavior
131- assertThat (isValidLabelName ("Avalid_23name" )).isTrue ();
132- assertThat (isValidLabelName ("_Avalid_23name" )).isTrue ();
133- assertThat (isValidLabelName ("1valid_23name" )).isTrue ();
134- assertThat (isValidLabelName ("avalid_23name" )).isTrue ();
135- assertThat (isValidLabelName ("Ava:lid_23name" )).isTrue ();
136- assertThat (isValidLabelName ("a lid_23name" )).isTrue ();
137- assertThat (isValidLabelName (":leading_colon" )).isTrue ();
138- assertThat (isValidLabelName ("colon:in:the:middle" )).isTrue ();
139- assertThat (isValidLabelName ("a\ud800 z" )).isFalse ();
140- }
145+ private static void assertLabelName (String labelName , boolean legacyValid ) {
146+ assertThat (isValidLabelName (labelName )).describedAs ("isValidLabelName(%s)" , labelName )
147+ .isEqualTo (legacyValid );
148+ }
149+
150+ private static void assertMetricName (String labelName , boolean valid ) {
151+ assertThat (validateMetricName (labelName )).describedAs ("validateMetricName(%s)" , labelName )
152+ .isEqualTo (valid ? null : "The metric name contains unsupported characters" );
153+ }
141154
142- assertThat (isValidLabelName ("Avalid_23name" )).isTrue ();
143- assertThat (isValidLabelName ("_Avalid_23name" )).isTrue ();
144- assertThat (isValidLabelName ("1valid_23name" )).isFalse ();
145- assertThat (isValidLabelName ("avalid_23name" )).isTrue ();
146- assertThat (isValidLabelName ("Ava:lid_23name" )).isFalse ();
147- assertThat (isValidLabelName ("a lid_23name" )).isFalse ();
148- assertThat (isValidLabelName (":leading_colon" )).isFalse ();
149- assertThat (isValidLabelName ("colon:in:the:middle" )).isFalse ();
150- assertThat (isValidLabelName ("a\ud800 z" )).isFalse ();
155+ static Stream <Arguments > testLabelNameIsValid () {
156+ return Stream .of (
157+ Arguments .of ("" , false , false , false ),
158+ Arguments .of ("Avalid_23name" , true , true , true ),
159+ Arguments .of ("_Avalid_23name" , true , true , true ),
160+ Arguments .of ("1valid_23name" , false , true , false ),
161+ Arguments .of ("avalid_23name" , true , true , true ),
162+ Arguments .of ("Ava:lid_23name" , false , true , true ),
163+ Arguments .of ("a lid_23name" , false , true , false ),
164+ Arguments .of (":leading_colon" , false , true , true ),
165+ Arguments .of ("colon:in:the:middle" , false , true , true ),
166+ Arguments .of ("aΩz" , false , true , false ),
167+ Arguments .of ("a\ud800 z" , false , false , false ));
151168 }
152169
153170 @ Test
0 commit comments