Skip to content

Commit 6b9636c

Browse files
authored
model: Clarify the purpose of model.NameValidationScheme (#765)
There is a lot of confusion around this global variable. Clarify that it is meant to indicate that a project is aware of UTF-8 support, and that those projects should have their own flags to control validation mode. Signed-off-by: Owen Williams <[email protected]>
1 parent 56f6f38 commit 6b9636c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Diff for: expfmt/decode_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,15 @@ func TestProtoDecoder(t *testing.T) {
369369

370370
var all model.Vector
371371
for {
372-
model.NameValidationScheme = model.LegacyValidation
372+
model.NameValidationScheme = model.LegacyValidation //nolint:staticcheck
373373
var smpls model.Vector
374374
err := dec.Decode(&smpls)
375375
if err != nil && errors.Is(err, io.EOF) {
376376
break
377377
}
378378
if scenario.legacyNameFail {
379379
require.Errorf(t, err, "Expected error when decoding without UTF-8 support enabled but got none")
380-
model.NameValidationScheme = model.UTF8Validation
380+
model.NameValidationScheme = model.UTF8Validation //nolint:staticcheck
381381
dec = &SampleDecoder{
382382
Dec: &protoDecoder{r: strings.NewReader(scenario.in)},
383383
Opts: &DecodeOptions{

Diff for: model/metric.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ import (
2727
)
2828

2929
var (
30-
// NameValidationScheme determines the method of name validation to be used by
31-
// all calls to IsValidMetricName() and LabelName IsValid(). Setting UTF-8
32-
// mode in isolation from other components that don't support UTF-8 may result
33-
// in bugs or other undefined behavior. This value can be set to
34-
// LegacyValidation during startup if a binary is not UTF-8-aware binaries. To
35-
// avoid need for locking, this value should be set once, ideally in an
36-
// init(), before multiple goroutines are started.
30+
// NameValidationScheme determines the global default method of the name
31+
// validation to be used by all calls to IsValidMetricName() and LabelName
32+
// IsValid().
33+
//
34+
// Deprecated: This variable should not be used and might be removed in the
35+
// far future. If you wish to stick to the legacy name validation use
36+
// `IsValidLegacyMetricName()` and `LabelName.IsValidLegacy()` methods
37+
// instead. This variable is here as an escape hatch for emergency cases,
38+
// given the recent change from `LegacyValidation` to `UTF8Validation`, e.g.,
39+
// to delay UTF-8 migrations in time or aid in debugging unforeseen results of
40+
// the change. In such a case, a temporary assignment to `LegacyValidation`
41+
// value in the `init()` function in your main.go or so, could be considered.
42+
//
43+
// Historically we opted for a global variable for feature gating different
44+
// validation schemes in operations that were not otherwise easily adjustable
45+
// (e.g. Labels yaml unmarshaling). That could have been a mistake, a separate
46+
// Labels structure or package might have been a better choice. Given the
47+
// change was made and many upgraded the common already, we live this as-is
48+
// with this warning and learning for the future.
3749
NameValidationScheme = UTF8Validation
3850

3951
// NameEscapingScheme defines the default way that names will be escaped when
@@ -50,7 +62,7 @@ var (
5062
type ValidationScheme int
5163

5264
const (
53-
// LegacyValidation is a setting that requirets that metric and label names
65+
// LegacyValidation is a setting that requires that all metric and label names
5466
// conform to the original Prometheus character requirements described by
5567
// MetricNameRE and LabelNameRE.
5668
LegacyValidation ValidationScheme = iota

0 commit comments

Comments
 (0)