Skip to content

Commit cebf8e3

Browse files
committed
Move changes behind build tag
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
1 parent 7da9bd1 commit cebf8e3

36 files changed

Lines changed: 880 additions & 187 deletions

expfmt/decode.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package expfmt
1515

1616
import (
17-
"bufio"
1817
"fmt"
1918
"io"
2019
"math"
@@ -29,7 +28,7 @@ import (
2928

3029
// Decoder types decode an input stream into metric families.
3130
type Decoder interface {
32-
Decode(*dto.MetricFamily, model.ValidationScheme) error
31+
Decode(*dto.MetricFamily) error
3332
}
3433

3534
// DecodeOptions contains options used by the Decoder and in sample extraction.
@@ -70,30 +69,15 @@ func ResponseFormat(h http.Header) Format {
7069
return FmtUnknown
7170
}
7271

73-
// NewDecoder returns a new decoder based on the given input format.
74-
// If the input format does not imply otherwise, a text format decoder is returned.
75-
func NewDecoder(r io.Reader, format Format) Decoder {
76-
switch format.FormatType() {
77-
case TypeProtoDelim:
78-
return &protoDecoder{r: bufio.NewReader(r)}
79-
}
80-
return &textDecoder{r: r}
81-
}
82-
83-
// protoDecoder implements the Decoder interface for protocol buffers.
84-
type protoDecoder struct {
85-
r protodelim.Reader
86-
}
87-
8872
// Decode implements the Decoder interface.
89-
func (d *protoDecoder) Decode(v *dto.MetricFamily, nameValidationScheme model.ValidationScheme) error {
73+
func (d *protoDecoder) Decode(v *dto.MetricFamily) error {
9074
opts := protodelim.UnmarshalOptions{
9175
MaxSize: -1,
9276
}
9377
if err := opts.UnmarshalFrom(d.r, v); err != nil {
9478
return err
9579
}
96-
if !model.IsValidMetricName(model.LabelValue(v.GetName()), nameValidationScheme) {
80+
if !d.isValidMetricName(v.GetName()) {
9781
return fmt.Errorf("invalid metric name %q", v.GetName())
9882
}
9983
for _, m := range v.GetMetric() {
@@ -107,7 +91,7 @@ func (d *protoDecoder) Decode(v *dto.MetricFamily, nameValidationScheme model.Va
10791
if !model.LabelValue(l.GetValue()).IsValid() {
10892
return fmt.Errorf("invalid label value %q", l.GetValue())
10993
}
110-
if !model.LabelName(l.GetName()).IsValid(nameValidationScheme) {
94+
if !d.isValidLabelName(l.GetName()) {
11195
return fmt.Errorf("invalid label name %q", l.GetName())
11296
}
11397
}
@@ -123,7 +107,7 @@ type textDecoder struct {
123107
}
124108

125109
// Decode implements the Decoder interface.
126-
func (d *textDecoder) Decode(v *dto.MetricFamily, _ model.ValidationScheme) error {
110+
func (d *textDecoder) Decode(v *dto.MetricFamily) error {
127111
if d.err == nil {
128112
// Read all metrics in one shot.
129113
var p TextParser
@@ -156,8 +140,8 @@ type SampleDecoder struct {
156140

157141
// Decode calls the Decode method of the wrapped Decoder and then extracts the
158142
// samples from the decoded MetricFamily into the provided model.Vector.
159-
func (sd *SampleDecoder) Decode(s *model.Vector, nameValidationScheme model.ValidationScheme) error {
160-
err := sd.Dec.Decode(&sd.f, nameValidationScheme)
143+
func (sd *SampleDecoder) Decode(s *model.Vector) error {
144+
err := sd.Dec.Decode(&sd.f)
161145
if err != nil {
162146
return err
163147
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !localvalidationscheme
15+
16+
package expfmt
17+
18+
import (
19+
"bufio"
20+
"io"
21+
22+
"google.golang.org/protobuf/encoding/protodelim"
23+
24+
"github.com/prometheus/common/model"
25+
)
26+
27+
// protoDecoder implements the Decoder interface for protocol buffers.
28+
type protoDecoder struct {
29+
r protodelim.Reader
30+
}
31+
32+
// NewDecoder returns a new decoder based on the given input format.
33+
// If the input format does not imply otherwise, a text format decoder is returned.
34+
func NewDecoder(r io.Reader, format Format) Decoder {
35+
switch format.FormatType() {
36+
case TypeProtoDelim:
37+
return &protoDecoder{r: bufio.NewReader(r)}
38+
}
39+
return &textDecoder{r: r}
40+
}
41+
42+
func (d *protoDecoder) isValidMetricName(name string) bool {
43+
return model.IsValidMetricName(model.LabelValue(name))
44+
}
45+
46+
func (d *protoDecoder) isValidLabelName(name string) bool {
47+
return model.LabelName(name).IsValid()
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !localvalidationscheme
15+
16+
package expfmt
17+
18+
import (
19+
"io"
20+
"testing"
21+
22+
"github.com/prometheus/common/model"
23+
)
24+
25+
func newDecoder(t *testing.T, r io.Reader, format Format, scheme model.ValidationScheme) Decoder {
26+
//nolint:staticcheck // NameValidationScheme is being phased out.
27+
origScheme := model.NameValidationScheme
28+
t.Cleanup(func() {
29+
//nolint:staticcheck // NameValidationScheme is being phased out.
30+
model.NameValidationScheme = origScheme
31+
})
32+
//nolint:staticcheck // NameValidationScheme is being phased out.
33+
model.NameValidationScheme = scheme
34+
35+
return NewDecoder(r, format)
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build localvalidationscheme
15+
16+
package expfmt
17+
18+
import (
19+
"bufio"
20+
"io"
21+
22+
"google.golang.org/protobuf/encoding/protodelim"
23+
24+
"github.com/prometheus/common/model"
25+
)
26+
27+
// protoDecoder implements the Decoder interface for protocol buffers.
28+
type protoDecoder struct {
29+
r protodelim.Reader
30+
validationScheme model.ValidationScheme
31+
}
32+
33+
// NewDecoder returns a new decoder based on the given input format.
34+
// If the input format does not imply otherwise, a text format decoder is returned.
35+
func NewDecoder(r io.Reader, format Format, validationScheme model.ValidationScheme) Decoder {
36+
switch format.FormatType() {
37+
case TypeProtoDelim:
38+
return &protoDecoder{
39+
r: bufio.NewReader(r),
40+
validationScheme: validationScheme,
41+
}
42+
}
43+
return &textDecoder{r: r}
44+
}
45+
46+
func (d *protoDecoder) isValidMetricName(name string) bool {
47+
return model.IsValidMetricName(model.LabelValue(name), d.validationScheme)
48+
}
49+
50+
func (d *protoDecoder) isValidLabelName(name string) bool {
51+
return model.LabelName(name).IsValid(d.validationScheme)
52+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build localvalidationscheme
15+
16+
package expfmt
17+
18+
import (
19+
"io"
20+
"testing"
21+
22+
"github.com/prometheus/common/model"
23+
)
24+
25+
func newDecoder(_ *testing.T, r io.Reader, format Format, scheme model.ValidationScheme) Decoder {
26+
return NewDecoder(r, format, scheme)
27+
}

expfmt/decode_test.go

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"reflect"
2525
"sort"
26+
"strconv"
2627
"strings"
2728
"testing"
2829

@@ -88,7 +89,7 @@ mf2 4
8889
var all model.Vector
8990
for {
9091
var smpls model.Vector
91-
err := dec.Decode(&smpls, model.UTF8Validation)
92+
err := dec.Decode(&smpls)
9293
if err != nil && errors.Is(err, io.EOF) {
9394
break
9495
}
@@ -360,44 +361,46 @@ func TestProtoDecoder(t *testing.T) {
360361
}
361362

362363
for i, scenario := range scenarios {
363-
dec := &SampleDecoder{
364-
Dec: &protoDecoder{r: strings.NewReader(scenario.in)},
365-
Opts: &DecodeOptions{
366-
Timestamp: testTime,
367-
},
368-
}
369-
370-
var all model.Vector
371-
for {
372-
var smpls model.Vector
373-
err := dec.Decode(&smpls, model.LegacyValidation)
374-
if err != nil && errors.Is(err, io.EOF) {
375-
break
364+
t.Run(strconv.Itoa(i), func(t *testing.T) {
365+
dec := &SampleDecoder{
366+
Dec: newDecoder(t, strings.NewReader(scenario.in), FmtProtoDelim, model.LegacyValidation),
367+
Opts: &DecodeOptions{
368+
Timestamp: testTime,
369+
},
376370
}
377-
if scenario.legacyNameFail {
378-
require.Errorf(t, err, "Expected error when decoding without UTF-8 support enabled but got none")
379-
dec = &SampleDecoder{
380-
Dec: &protoDecoder{r: strings.NewReader(scenario.in)},
381-
Opts: &DecodeOptions{
382-
Timestamp: testTime,
383-
},
371+
372+
var all model.Vector
373+
for {
374+
var smpls model.Vector
375+
err := dec.Decode(&smpls)
376+
if err != nil && errors.Is(err, io.EOF) {
377+
break
378+
}
379+
if scenario.legacyNameFail {
380+
require.Errorf(t, err, "Expected error when decoding without UTF-8 support enabled but got none")
381+
dec = &SampleDecoder{
382+
Dec: newDecoder(t, strings.NewReader(scenario.in), FmtProtoDelim, model.UTF8Validation),
383+
Opts: &DecodeOptions{
384+
Timestamp: testTime,
385+
},
386+
}
387+
err = dec.Decode(&smpls)
388+
if errors.Is(err, io.EOF) {
389+
break
390+
}
391+
require.NoErrorf(t, err, "Unexpected error when decoding with UTF-8 support: %v", err)
384392
}
385-
err = dec.Decode(&smpls, model.UTF8Validation)
386-
if errors.Is(err, io.EOF) {
393+
if scenario.fail {
394+
require.Errorf(t, err, "Expected error but got none")
387395
break
388396
}
389-
require.NoErrorf(t, err, "Unexpected error when decoding with UTF-8 support: %v", err)
397+
require.NoError(t, err)
398+
all = append(all, smpls...)
390399
}
391-
if scenario.fail {
392-
require.Errorf(t, err, "Expected error but got none")
393-
break
394-
}
395-
require.NoError(t, err)
396-
all = append(all, smpls...)
397-
}
398-
sort.Sort(all)
399-
sort.Sort(scenario.expected)
400-
require.Truef(t, reflect.DeepEqual(all, scenario.expected), "%d. output does not match, want: %#v, got %#v", i, scenario.expected, all)
400+
sort.Sort(all)
401+
sort.Sort(scenario.expected)
402+
require.Truef(t, reflect.DeepEqual(all, scenario.expected), "%d. output does not match, want: %#v, got %#v", i, scenario.expected, all)
403+
})
401404
}
402405
}
403406

@@ -406,11 +409,11 @@ func TestProtoMultiMessageDecoder(t *testing.T) {
406409
require.NoErrorf(t, err, "Reading file failed: %v", err)
407410

408411
buf := bytes.NewReader(data)
409-
decoder := NewDecoder(buf, FmtProtoDelim)
412+
decoder := newDecoder(t, buf, FmtProtoDelim, model.UTF8Validation)
410413
var metrics []*dto.MetricFamily
411414
for {
412415
var mf dto.MetricFamily
413-
if err := decoder.Decode(&mf, model.UTF8Validation); err != nil {
416+
if err := decoder.Decode(&mf); err != nil {
414417
if errors.Is(err, io.EOF) {
415418
break
416419
}
@@ -555,10 +558,10 @@ func TestTextDecoderWithBufioReader(t *testing.T) {
555558

556559
var decoded bool
557560
r := bufio.NewReader(strings.NewReader(example))
558-
dec := NewDecoder(r, FmtText)
561+
dec := newDecoder(t, r, FmtText, model.UTF8Validation)
559562
for {
560563
var mf dto.MetricFamily
561-
if err := dec.Decode(&mf, model.UTF8Validation); err != nil {
564+
if err := dec.Decode(&mf); err != nil {
562565
if errors.Is(err, io.EOF) {
563566
break
564567
}

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ require (
2525
github.com/davecgh/go-spew v1.1.1 // indirect
2626
github.com/jpillora/backoff v1.0.0 // indirect
2727
github.com/pmezard/go-difflib v1.0.0 // indirect
28-
github.com/prometheus/client_golang v1.20.4 // indirect
29-
github.com/prometheus/procfs v0.15.1 // indirect
28+
github.com/prometheus/client_golang v1.22.1-0.20250714085536-7c8795190db3 // indirect
29+
github.com/prometheus/procfs v0.16.1 // indirect
3030
github.com/rogpeppe/go-internal v1.10.0 // indirect
3131
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
3232
golang.org/x/sys v0.33.0 // indirect
@@ -36,5 +36,3 @@ require (
3636
)
3737

3838
retract v0.50.0 // Critical bug in counter suffixes, please read issue https://github.com/prometheus/common/issues/605
39-
40-
replace github.com/prometheus/client_golang => github.com/juliusmh/client_golang v1.22.1-0.20250701110037-ceb5803cbf1f

0 commit comments

Comments
 (0)