Skip metrics with invalid utf8

This commit is contained in:
Ilya Margolin 2016-07-15 16:05:47 +02:00 committed by Ilya Margolin
parent 0c8b644ee1
commit bb4e42068f
3 changed files with 57 additions and 3 deletions

View file

@ -136,6 +136,22 @@ func TestHandlePacket(t *testing.T) {
labels: map[string]string{"tag1": "foo:bar"},
},
},
}, {
name: "datadog tag extension with invalid utf8 tag values",
in: "foo:100|c|@0.1|#tag:\xc3\x28invalid",
}, {
name: "datadog tag extension with both valid and invalid utf8 tag values",
in: "foo:100|c|@0.1|#tag1:valid,tag2:\xc3\x28invalid",
}, {
name: "multiple metrics with invalid datadog utf8 tag values",
in: "foo:200|c|#tag:value\nfoo:300|c|#tag:\xc3\x28invalid",
out: Events{
&CounterEvent{
metricName: "foo",
value: 200,
labels: map[string]string{"tag": "value"},
},
},
}, {
name: "combined multiline metrics",
in: "foo:200|ms:300|ms:5|c|@0.1:6|g\nbar:1|c:5|ms",
@ -212,6 +228,21 @@ func TestHandlePacket(t *testing.T) {
name: "empty component",
in: "foo:1|c|",
},
{
name: "invalid utf8",
in: "invalid\xc3\x28utf8:1|c",
},
{
name: "some invalid utf8",
in: "valid_utf8:1|c\ninvalid\xc3\x28utf8:1|c",
out: Events{
&CounterEvent{
metricName: "valid_utf8",
value: 1,
labels: map[string]string{},
},
},
},
}
l := StatsDListener{}

View file

@ -22,6 +22,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -350,9 +351,8 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
if line == "" {
continue
}
elements := strings.SplitN(line, ":", 2)
if len(elements) < 2 || len(elements[0]) == 0 {
if len(elements) < 2 || len(elements[0]) == 0 || !utf8.ValidString(line) {
networkStats.WithLabelValues("malformed_line").Inc()
log.Errorln("Bad line from StatsD:", line)
continue
@ -365,7 +365,8 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
} else {
samples = strings.Split(elements[1], ":")
}
samples: for _, sample := range samples {
samples:
for _, sample := range samples {
components := strings.Split(sample, "|")
samplingFactor := 1.0
if len(components) < 2 || len(components) > 4 {

View file

@ -50,3 +50,25 @@ func TestNegativeCounter(t *testing.T) {
ex.Listen(events)
}
// TestInvalidUtf8InDatadogTagValue validates robustness of exporter listener
// against datadog tags with invalid tag values.
// It sends the same tags first with a valid value, then with an invalid one.
// The exporter should not panic, but drop the invalid event
func TestInvalidUtf8InDatadogTagValue(t *testing.T) {
l := StatsDListener{}
events := make(chan Events, 2)
l.handlePacket([]byte("bar:200|c|#tag:value"), events)
l.handlePacket([]byte("bar:200|c|#tag:\xc3\x28invalid"), events)
ex := NewExporter(&metricMapper{}, true)
// Close channel to signify we are done with the listener after a short period.
go func() {
time.Sleep(time.Millisecond * 100)
close(events)
}()
ex.Listen(events)
}