Skip to content

Commit 73da0f0

Browse files
committed
Refine, add comments
1 parent c317dde commit 73da0f0

2 files changed

Lines changed: 69 additions & 83 deletions

File tree

expfmt/decode.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,16 @@ func ResponseFormat(h http.Header) Format {
7171
return FmtUnknown
7272
}
7373

74-
// NewDecoder returns a new decoder based on the given input format. If the
75-
// input format does not imply otherwise, a text format decoder is returned.
76-
// This decoder does not fully support OpenMetrics and
74+
// NewDecoder returns a new decoder based on the given input format. Supported
75+
// formats include delimited protobuf, text protos, and Prometheus text format.
76+
// This decoder does not fully support OpenMetrics although it may often succeed
77+
// due to the similarities between the formats. This decoder may not support the
78+
// 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.
7784
func NewDecoder(r io.Reader, format Format) Decoder {
7885
switch format.FormatType() {
7986
case TypeProtoDelim:

expfmt/encode_test.go

Lines changed: 59 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"net/http"
1919
"testing"
2020

21+
"github.com/stretchr/testify/require"
2122
"google.golang.org/protobuf/proto"
2223

2324
"github.com/prometheus/common/model"
@@ -309,8 +310,54 @@ foo_metric 1.234
309310
}
310311

311312
func TestEscapedEncode(t *testing.T) {
312-
var buff bytes.Buffer
313-
delimEncoder := NewEncoder(&buff, FmtProtoDelim+"; escaping=underscores")
313+
tests := []struct {
314+
name string
315+
format Format
316+
expectedOutput string // empty means no specific output check
317+
}{
318+
{
319+
name: "ProtoDelim with escaping",
320+
format: FmtProtoDelim + "; escaping=underscores",
321+
expectedOutput: "M\n\nfoo_metric\x18\x03\"\v*\t\tX9\xb4\xc8v\xbe\xf3?\"0\n#\n\x11dotted_label_name\x12\x0emy.label.value*\t\t\x00\x00\x00\x00\x00\x00 @",
322+
},
323+
{
324+
name: "ProtoCompact",
325+
format: FmtProtoCompact,
326+
expectedOutput: `name:"foo_metric" type:UNTYPED metric:{untyped:{value:1.234}} metric:{label:{name:"dotted_label_name" value:"my.label.value"} untyped:{value:8}}
327+
`,
328+
},
329+
{
330+
name: "ProtoText",
331+
format: FmtProtoText,
332+
expectedOutput: `name: "foo_metric"
333+
type: UNTYPED
334+
metric: {
335+
untyped: {
336+
value: 1.234
337+
}
338+
}
339+
metric: {
340+
label: {
341+
name: "dotted_label_name"
342+
value: "my.label.value"
343+
}
344+
untyped: {
345+
value: 8
346+
}
347+
}
348+
349+
`,
350+
},
351+
{
352+
name: "Text",
353+
format: FmtText,
354+
expectedOutput: `# TYPE foo_metric untyped
355+
foo_metric 1.234
356+
foo_metric{dotted_label_name="my.label.value"} 8
357+
`,
358+
},
359+
}
360+
314361
metric := &dto.MetricFamily{
315362
Name: proto.String("foo.metric"),
316363
Type: dto.MetricType_UNTYPED.Enum(),
@@ -334,86 +381,18 @@ func TestEscapedEncode(t *testing.T) {
334381
},
335382
}
336383

337-
err := delimEncoder.Encode(metric)
338-
if err != nil {
339-
t.Errorf("unexpected error during encode: %s", err.Error())
340-
}
341-
342-
out := buff.Bytes()
343-
if len(out) == 0 {
344-
t.Errorf("expected the output bytes buffer to be non-empty")
345-
}
346-
347-
dec := NewDecoder(bytes.NewReader(out), FmtProtoDelim)
348-
var gotFamily dto.MetricFamily
349-
err = dec.Decode(&gotFamily)
350-
if err != nil {
351-
t.Errorf("unexpected error during proto decode: %s", err.Error())
352-
}
353-
expectEscapedName := "foo_metric"
354-
if gotFamily.GetName() != expectEscapedName {
355-
t.Errorf("incorrect encoded metric name, want %v, got %v", expectEscapedName, gotFamily.GetName())
356-
}
357-
358-
buff.Reset()
359-
compactEncoder := NewEncoder(&buff, FmtProtoCompact)
360-
err = compactEncoder.Encode(metric)
361-
if err != nil {
362-
t.Errorf("unexpected error during encode: %s", err.Error())
363-
}
364-
365-
out = buff.Bytes()
366-
if len(out) == 0 {
367-
t.Errorf("expected the output bytes buffer to be non-empty")
368-
}
369-
dec = NewDecoder(bytes.NewReader(out), FmtProtoCompact)
370-
err = dec.Decode(&gotFamily)
371-
if err != nil {
372-
t.Errorf("unexpected error during proto decode: %s", err.Error())
373-
}
374-
if gotFamily.GetName() != "foo_metric" {
375-
t.Errorf("incorrect encoded metricname , want foo_metric, got %v", gotFamily.GetName())
376-
}
377-
378-
buff.Reset()
379-
protoTextEncoder := NewEncoder(&buff, FmtProtoText)
380-
err = protoTextEncoder.Encode(metric)
381-
if err != nil {
382-
t.Errorf("unexpected error during encode: %s", err.Error())
383-
}
384-
385-
out = buff.Bytes()
386-
if len(out) == 0 {
387-
t.Errorf("expected the output bytes buffer to be non-empty")
388-
}
389-
dec = NewDecoder(bytes.NewReader(out), FmtProtoText)
390-
err = dec.Decode(&gotFamily)
391-
if err != nil {
392-
t.Errorf("unexpected error during proto decode: %s", err.Error())
393-
}
394-
if gotFamily.GetName() != "foo_metric" {
395-
t.Errorf("incorrect encoded metric name, want foo_metic, got %v", gotFamily.GetName())
396-
}
397-
398-
buff.Reset()
399-
textEncoder := NewEncoder(&buff, FmtText)
400-
err = textEncoder.Encode(metric)
401-
if err != nil {
402-
t.Errorf("unexpected error during encode: %s", err.Error())
403-
}
404-
405-
out = buff.Bytes()
406-
if len(out) == 0 {
407-
t.Errorf("expected the output bytes buffer to be non-empty")
408-
}
384+
for _, tt := range tests {
385+
t.Run(tt.name, func(t *testing.T) {
386+
var buff bytes.Buffer
387+
encoder := NewEncoder(&buff, tt.format)
388+
err := encoder.Encode(metric)
389+
require.NoError(t, err)
409390

410-
expected := `# TYPE foo_metric untyped
411-
foo_metric 1.234
412-
foo_metric{dotted_label_name="my.label.value"} 8
413-
`
391+
out := buff.Bytes()
392+
require.NotEmptyf(t, out, "expected the output bytes buffer to be non-empty")
414393

415-
if string(out) != expected {
416-
t.Errorf("expected TextEncoder to return %s, but got %s instead", expected, string(out))
394+
require.Equal(t, tt.expectedOutput, string(out))
395+
})
417396
}
418397
}
419398

0 commit comments

Comments
 (0)