33
33
// names. Note that the IsValidMetricName function performs the same
34
34
// check but faster than a match with this regular expression.
35
35
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.
36
39
NameValidationScheme = LegacyValidation
37
40
)
38
41
@@ -95,26 +98,9 @@ func (m Metric) FastFingerprint() Fingerprint {
95
98
return LabelSet (m ).FastFingerprint ()
96
99
}
97
100
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
-
114
101
// 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.
118
104
func IsValidMetricName (n LabelValue ) bool {
119
105
switch NameValidationScheme {
120
106
case LegacyValidation :
@@ -128,3 +114,21 @@ func IsValidMetricName(n LabelValue) bool {
128
114
panic (fmt .Sprintf ("Invalid name validation scheme requested: %d" , NameValidationScheme ))
129
115
}
130
116
}
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