Fix panic when parsing invalid lines

The line

```
|h|#consumer:Kafka::SharedConfigurationConsumer,topic:shared_configuration_update,partition:1,consumer_group:tc_rc_us
```

caused a panic because the line parsing _first_ splits by `:` and then failed to
find a `|` to split on.

Check that we get at least two "line parts" (i.e. splits around `|`) when we
expect them, and if not, gracefully reject the line instead of crashing.

Fixes #572.

Signed-off-by: Matthias Rampke <matthias@prometheus.io>
This commit is contained in:
Matthias Rampke 2024-09-15 09:55:11 +00:00
parent eccf511051
commit 0c2c1d96f9
No known key found for this signature in database
GPG key ID: F9AFF7F67ACE10BA
2 changed files with 14 additions and 3 deletions

View file

@ -211,7 +211,7 @@ func (p *Parser) LineToEvents(line string, sampleErrors prometheus.CounterVec, s
elements := strings.SplitN(line, ":", 2) elements := strings.SplitN(line, ":", 2)
if len(elements) < 2 || len(elements[0]) == 0 || !utf8.ValidString(line) { if len(elements) < 2 || len(elements[0]) == 0 || !utf8.ValidString(line) {
sampleErrors.WithLabelValues("malformed_line").Inc() sampleErrors.WithLabelValues("malformed_line").Inc()
level.Debug(logger).Log("msg", "Bad line from StatsD", "line", line) level.Debug(logger).Log("msg", "Bad line", "line", line)
return events return events
} }
@ -223,12 +223,17 @@ func (p *Parser) LineToEvents(line string, sampleErrors prometheus.CounterVec, s
// don't allow mixed tagging styles // don't allow mixed tagging styles
sampleErrors.WithLabelValues("mixed_tagging_styles").Inc() sampleErrors.WithLabelValues("mixed_tagging_styles").Inc()
level.Debug(logger).Log("msg", "Bad line (multiple tagging styles) from StatsD", "line", line) level.Debug(logger).Log("msg", "Bad line (multiple tagging styles)", "line", line)
return events return events
} }
var samples []string var samples []string
lineParts := strings.SplitN(elements[1], "|", 3) lineParts := strings.SplitN(elements[1], "|", 3)
if len(lineParts) < 2 {
sampleErrors.WithLabelValues("not_enough_parts_after_colon").Inc()
level.Debug(logger).Log("msg", "Bad line: not enough '|'-delimited parts after first ':'", "line", line)
return events
}
if strings.Contains(lineParts[0], ":") { if strings.Contains(lineParts[0], ":") {
// handle DogStatsD extended aggregation // handle DogStatsD extended aggregation
isValidAggType := false isValidAggType := false
@ -251,7 +256,7 @@ func (p *Parser) LineToEvents(line string, sampleErrors prometheus.CounterVec, s
samples = aggLines samples = aggLines
} else { } else {
sampleErrors.WithLabelValues("invalid_extended_aggregate_type").Inc() sampleErrors.WithLabelValues("invalid_extended_aggregate_type").Inc()
level.Debug(logger).Log("msg", "Bad line (invalid extended aggregate type) from StatsD", "line", line) level.Debug(logger).Log("msg", "Bad line (invalid extended aggregate type)", "line", line)
return events return events
} }
} else if usingDogStatsDTags { } else if usingDogStatsDTags {

View file

@ -823,6 +823,12 @@ func TestLineToEvents(t *testing.T) {
}, },
}, },
}, },
"invalid event split over lines part 1": {
in: "karafka.consumer.consume.cpu_idle_second: 0.111090 -0.055903 -0.195390 ( 2.419002)",
},
"invalid event split over lines part 2": {
in: "|h|#consumer:Kafka::SharedConfigurationConsumer,topic:shared_configuration_update,partition:1,consumer_group:tc_rc_us",
},
} }
parser := NewParser() parser := NewParser()