diff --git a/bridge_test.go b/bridge_test.go index 05cc90a..704ce70 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -204,6 +204,14 @@ func TestHandlePacket(t *testing.T) { name: "illegal stat type", in: "foo:2|t", }, + { + name: "empty metric name", + in: ":100|ms", + }, + { + name: "empty component", + in: "foo:1|c|", + }, } l := StatsDListener{} diff --git a/exporter.go b/exporter.go index df8f06c..7303f6b 100644 --- a/exporter.go +++ b/exporter.go @@ -352,7 +352,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) { } elements := strings.SplitN(line, ":", 2) - if len(elements) < 2 { + if len(elements) < 2 || len(elements[0]) == 0 { networkStats.WithLabelValues("malformed_line").Inc() log.Errorln("Bad line from StatsD:", line) continue @@ -365,7 +365,7 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) { } else { samples = strings.Split(elements[1], ":") } - for _, sample := range samples { + samples: for _, sample := range samples { components := strings.Split(sample, "|") samplingFactor := 1.0 if len(components) < 2 || len(components) > 4 { @@ -383,6 +383,14 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) { labels := map[string]string{} if len(components) >= 3 { + for _, component := range components[2:] { + if len(component) == 0 { + log.Errorln("Empty component on line: ", line) + networkStats.WithLabelValues("malformed_component").Inc() + continue samples + } + } + for _, component := range components[2:] { switch component[0] { case '@':