Skip to content

Commit 0ad974f

Browse files
authored
Merge pull request #809 from thampiotr/fix-escape-delim-proto
Fix delimited proto not escaped correctly
2 parents c79a891 + 96a9730 commit 0ad974f

File tree

2 files changed

+42
-58
lines changed

2 files changed

+42
-58
lines changed

expfmt/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder {
153153
case TypeProtoDelim:
154154
return encoderCloser{
155155
encode: func(v *dto.MetricFamily) error {
156-
_, err := protodelim.MarshalTo(w, v)
156+
_, err := protodelim.MarshalTo(w, model.EscapeMetricFamily(v, escapingScheme))
157157
return err
158158
},
159159
close: func() error { return nil },

expfmt/encode_test.go

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

21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
2123
"google.golang.org/protobuf/proto"
2224

2325
"github.com/prometheus/common/model"
@@ -309,8 +311,32 @@ foo_metric 1.234
309311
}
310312

311313
func TestEscapedEncode(t *testing.T) {
312-
var buff bytes.Buffer
313-
delimEncoder := NewEncoder(&buff, FmtProtoDelim+"; escaping=underscores")
314+
tests := []struct {
315+
name string
316+
format Format
317+
}{
318+
{
319+
name: "ProtoDelim",
320+
format: FmtProtoDelim,
321+
},
322+
{
323+
name: "ProtoDelim with escaping underscores",
324+
format: FmtProtoDelim + "; escaping=underscores",
325+
},
326+
{
327+
name: "ProtoCompact",
328+
format: FmtProtoCompact,
329+
},
330+
{
331+
name: "ProtoText",
332+
format: FmtProtoText,
333+
},
334+
{
335+
name: "Text",
336+
format: FmtText,
337+
},
338+
}
339+
314340
metric := &dto.MetricFamily{
315341
Name: proto.String("foo.metric"),
316342
Type: dto.MetricType_UNTYPED.Enum(),
@@ -334,61 +360,19 @@ func TestEscapedEncode(t *testing.T) {
334360
},
335361
}
336362

337-
err := delimEncoder.Encode(metric)
338-
if err != nil {
339-
t.Errorf("unexpected error during encode: %s", err.Error())
340-
}
363+
for _, tt := range tests {
364+
t.Run(tt.name, func(t *testing.T) {
365+
var buff bytes.Buffer
366+
encoder := NewEncoder(&buff, tt.format)
367+
err := encoder.Encode(metric)
368+
require.NoError(t, err)
341369

342-
out := buff.Bytes()
343-
if len(out) == 0 {
344-
t.Errorf("expected the output bytes buffer to be non-empty")
345-
}
346-
347-
buff.Reset()
348-
349-
compactEncoder := NewEncoder(&buff, FmtProtoCompact)
350-
err = compactEncoder.Encode(metric)
351-
if err != nil {
352-
t.Errorf("unexpected error during encode: %s", err.Error())
353-
}
354-
355-
out = buff.Bytes()
356-
if len(out) == 0 {
357-
t.Errorf("expected the output bytes buffer to be non-empty")
358-
}
359-
360-
buff.Reset()
361-
362-
protoTextEncoder := NewEncoder(&buff, FmtProtoText)
363-
err = protoTextEncoder.Encode(metric)
364-
if err != nil {
365-
t.Errorf("unexpected error during encode: %s", err.Error())
366-
}
367-
368-
out = buff.Bytes()
369-
if len(out) == 0 {
370-
t.Errorf("expected the output bytes buffer to be non-empty")
371-
}
372-
373-
buff.Reset()
374-
375-
textEncoder := NewEncoder(&buff, FmtText)
376-
err = textEncoder.Encode(metric)
377-
if err != nil {
378-
t.Errorf("unexpected error during encode: %s", err.Error())
379-
}
380-
381-
out = buff.Bytes()
382-
if len(out) == 0 {
383-
t.Errorf("expected the output bytes buffer to be non-empty")
384-
}
385-
386-
expected := `# TYPE foo_metric untyped
387-
foo_metric 1.234
388-
foo_metric{dotted_label_name="my.label.value"} 8
389-
`
390-
391-
if string(out) != expected {
392-
t.Errorf("expected TextEncoder to return %s, but got %s instead", expected, string(out))
370+
s := buff.String()
371+
assert.NotContains(t, s, "foo.metric")
372+
assert.Contains(t, s, "foo_metric")
373+
assert.NotContains(t, s, "dotted.label.name")
374+
assert.Contains(t, s, "dotted_label_name")
375+
assert.Contains(t, s, "my.label.value")
376+
})
393377
}
394378
}

0 commit comments

Comments
 (0)