@@ -127,6 +127,53 @@ func (s *ValidationScheme) UnmarshalYAML(unmarshal func(any) error) error {
127127 return nil
128128}
129129
130+ // IsValidMetricName returns whether metricName is valid according to s.
131+ func (s ValidationScheme ) IsValidMetricName (metricName string ) bool {
132+ switch s {
133+ case LegacyValidation :
134+ if len (metricName ) == 0 {
135+ return false
136+ }
137+ for i , b := range metricName {
138+ if ! isValidLegacyRune (b , i ) {
139+ return false
140+ }
141+ }
142+ return true
143+ case UTF8Validation :
144+ if len (metricName ) == 0 {
145+ return false
146+ }
147+ return utf8 .ValidString (metricName )
148+ default :
149+ panic (fmt .Sprintf ("Invalid name validation scheme requested: %s" , s .String ()))
150+ }
151+ }
152+
153+ // IsValidLabelName returns whether labelName is valid according to s.
154+ func (s ValidationScheme ) IsValidLabelName (labelName string ) bool {
155+ switch s {
156+ case LegacyValidation :
157+ if len (labelName ) == 0 {
158+ return false
159+ }
160+ for i , b := range labelName {
161+ // TODO: Apply De Morgan's law. Make sure there are tests for this.
162+ if ! ((b >= 'a' && b <= 'z' ) || (b >= 'A' && b <= 'Z' ) || b == '_' || (b >= '0' && b <= '9' && i > 0 )) { //nolint:staticcheck
163+ return false
164+ }
165+ }
166+ return true
167+ case UTF8Validation :
168+ if len (labelName ) == 0 {
169+ return false
170+ }
171+ return utf8 .ValidString (labelName )
172+ default :
173+ panic (fmt .Sprintf ("Invalid name validation scheme requested: %s" , s ))
174+ }
175+ }
176+
130177type EscapingScheme int
131178
132179const (
@@ -230,34 +277,22 @@ func (m Metric) FastFingerprint() Fingerprint {
230277// IsValidMetricName returns true iff name matches the pattern of MetricNameRE
231278// for legacy names, and iff it's valid UTF-8 if the UTF8Validation scheme is
232279// selected.
280+ //
281+ // Deprecated: This function should not be used and might be removed in the future.
282+ // Use [ValidationScheme.IsValidMetricName] instead.
233283func IsValidMetricName (n LabelValue ) bool {
234- switch NameValidationScheme {
235- case LegacyValidation :
236- return IsValidLegacyMetricName (string (n ))
237- case UTF8Validation :
238- if len (n ) == 0 {
239- return false
240- }
241- return utf8 .ValidString (string (n ))
242- default :
243- panic (fmt .Sprintf ("Invalid name validation scheme requested: %s" , NameValidationScheme .String ()))
244- }
284+ return NameValidationScheme .IsValidMetricName (string (n ))
245285}
246286
247287// IsValidLegacyMetricName is similar to IsValidMetricName but always uses the
248288// legacy validation scheme regardless of the value of NameValidationScheme.
249289// This function, however, does not use MetricNameRE for the check but a much
250290// faster hardcoded implementation.
291+ //
292+ // Deprecated: This function should not be used and might be removed in the future.
293+ // Use [LegacyValidation.IsValidMetricName] instead.
251294func IsValidLegacyMetricName (n string ) bool {
252- if len (n ) == 0 {
253- return false
254- }
255- for i , b := range n {
256- if ! isValidLegacyRune (b , i ) {
257- return false
258- }
259- }
260- return true
295+ return LegacyValidation .IsValidMetricName (n )
261296}
262297
263298// EscapeMetricFamily escapes the given metric names and labels with the given
0 commit comments