3333 // names. Note that the IsValidMetricName function performs the same
3434 // check but faster than a match with this regular expression.
3535 MetricNameRE = regexp .MustCompile (`^[a-zA-Z_:][a-zA-Z0-9_:]*$` )
36+ // NameValidationScheme determines the default method of name validation to be
37+ // used. To avoid need for locking, this value should be set once, probably in
38+ // an init(), before multiple goroutines are started.
3639 NameValidationScheme = LegacyValidation
3740)
3841
@@ -95,26 +98,9 @@ func (m Metric) FastFingerprint() Fingerprint {
9598 return LabelSet (m ).FastFingerprint ()
9699}
97100
98- // IsValidLegacyMetricName returns true iff name matches the pattern of MetricNameRE
99- // for legacy names.
100- // This function, however, does not use MetricNameRE for the check but a much
101- // faster hardcoded implementation.
102- func IsValidLegacyMetricName (n LabelValue ) bool {
103- if len (n ) == 0 {
104- return false
105- }
106- for i , b := range n {
107- if ! ((b >= 'a' && b <= 'z' ) || (b >= 'A' && b <= 'Z' ) || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0 )) {
108- return false
109- }
110- }
111- return true
112- }
113-
114101// IsValidMetricName returns true iff name matches the pattern of MetricNameRE
115- // for legacy names, and iff it's valid UTF-8 if isUtf8 is true.
116- // This function, however, does not use MetricNameRE for the check but a much
117- // faster hardcoded implementation.
102+ // for legacy names, and iff it's valid UTF-8 if the UTF8Validation scheme is
103+ // selected.
118104func IsValidMetricName (n LabelValue ) bool {
119105 switch NameValidationScheme {
120106 case LegacyValidation :
@@ -128,3 +114,21 @@ func IsValidMetricName(n LabelValue) bool {
128114 panic (fmt .Sprintf ("Invalid name validation scheme requested: %d" , NameValidationScheme ))
129115 }
130116}
117+
118+ // IsValidLegacyMetricName is similar to IsValidMetricName but always uses the
119+ // legacy validation scheme regardless of the value of NameValidationScheme. It
120+ // returns true iff name matches the pattern of MetricNameRE for legacy names.
121+ // This function, however, does not use MetricNameRE for the check but a much
122+ // faster hardcoded implementation.
123+ func IsValidLegacyMetricName (n LabelValue ) bool {
124+ if len (n ) == 0 {
125+ return false
126+ }
127+ for i , b := range n {
128+ if ! ((b >= 'a' && b <= 'z' ) || (b >= 'A' && b <= 'Z' ) || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0 )) {
129+ return false
130+ }
131+ }
132+ return true
133+ }
134+
0 commit comments