@@ -5,53 +5,30 @@ use std::collections::{BTreeSet, HashMap};
55use std:: hash:: Hasher ;
66
77use fnv:: FnvHasher ;
8+ use regex:: Regex ;
89
910use crate :: errors:: { Error , Result } ;
1011use crate :: metrics:: SEPARATOR_BYTE ;
1112use crate :: proto:: LabelPair ;
1213
1314// Details of required format are at
14- // https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
15+ // https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
1516fn is_valid_metric_name ( name : & str ) -> bool {
16- // Valid metric names must match regex [a-zA-Z_:][a-zA-Z0-9_:]*.
17- fn valid_start ( c : char ) -> bool {
18- c. is_ascii ( )
19- && match c as u8 {
20- b'a' ..=b'z' | b'A' ..=b'Z' | b'_' | b':' => true ,
21- _ => false ,
22- }
23- }
24-
25- fn valid_char ( c : char ) -> bool {
26- c. is_ascii ( )
27- && match c as u8 {
28- b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' | b'_' | b':' => true ,
29- _ => false ,
30- }
17+ lazy_static ! {
18+ static ref VALIDATOR : Regex =
19+ Regex :: new( "^[a-zA-Z_:][a-zA-Z0-9_:]*$" ) . expect( "Regex to be valid." ) ;
3120 }
3221
33- name . starts_with ( valid_start ) && ! name. contains ( |c| ! valid_char ( c ) )
22+ VALIDATOR . is_match ( name)
3423}
3524
3625fn is_valid_label_name ( name : & str ) -> bool {
37- // Valid label names must match regex [a-zA-Z_][a-zA-Z0-9_]*.
38- fn valid_start ( c : char ) -> bool {
39- c. is_ascii ( )
40- && match c as u8 {
41- b'a' ..=b'z' | b'A' ..=b'Z' | b'_' => true ,
42- _ => false ,
43- }
44- }
45-
46- fn valid_char ( c : char ) -> bool {
47- c. is_ascii ( )
48- && match c as u8 {
49- b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' | b'_' => true ,
50- _ => false ,
51- }
26+ lazy_static ! {
27+ static ref VALIDATOR : Regex =
28+ Regex :: new( "^[a-zA-Z_][a-zA-Z0-9_]*$" ) . expect( "Regex to be valid." ) ;
5229 }
5330
54- name . starts_with ( valid_start ) && ! name. contains ( |c| ! valid_char ( c ) )
31+ VALIDATOR . is_match ( name)
5532}
5633
5734/// The descriptor used by every Prometheus [`Metric`](crate::core::Metric). It is essentially
0 commit comments