From bb4e42068f195dc5672f02924583dcd4aed576e2 Mon Sep 17 00:00:00 2001 From: Ilya Margolin Date: Fri, 15 Jul 2016 16:05:47 +0200 Subject: [PATCH] Skip metrics with invalid utf8 --- bridge_test.go | 31 +++++++++++++++++++++++++++++++ exporter.go | 7 ++++--- exporter_test.go | 22 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/bridge_test.go b/bridge_test.go index 704ce70..312869e 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -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{} diff --git a/exporter.go b/exporter.go index 7303f6b..ec8df97 100644 --- a/exporter.go +++ b/exporter.go @@ -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 { diff --git a/exporter_test.go b/exporter_test.go index fb806e3..1eb4bf5 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -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) +}