Skip to content

Commit b459db3

Browse files
committed
do not try to decode openmetrics, which is not supported
Signed-off-by: Owen Williams <owen.williams@grafana.com>
1 parent 032f3f6 commit b459db3

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

expfmt/decode.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package expfmt
1515

1616
import (
1717
"bufio"
18+
"errors"
1819
"fmt"
1920
"io"
2021
"math"
@@ -73,14 +74,17 @@ func ResponseFormat(h http.Header) Format {
7374

7475
// NewDecoder returns a new decoder based on the given input format.
7576
// If the input format does not imply otherwise, a text format decoder is returned.
76-
func NewDecoder(r io.Reader, format Format) Decoder {
77+
func NewDecoder(r io.Reader, format Format) (Decoder, error) {
7778
switch format.FormatType() {
7879
case TypeProtoDelim:
79-
return &protoDecoder{r: bufio.NewReader(r)}
80+
return &protoDecoder{r: bufio.NewReader(r)}, nil
8081
case TypeProtoText, TypeProtoCompact:
81-
return &prototextDecoder{r: r}
82+
return &prototextDecoder{r: r}, nil
8283
}
83-
return &textDecoder{r: r}
84+
if format.FormatType() == TypeOpenMetrics {
85+
return nil, errors.New("cannot decode openmetrics")
86+
}
87+
return &textDecoder{r: r}, nil
8488
}
8589

8690
// protoDecoder implements the Decoder interface for protocol buffers.

expfmt/decode_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ func TestProtoMultiMessageDecoder(t *testing.T) {
408408
require.NoErrorf(t, err, "Reading file failed: %v", err)
409409

410410
buf := bytes.NewReader(data)
411-
decoder := NewDecoder(buf, FmtProtoDelim)
411+
decoder, err := NewDecoder(buf, FmtProtoDelim)
412+
require.NoError(t, err)
412413
var metrics []*dto.MetricFamily
413414
for {
414415
var mf dto.MetricFamily
@@ -557,7 +558,8 @@ func TestTextDecoderWithBufioReader(t *testing.T) {
557558

558559
var decoded bool
559560
r := bufio.NewReader(strings.NewReader(example))
560-
dec := NewDecoder(r, FmtText)
561+
dec, err := NewDecoder(r, FmtText)
562+
require.NoError(t, err)
561563
for {
562564
var mf dto.MetricFamily
563565
if err := dec.Decode(&mf); err != nil {

expfmt/encode_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,10 @@ func TestEscapedEncode(t *testing.T) {
344344
t.Errorf("expected the output bytes buffer to be non-empty")
345345
}
346346

347-
dec := NewDecoder(bytes.NewReader(out), FmtProtoDelim)
347+
dec, err := NewDecoder(bytes.NewReader(out), FmtProtoDelim)
348+
if err != nil {
349+
t.Errorf("unexpected error calling NewDecoder: %s", err.Error())
350+
}
348351
var gotFamily dto.MetricFamily
349352
err = dec.Decode(&gotFamily)
350353
if err != nil {
@@ -366,7 +369,10 @@ func TestEscapedEncode(t *testing.T) {
366369
if len(out) == 0 {
367370
t.Errorf("expected the output bytes buffer to be non-empty")
368371
}
369-
dec = NewDecoder(bytes.NewReader(out), FmtProtoCompact)
372+
dec, err = NewDecoder(bytes.NewReader(out), FmtProtoCompact)
373+
if err != nil {
374+
t.Errorf("unexpected error calling NewDecoder: %s", err.Error())
375+
}
370376
err = dec.Decode(&gotFamily)
371377
if err != nil {
372378
t.Errorf("unexpected error during proto decode: %s", err.Error())
@@ -386,7 +392,10 @@ func TestEscapedEncode(t *testing.T) {
386392
if len(out) == 0 {
387393
t.Errorf("expected the output bytes buffer to be non-empty")
388394
}
389-
dec = NewDecoder(bytes.NewReader(out), FmtProtoText)
395+
dec, err = NewDecoder(bytes.NewReader(out), FmtProtoText)
396+
if err != nil {
397+
t.Errorf("unexpected error calling NewDecoder: %s", err.Error())
398+
}
390399
err = dec.Decode(&gotFamily)
391400
if err != nil {
392401
t.Errorf("unexpected error during proto decode: %s", err.Error())
@@ -496,16 +505,8 @@ func TestDottedEncode(t *testing.T) {
496505
expectMetricName: "foo.metric",
497506
expectLabelName: "dotted.label.name",
498507
},
499-
{
500-
format: FmtOpenMetrics_1_0_0,
501-
expectMetricName: "foo_metric",
502-
expectLabelName: "dotted_label_name",
503-
},
504-
{
505-
format: FmtOpenMetrics_1_0_0.WithEscapingScheme(model.NoEscaping),
506-
expectMetricName: "foo.metric",
507-
expectLabelName: "dotted.label.name",
508-
},
508+
// common library does not support open metrics parsing so we do not test
509+
// that here.
509510
}
510511

511512
for i, scenario := range scenarios {
@@ -517,7 +518,10 @@ func TestDottedEncode(t *testing.T) {
517518
continue
518519
}
519520

520-
dec := NewDecoder(bytes.NewReader(out.Bytes()), scenario.format)
521+
dec, err := NewDecoder(bytes.NewReader(out.Bytes()), scenario.format)
522+
if err != nil {
523+
t.Errorf("unexpected error calling NewDecoder: %s", err.Error())
524+
}
521525
var gotFamily dto.MetricFamily
522526
err = dec.Decode(&gotFamily)
523527
if err != nil {

expfmt/text_parse.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,19 @@ func (p *TextParser) readingType() stateFn {
574574
if p.readTokenUntilNewline(false); p.err != nil {
575575
return nil // Unexpected end of input.
576576
}
577-
var metricType int32
578577
typString := strings.ToUpper(p.currentToken.String())
579578
// OpenMetrics uses UNKNOWN for untyped metrics instead of UNTYPED, so we add
580579
// a special case for that here.
581580
if typString == "UNKNOWN" {
582-
metricType = int32(dto.MetricType_UNTYPED)
583-
} else {
584-
var ok bool
585-
metricType, ok = dto.MetricType_value[typString]
586-
if !ok {
587-
p.parseError(fmt.Sprintf("unknown metric type %q", p.currentToken.String()))
588-
return nil
589-
}
581+
p.parseError("found metric type UNKNOWN, is the input OpenMetrics instead of Prometheus format?")
582+
return nil
590583
}
584+
metricType, ok := dto.MetricType_value[typString]
585+
if !ok {
586+
p.parseError(fmt.Sprintf("unknown metric type %q", p.currentToken.String()))
587+
return nil
588+
}
589+
591590
p.currentMF.Type = dto.MetricType(metricType).Enum()
592591
return p.startOfLine
593592
}

expfmt/text_parse_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ my_summary{n1="val1",quantile="0.9"} 140 1
181181
my_summary_count{n1="val1"} 42
182182
# Latest timestamp wins in case of a summary.
183183
my_summary_sum{n1="val1"} 4711 2
184-
# TYPE fake_sum unknown
185184
fake_sum{n1="val1"} 2001
186185
# TYPE another_summary summary
187186
another_summary_count{n2="val2",n1="val1"} 20
@@ -192,10 +191,10 @@ my_summary{n1="val3", quantile="0.2"} 4711
192191
my_summary{n1="val1",n2="val2",quantile="-12.34",} NaN
193192
# some
194193
# funny comments
195-
# HELP
196194
# HELP
195+
# HELP
196+
# HELP my_summary
197197
# HELP my_summary
198-
# HELP my_summary
199198
`,
200199
out: []*dto.MetricFamily{
201200
{

0 commit comments

Comments
 (0)