@@ -26,31 +26,77 @@ import (
2626 "google.golang.org/protobuf/proto"
2727)
2828
29- var (
30- // NameEscapingScheme defines the default way that names will be escaped when
31- // presented to systems that do not support UTF-8 names. If the Content-Type
32- // "escaping" term is specified, that will override this value.
33- // NameEscapingScheme should not be set to the NoEscaping value. That string
34- // is used in content negotiation to indicate that a system supports UTF-8 and
35- // has that feature enabled.
36- NameEscapingScheme = UnderscoreEscaping
37- )
38-
39- // NameValidationScheme is a Go enum for determining how metric and label names will
29+ // NameEscapingScheme defines the default way that names will be escaped when
30+ // presented to systems that do not support UTF-8 names. If the Content-Type
31+ // "escaping" term is specified, that will override this value.
32+ // NameEscapingScheme should not be set to the NoEscaping value. That string
33+ // is used in content negotiation to indicate that a system supports UTF-8 and
34+ // has that feature enabled.
35+ var NameEscapingScheme = UnderscoreEscaping
36+
37+ // ValidationScheme is a Go enum for determining how metric and label names will
4038// be validated by this library.
4139type ValidationScheme int
4240
4341const (
42+ // UnsetValidation represents an undefined ValidationScheme.
43+ // Should not be used in practice.
44+ UnsetValidation ValidationScheme = iota
45+
4446 // LegacyValidation is a setting that requires that all metric and label names
4547 // conform to the original Prometheus character requirements described by
4648 // MetricNameRE and LabelNameRE.
47- LegacyValidation ValidationScheme = iota
49+ LegacyValidation
4850
4951 // UTF8Validation only requires that metric and label names be valid UTF-8
5052 // strings.
5153 UTF8Validation
5254)
5355
56+ func (s ValidationScheme ) String () string {
57+ switch s {
58+ case UnsetValidation :
59+ return "unset"
60+ case LegacyValidation :
61+ return "legacy"
62+ case UTF8Validation :
63+ return "utf8"
64+ default :
65+ panic (fmt .Errorf ("invalid ValidationScheme: %d" , s ))
66+ }
67+ }
68+
69+ // MarshalYAML implements the yaml.Marshaler interface.
70+ func (s ValidationScheme ) MarshalYAML () (any , error ) {
71+ switch s {
72+ case UnsetValidation :
73+ return "" , nil
74+ case LegacyValidation , UTF8Validation :
75+ return s .String (), nil
76+ default :
77+ panic (fmt .Errorf ("invalid ValidationScheme: %d" , s ))
78+ }
79+ }
80+
81+ // UnmarshalYAML implements the yaml.Unmarshaler interface.
82+ func (s * ValidationScheme ) UnmarshalYAML (unmarshal func (any ) error ) error {
83+ var scheme string
84+ if err := unmarshal (& scheme ); err != nil {
85+ return err
86+ }
87+ switch scheme {
88+ case "" :
89+ // Don't change the value.
90+ case "legacy" :
91+ * s = LegacyValidation
92+ case "utf8" :
93+ * s = UTF8Validation
94+ default :
95+ return fmt .Errorf ("invalid ValidationScheme: %q" , scheme )
96+ }
97+ return nil
98+ }
99+
54100type EscapingScheme int
55101
56102const (
@@ -164,7 +210,7 @@ func IsValidMetricName(n LabelValue, scheme ValidationScheme) bool {
164210 }
165211 return utf8 .ValidString (string (n ))
166212 default :
167- panic (fmt .Sprintf ("Invalid name validation scheme requested: %d" , scheme ))
213+ panic (fmt .Sprintf ("Invalid metric name validation scheme requested: %d" , scheme ))
168214 }
169215}
170216
0 commit comments