@@ -26,31 +26,77 @@ import (
26
26
"google.golang.org/protobuf/proto"
27
27
)
28
28
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
40
38
// be validated by this library.
41
39
type ValidationScheme int
42
40
43
41
const (
42
+ // UnsetValidation represents an undefined ValidationScheme.
43
+ // Should not be used in practice.
44
+ UnsetValidation ValidationScheme = iota
45
+
44
46
// LegacyValidation is a setting that requires that all metric and label names
45
47
// conform to the original Prometheus character requirements described by
46
48
// MetricNameRE and LabelNameRE.
47
- LegacyValidation ValidationScheme = iota
49
+ LegacyValidation
48
50
49
51
// UTF8Validation only requires that metric and label names be valid UTF-8
50
52
// strings.
51
53
UTF8Validation
52
54
)
53
55
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
+
54
100
type EscapingScheme int
55
101
56
102
const (
@@ -164,7 +210,7 @@ func IsValidMetricName(n LabelValue, scheme ValidationScheme) bool {
164
210
}
165
211
return utf8 .ValidString (string (n ))
166
212
default :
167
- panic (fmt .Sprintf ("Invalid name validation scheme requested: %d" , scheme ))
213
+ panic (fmt .Sprintf ("Invalid metric name validation scheme requested: %d" , scheme ))
168
214
}
169
215
}
170
216
0 commit comments