Skip to content

Commit a09de84

Browse files
committed
feat(model): add UnsetValidation scheme
1 parent 4e2833b commit a09de84

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

model/alert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestAlertValidate(t *testing.T) {
2727
cases := []struct {
2828
alert *Alert
2929
err string
30-
scheme NameValidationScheme
30+
scheme ValidationScheme
3131
}{
3232
{
3333
alert: &Alert{

model/labels.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (ln LabelName) IsValid(scheme ValidationScheme) bool {
110110
case UTF8Validation:
111111
return utf8.ValidString(string(ln))
112112
default:
113-
panic(fmt.Sprintf("Invalid name validation scheme requested: %d", scheme))
113+
panic(fmt.Sprintf("Invalid label name validation scheme requested: %d", scheme))
114114
}
115115
}
116116

model/metric.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4139
type ValidationScheme int
4240

4341
const (
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+
54100
type EscapingScheme int
55101

56102
const (
@@ -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

Comments
 (0)