Skip to content

Commit b6dc9a8

Browse files
committed
address notes
Signed-off-by: Owen Williams <owen.williams@grafana.com>
1 parent d3a8808 commit b6dc9a8

File tree

2 files changed

+15
-69
lines changed

2 files changed

+15
-69
lines changed

expfmt/decode.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
dto "github.com/prometheus/client_model/go"
2525
"google.golang.org/protobuf/encoding/protodelim"
26-
"google.golang.org/protobuf/encoding/prototext"
2726

2827
"github.com/prometheus/common/model"
2928
)
@@ -76,11 +75,8 @@ func ResponseFormat(h http.Header) Format {
7675
// This decoder does not fully support OpenMetrics although it may often succeed
7776
// due to the similarities between the formats. This decoder may not support the
7877
// latest features of Prometheus text format and is not intended for
79-
// high-performance applications. The parsers in Prometheus
80-
// (https://github.com/prometheus/prometheus/blob/main/model/textparse/promparse.go
81-
// and
82-
// https://github.com/prometheus/prometheus/blob/main/model/textparse/openmetricsparse.go)
83-
// are more regularly maintained.
78+
// high-performance applications. See:
79+
// https://github.com/prometheus/common/issues/812
8480
func NewDecoder(r io.Reader, format Format) Decoder {
8581
scheme := model.LegacyValidation
8682
if format.ToEscapingScheme() == model.NoEscaping {
@@ -90,7 +86,7 @@ func NewDecoder(r io.Reader, format Format) Decoder {
9086
case TypeProtoDelim:
9187
return &protoDecoder{r: bufio.NewReader(r), s: scheme}
9288
case TypeProtoText, TypeProtoCompact:
93-
return &prototextDecoder{r: r, s: scheme}
89+
return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
9490
}
9591
return &textDecoder{r: r, s: scheme}
9692
}
@@ -131,43 +127,13 @@ func (d *protoDecoder) Decode(v *dto.MetricFamily) error {
131127
return nil
132128
}
133129

134-
// prototextDecoder implements the Decoder interface for protocol buffers that
135-
// are encoded in text format.
136-
type prototextDecoder struct {
137-
r io.Reader
138-
s model.ValidationScheme
130+
// errDecoder is an error-state decoder that always returns the same error.
131+
type errDecoder struct {
132+
err error
139133
}
140134

141-
// Decode implements the Decoder interface.
142-
func (d *prototextDecoder) Decode(v *dto.MetricFamily) error {
143-
opts := prototext.UnmarshalOptions{}
144-
b, err := io.ReadAll(d.r)
145-
if err != nil {
146-
return err
147-
}
148-
if err := opts.Unmarshal(b, v); err != nil {
149-
return err
150-
}
151-
if !d.s.IsValidMetricName(v.GetName()) {
152-
return fmt.Errorf("invalid metric name %q", v.GetName())
153-
}
154-
for _, m := range v.GetMetric() {
155-
if m == nil {
156-
continue
157-
}
158-
for _, l := range m.GetLabel() {
159-
if l == nil {
160-
continue
161-
}
162-
if !model.LabelValue(l.GetValue()).IsValid() {
163-
return fmt.Errorf("invalid label value %q", l.GetValue())
164-
}
165-
if !d.s.IsValidLabelName(l.GetName()) {
166-
return fmt.Errorf("invalid label name %q", l.GetName())
167-
}
168-
}
169-
}
170-
return nil
135+
func (d *errDecoder) Decode(v *dto.MetricFamily) error {
136+
return d.err
171137
}
172138

173139
// textDecoder implements the Decoder interface for the text protocol.

expfmt/encode_test.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -423,31 +423,6 @@ func TestDottedEncode(t *testing.T) {
423423
expectMetricName: "foo_dot_metric",
424424
expectLabelName: "dotted_dot_label_dot_name",
425425
},
426-
{
427-
format: FmtProtoCompact,
428-
expectMetricName: "foo_metric",
429-
expectLabelName: "dotted_label_name",
430-
},
431-
{
432-
format: FmtProtoCompact.WithEscapingScheme(model.NoEscaping),
433-
expectMetricName: "foo.metric",
434-
expectLabelName: "dotted.label.name",
435-
},
436-
{
437-
format: FmtProtoCompact.WithEscapingScheme(model.ValueEncodingEscaping),
438-
expectMetricName: "U__foo_2e_metric",
439-
expectLabelName: "U__dotted_2e_label_2e_name",
440-
},
441-
{
442-
format: FmtProtoText,
443-
expectMetricName: "foo_metric",
444-
expectLabelName: "dotted_label_name",
445-
},
446-
{
447-
format: FmtProtoText.WithEscapingScheme(model.NoEscaping),
448-
expectMetricName: "foo.metric",
449-
expectLabelName: "dotted.label.name",
450-
},
451426
{
452427
format: FmtText,
453428
expectMetricName: "foo_metric",
@@ -458,8 +433,13 @@ func TestDottedEncode(t *testing.T) {
458433
expectMetricName: "foo.metric",
459434
expectLabelName: "dotted.label.name",
460435
},
461-
// common library does not support open metrics parsing so we do not test
462-
// that here.
436+
{
437+
format: FmtText.WithEscapingScheme(model.DotsEscaping),
438+
expectMetricName: "foo_dot_metric",
439+
expectLabelName: "dotted_dot_label_dot_name",
440+
},
441+
// common library does not support proto text or open metrics parsing so we
442+
// do not test those here.
463443
}
464444

465445
for i, scenario := range scenarios {

0 commit comments

Comments
 (0)