Skip to content

Commit 2d1f151

Browse files
authored
Use prometheus/common.expfmt.NewTextParser (#1859)
* Use prometheus/common.expfmt.NewTextParser * Use explicit name validation schemes --------- Signed-off-by: Arve Knudsen <[email protected]>
1 parent 4ae032f commit 2d1f151

File tree

8 files changed

+19
-15
lines changed

8 files changed

+19
-15
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/klauspost/compress v1.18.0
1111
github.com/kylelemons/godebug v1.1.0
1212
github.com/prometheus/client_model v0.6.2
13-
github.com/prometheus/common v0.65.0
13+
github.com/prometheus/common v0.65.1-0.20250825082953-9deefbac4820
1414
github.com/prometheus/procfs v0.17.0
1515
go.uber.org/goleak v1.3.0
1616
golang.org/x/sys v0.34.0
@@ -24,9 +24,9 @@ require (
2424
github.com/modern-go/reflect2 v1.0.2 // indirect
2525
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2626
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
27-
golang.org/x/net v0.40.0 // indirect
27+
golang.org/x/net v0.42.0 // indirect
2828
golang.org/x/oauth2 v0.30.0 // indirect
29-
golang.org/x/text v0.25.0 // indirect
29+
golang.org/x/text v0.27.0 // indirect
3030
gopkg.in/yaml.v2 v2.4.0 // indirect
3131
)
3232

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3636
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
3737
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
38-
github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE=
39-
github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
38+
github.com/prometheus/common v0.65.1-0.20250825082953-9deefbac4820 h1:B78c+GuucJKi6g63ox+0L/SHB+Z6xMa+R9+70INFFjg=
39+
github.com/prometheus/common v0.65.1-0.20250825082953-9deefbac4820/go.mod h1:RGbSjGvjaWnQygUPajGOk7hD3bm75rored4huOYhktI=
4040
github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0=
4141
github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw=
4242
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
@@ -48,14 +48,14 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
4848
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4949
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5050
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
51-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
52-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
51+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
52+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
5353
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
5454
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
5555
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
5656
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
57-
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
58-
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
57+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
58+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
5959
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
6060
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
6161
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

prometheus/desc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, const
9595
help: help,
9696
variableLabels: variableLabels.compile(),
9797
}
98-
if !model.IsValidMetricName(model.LabelValue(fqName)) {
98+
//nolint:staticcheck // TODO: Don't use deprecated model.NameValidationScheme.
99+
if !model.NameValidationScheme.IsValidMetricName(fqName) {
99100
d.err = fmt.Errorf("%q is not a valid metric name", fqName)
100101
return d
101102
}

prometheus/examples_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
dto "github.com/prometheus/client_model/go"
2727
"github.com/prometheus/common/expfmt"
28+
"github.com/prometheus/common/model"
2829

2930
"github.com/prometheus/client_golang/prometheus"
3031
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -584,7 +585,7 @@ func ExampleGatherers() {
584585
temp.WithLabelValues("outside").Set(273.14)
585586
temp.WithLabelValues("inside").Set(298.44)
586587

587-
var parser expfmt.TextParser
588+
parser := expfmt.NewTextParser(model.UTF8Validation)
588589

589590
text := `
590591
# TYPE humidity_percent gauge

prometheus/internal/go_runtime_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func RuntimeMetricsToProm(d *metrics.Description) (string, string, string, bool)
6767
}
6868

6969
// Our current conversion moves to legacy naming, so use legacy validation.
70-
valid := model.IsValidLegacyMetricName(namespace + "_" + subsystem + "_" + name)
70+
valid := model.LegacyValidation.IsValidMetricName(namespace + "_" + subsystem + "_" + name)
7171
switch d.Kind {
7272
case metrics.KindUint64:
7373
case metrics.KindFloat64:

prometheus/labels.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,6 @@ func validateLabelValues(vals []string, expectedNumberOfValues int) error {
184184
}
185185

186186
func checkLabelName(l string) bool {
187-
return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix)
187+
//nolint:staticcheck // TODO: Don't use deprecated model.NameValidationScheme.
188+
return model.NameValidationScheme.IsValidLabelName(l) && !strings.HasPrefix(l, reservedLabelPrefix)
188189
}

prometheus/push/push.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ func (p *Pusher) Error() error {
182182
// For convenience, this method returns a pointer to the Pusher itself.
183183
func (p *Pusher) Grouping(name, value string) *Pusher {
184184
if p.error == nil {
185-
if !model.LabelName(name).IsValid() {
185+
//nolint:staticcheck // TODO: Don't use deprecated model.NameValidationScheme.
186+
if !model.NameValidationScheme.IsValidLabelName(name) {
186187
p.error = fmt.Errorf("grouping label has invalid name: %s", name)
187188
return p
188189
}

prometheus/testutil/testutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNa
262262
// convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of
263263
// dto.MetricFamily.
264264
func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error) {
265-
var tp expfmt.TextParser
265+
tp := expfmt.NewTextParser(model.UTF8Validation)
266266
notNormalized, err := tp.TextToMetricFamilies(reader)
267267
if err != nil {
268268
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)

0 commit comments

Comments
 (0)