Skip to content

Commit 0bfbd46

Browse files
authored
Merge pull request prometheus#16080 from prometheus/owilliams/escapeconfig-00-cleanup
utf8: Remove support for legacy global validation setting
2 parents c88d0b0 + 94b43c5 commit 0bfbd46

26 files changed

+39
-188
lines changed

cmd/prometheus/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ var (
140140
)
141141

142142
func init() {
143-
// This can be removed when the default validation scheme in common is updated.
143+
// This can be removed when the legacy global mode is fully deprecated.
144144
model.NameValidationScheme = model.UTF8Validation
145+
145146
prometheus.MustRegister(versioncollector.NewCollector(strings.ReplaceAll(appName, "-", "_")))
146147

147148
var err error

cmd/prometheus/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
)
4545

4646
func init() {
47-
// This can be removed when the default validation scheme in common is updated.
47+
// This can be removed when the legacy global mode is fully deprecated.
4848
model.NameValidationScheme = model.UTF8Validation
4949
}
5050

cmd/promtool/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import (
6363
)
6464

6565
func init() {
66-
// This can be removed when the default validation scheme in common is updated.
66+
// This can be removed when the legacy global mode is fully deprecated.
6767
model.NameValidationScheme = model.UTF8Validation
6868
}
6969

cmd/promtool/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
)
4141

4242
func init() {
43-
// This can be removed when the default validation scheme in common is updated.
43+
// This can be removed when the legacy global mode is fully deprecated.
4444
model.NameValidationScheme = model.UTF8Validation
4545
}
4646

config/config.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,11 @@ func (c *ScrapeConfig) Validate(globalConfig GlobalConfig) error {
837837
}
838838
}
839839

840+
if model.NameValidationScheme != model.UTF8Validation {
841+
return errors.New("model.NameValidationScheme must be set to UTF8")
842+
}
840843
switch globalConfig.MetricNameValidationScheme {
841-
case LegacyValidationConfig:
842-
case "", UTF8ValidationConfig:
843-
if model.NameValidationScheme != model.UTF8Validation {
844-
panic("utf8 name validation requested but model.NameValidationScheme is not set to UTF8")
845-
}
844+
case "", LegacyValidationConfig, UTF8ValidationConfig:
846845
default:
847846
return fmt.Errorf("unknown name validation method specified, must be either 'legacy' or 'utf8', got %s", globalConfig.MetricNameValidationScheme)
848847
}

config/config_test.go

+2-45
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package config
1515

1616
import (
1717
"crypto/tls"
18-
"encoding/json"
1918
"fmt"
2019
"net/url"
2120
"os"
@@ -62,11 +61,6 @@ import (
6261
"github.com/prometheus/prometheus/util/testutil"
6362
)
6463

65-
func init() {
66-
// This can be removed when the default validation scheme in common is updated.
67-
model.NameValidationScheme = model.UTF8Validation
68-
}
69-
7064
func mustParseURL(u string) *config.URL {
7165
parsed, err := url.Parse(u)
7266
if err != nil {
@@ -1716,11 +1710,7 @@ var expectedErrors = []struct {
17161710
},
17171711
{
17181712
filename: "labelname.bad.yml",
1719-
errMsg: `"not$allowed" is not a valid label name`,
1720-
},
1721-
{
1722-
filename: "labelname2.bad.yml",
1723-
errMsg: `"not:allowed" is not a valid label name`,
1713+
errMsg: `"\xff" is not a valid label name`,
17241714
},
17251715
{
17261716
filename: "labelvalue.bad.yml",
@@ -1792,16 +1782,12 @@ var expectedErrors = []struct {
17921782
},
17931783
{
17941784
filename: "labelmap.bad.yml",
1795-
errMsg: "\"l-$1\" is invalid 'replacement' for labelmap action",
1785+
errMsg: "!!binary value contains invalid base64 data",
17961786
},
17971787
{
17981788
filename: "lowercase.bad.yml",
17991789
errMsg: "relabel configuration for lowercase action requires 'target_label' value",
18001790
},
1801-
{
1802-
filename: "lowercase2.bad.yml",
1803-
errMsg: "\"42lab\" is invalid 'target_label' for lowercase action",
1804-
},
18051791
{
18061792
filename: "lowercase3.bad.yml",
18071793
errMsg: "'replacement' can not be set for lowercase action",
@@ -1810,10 +1796,6 @@ var expectedErrors = []struct {
18101796
filename: "uppercase.bad.yml",
18111797
errMsg: "relabel configuration for uppercase action requires 'target_label' value",
18121798
},
1813-
{
1814-
filename: "uppercase2.bad.yml",
1815-
errMsg: "\"42lab\" is invalid 'target_label' for uppercase action",
1816-
},
18171799
{
18181800
filename: "uppercase3.bad.yml",
18191801
errMsg: "'replacement' can not be set for uppercase action",
@@ -2181,34 +2163,14 @@ var expectedErrors = []struct {
21812163
}
21822164

21832165
func TestBadConfigs(t *testing.T) {
2184-
model.NameValidationScheme = model.LegacyValidation
2185-
defer func() {
2186-
model.NameValidationScheme = model.UTF8Validation
2187-
}()
21882166
for _, ee := range expectedErrors {
21892167
_, err := LoadFile("testdata/"+ee.filename, false, promslog.NewNopLogger())
21902168
require.ErrorContains(t, err, ee.errMsg,
21912169
"Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
21922170
}
21932171
}
21942172

2195-
func TestBadStaticConfigsJSON(t *testing.T) {
2196-
model.NameValidationScheme = model.LegacyValidation
2197-
defer func() {
2198-
model.NameValidationScheme = model.UTF8Validation
2199-
}()
2200-
content, err := os.ReadFile("testdata/static_config.bad.json")
2201-
require.NoError(t, err)
2202-
var tg targetgroup.Group
2203-
err = json.Unmarshal(content, &tg)
2204-
require.Error(t, err)
2205-
}
2206-
22072173
func TestBadStaticConfigsYML(t *testing.T) {
2208-
model.NameValidationScheme = model.LegacyValidation
2209-
defer func() {
2210-
model.NameValidationScheme = model.UTF8Validation
2211-
}()
22122174
content, err := os.ReadFile("testdata/static_config.bad.yml")
22132175
require.NoError(t, err)
22142176
var tg targetgroup.Group
@@ -2453,11 +2415,6 @@ func TestScrapeConfigDisableCompression(t *testing.T) {
24532415
}
24542416

24552417
func TestScrapeConfigNameValidationSettings(t *testing.T) {
2456-
model.NameValidationScheme = model.UTF8Validation
2457-
defer func() {
2458-
model.NameValidationScheme = model.LegacyValidation
2459-
}()
2460-
24612418
tests := []struct {
24622419
name string
24632420
inputFile string

config/testdata/labelmap.bad.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ scrape_configs:
22
- job_name: prometheus
33
relabel_configs:
44
- action: labelmap
5-
replacement: l-$1
5+
replacement: !!binary "/w==$1"

config/testdata/labelname.bad.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
global:
22
external_labels:
3-
not$allowed: value
3+
!!binary "/w==": value

config/testdata/labelname2.bad.yml

-3
This file was deleted.

config/testdata/lowercase2.bad.yml

-6
This file was deleted.

config/testdata/static_config.bad.json

-7
This file was deleted.

config/testdata/static_config.bad.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
targets: ['1.2.3.4:9001', '1.2.3.5:9090']
22
labels:
33
valid_label: foo
4-
not:valid_label: bar
4+
!!binary "/w==": bar

config/testdata/uppercase2.bad.yml

-6
This file was deleted.

model/labels/labels_common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ func (ls Labels) IsValid(validationScheme model.ValidationScheme) bool {
104104
if l.Name == model.MetricNameLabel {
105105
// If the default validation scheme has been overridden with legacy mode,
106106
// we need to call the special legacy validation checker.
107-
if validationScheme == model.LegacyValidation && model.NameValidationScheme == model.UTF8Validation && !model.IsValidLegacyMetricName(string(model.LabelValue(l.Value))) {
107+
if validationScheme == model.LegacyValidation && !model.IsValidLegacyMetricName(string(model.LabelValue(l.Value))) {
108108
return strconv.ErrSyntax
109109
}
110110
if !model.IsValidMetricName(model.LabelValue(l.Value)) {
111111
return strconv.ErrSyntax
112112
}
113113
}
114-
if validationScheme == model.LegacyValidation && model.NameValidationScheme == model.UTF8Validation {
114+
if validationScheme == model.LegacyValidation {
115115
if !model.LabelName(l.Name).IsValidLegacy() || !model.LabelValue(l.Value).IsValid() {
116116
return strconv.ErrSyntax
117117
}

model/labels/labels_test.go

+11-39
Original file line numberDiff line numberDiff line change
@@ -284,75 +284,47 @@ func TestLabels_IsValid(t *testing.T) {
284284

285285
func TestLabels_ValidationModes(t *testing.T) {
286286
for _, test := range []struct {
287-
input Labels
288-
globalMode model.ValidationScheme
289-
callMode model.ValidationScheme
290-
expected bool
287+
input Labels
288+
callMode model.ValidationScheme
289+
expected bool
291290
}{
292291
{
293292
input: FromStrings(
294293
"__name__", "test.metric",
295294
"hostname", "localhost",
296295
"job", "check",
297296
),
298-
globalMode: model.UTF8Validation,
299-
callMode: model.UTF8Validation,
300-
expected: true,
297+
callMode: model.UTF8Validation,
298+
expected: true,
301299
},
302300
{
303301
input: FromStrings(
304302
"__name__", "test",
305303
"\xc5 bad utf8", "localhost",
306304
"job", "check",
307305
),
308-
globalMode: model.UTF8Validation,
309-
callMode: model.UTF8Validation,
310-
expected: false,
311-
},
312-
{
313-
// Setting the common model to legacy validation and then trying to check for UTF-8 on a
314-
// per-call basis is not supported.
315-
input: FromStrings(
316-
"__name__", "test.utf8.metric",
317-
"hostname", "localhost",
318-
"job", "check",
319-
),
320-
globalMode: model.LegacyValidation,
321-
callMode: model.UTF8Validation,
322-
expected: false,
323-
},
324-
{
325-
input: FromStrings(
326-
"__name__", "test",
327-
"hostname", "localhost",
328-
"job", "check",
329-
),
330-
globalMode: model.LegacyValidation,
331-
callMode: model.LegacyValidation,
332-
expected: true,
306+
callMode: model.UTF8Validation,
307+
expected: false,
333308
},
334309
{
335310
input: FromStrings(
336311
"__name__", "test.utf8.metric",
337312
"hostname", "localhost",
338313
"job", "check",
339314
),
340-
globalMode: model.UTF8Validation,
341-
callMode: model.LegacyValidation,
342-
expected: false,
315+
callMode: model.LegacyValidation,
316+
expected: false,
343317
},
344318
{
345319
input: FromStrings(
346320
"__name__", "test",
347321
"host.name", "localhost",
348322
"job", "check",
349323
),
350-
globalMode: model.UTF8Validation,
351-
callMode: model.LegacyValidation,
352-
expected: false,
324+
callMode: model.LegacyValidation,
325+
expected: false,
353326
},
354327
} {
355-
model.NameValidationScheme = test.globalMode
356328
require.Equal(t, test.expected, test.input.IsValid(test.callMode))
357329
}
358330
}

model/relabel/relabel.go

-5
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ func (c *Config) Validate() error {
135135
// Design escaping mechanism to allow that, once valid use case appears.
136136
return model.LabelName(value).IsValid()
137137
}
138-
if model.NameValidationScheme == model.LegacyValidation {
139-
isValidLabelNameWithRegexVarFn = func(value string) bool {
140-
return relabelTargetLegacy.MatchString(value)
141-
}
142-
}
143138
if c.Action == Replace && varInRegexTemplate(c.TargetLabel) && !isValidLabelNameWithRegexVarFn(c.TargetLabel) {
144139
return fmt.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action)
145140
}

model/textparse/openmetricsparse_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,6 @@ foobar{quantile="0.99"} 150.1`
469469
}
470470

471471
func TestUTF8OpenMetricsParse(t *testing.T) {
472-
oldValidationScheme := model.NameValidationScheme
473-
model.NameValidationScheme = model.UTF8Validation
474-
defer func() {
475-
model.NameValidationScheme = oldValidationScheme
476-
}()
477-
478472
input := `# HELP "go.gc_duration_seconds" A summary of the GC invocation durations.
479473
# TYPE "go.gc_duration_seconds" summary
480474
# UNIT "go.gc_duration_seconds" seconds

model/textparse/promparse_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@ testmetric{le="10"} 1`
205205
}
206206

207207
func TestUTF8PromParse(t *testing.T) {
208-
oldValidationScheme := model.NameValidationScheme
209-
model.NameValidationScheme = model.UTF8Validation
210-
defer func() {
211-
model.NameValidationScheme = oldValidationScheme
212-
}()
213-
214208
input := `# HELP "go.gc_duration_seconds" A summary of the GC invocation durations.
215209
# TYPE "go.gc_duration_seconds" summary
216210
{"go.gc_duration_seconds",quantile="0"} 4.9351e-05

promql/parser/parse_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -3970,7 +3970,6 @@ func TestParseExpressions(t *testing.T) {
39703970
EnableExperimentalFunctions = false
39713971
})
39723972

3973-
model.NameValidationScheme = model.UTF8Validation
39743973
for _, test := range testExpr {
39753974
t.Run(readable(test.input), func(t *testing.T) {
39763975
expr, err := ParseExpr(test.input)

promql/parser/printer_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package parser
1616
import (
1717
"testing"
1818

19-
"github.com/prometheus/common/model"
2019
"github.com/stretchr/testify/require"
2120

2221
"github.com/prometheus/prometheus/model/labels"
@@ -170,8 +169,6 @@ func TestExprString(t *testing.T) {
170169
},
171170
}
172171

173-
model.NameValidationScheme = model.UTF8Validation
174-
175172
for _, test := range inputs {
176173
expr, err := ParseExpr(test.in)
177174
require.NoError(t, err)

promql/promqltest/test.go

-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ const (
5757
DefaultMaxSamplesPerQuery = 10000
5858
)
5959

60-
func init() {
61-
model.NameValidationScheme = model.UTF8Validation
62-
}
63-
6460
type TBRun interface {
6561
testing.TB
6662
Run(string, func(*testing.T)) bool

scrape/manager_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ import (
5656
"github.com/prometheus/prometheus/util/testutil"
5757
)
5858

59-
func init() {
60-
// This can be removed when the default validation scheme in common is updated.
61-
model.NameValidationScheme = model.UTF8Validation
62-
}
63-
6459
func TestPopulateLabels(t *testing.T) {
6560
cases := []struct {
6661
in model.LabelSet

0 commit comments

Comments
 (0)