Fix panic with empty datadog tag string (#6088)

This commit is contained in:
Daniel Nelson 2019-07-08 14:44:36 -07:00 committed by GitHub
parent 04937d0498
commit 5dea2175d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 69 deletions

View File

@ -138,6 +138,10 @@ func (s *Statsd) parseEventMessage(now time.Time, message string, defaultHostnam
} }
func parseDataDogTags(tags map[string]string, message string) { func parseDataDogTags(tags map[string]string, message string) {
if len(message) == 0 {
return
}
start, i := 0, 0 start, i := 0, 0
var k string var k string
var inVal bool // check if we are parsing the value part of the tag var inVal bool // check if we are parsing the value part of the tag

View File

@ -847,85 +847,122 @@ func TestParse_Tags(t *testing.T) {
} }
} }
// Test that DataDog tags are parsed
func TestParse_DataDogTags(t *testing.T) { func TestParse_DataDogTags(t *testing.T) {
s := NewTestStatsd() tests := []struct {
s.DataDogExtensions = true name string
line string
lines := []string{ expected []telegraf.Metric
"my_counter:1|c|#host:localhost,environment:prod,endpoint:/:tenant?/oauth/ro", }{
"my_gauge:10.1|g|#live", {
"my_set:1|s|#host:localhost", name: "counter",
"my_timer:3|ms|@0.1|#live,host:localhost", line: "my_counter:1|c|#host:localhost,environment:prod,endpoint:/:tenant?/oauth/ro",
} expected: []telegraf.Metric{
testutil.MustMetric(
expectedTags := map[string]map[string]string{ "my_counter",
"my_counter": { map[string]string{
"host": "localhost",
"environment": "prod",
"endpoint": "/:tenant?/oauth/ro", "endpoint": "/:tenant?/oauth/ro",
"environment": "prod",
"host": "localhost",
"metric_type": "counter", "metric_type": "counter",
}, },
map[string]interface{}{
"my_gauge": { "value": 1,
},
time.Now(),
),
},
},
{
name: "gauge",
line: "my_gauge:10.1|g|#live",
expected: []telegraf.Metric{
testutil.MustMetric(
"my_gauge",
map[string]string{
"live": "true", "live": "true",
"metric_type": "gauge", "metric_type": "gauge",
}, },
map[string]interface{}{
"my_set": { "value": 10.1,
},
time.Now(),
),
},
},
{
name: "set",
line: "my_set:1|s|#host:localhost",
expected: []telegraf.Metric{
testutil.MustMetric(
"my_set",
map[string]string{
"host": "localhost", "host": "localhost",
"metric_type": "set", "metric_type": "set",
}, },
map[string]interface{}{
"my_timer": { "value": 1,
"live": "true", },
time.Now(),
),
},
},
{
name: "timer",
line: "my_timer:3|ms|@0.1|#live,host:localhost",
expected: []telegraf.Metric{
testutil.MustMetric(
"my_timer",
map[string]string{
"host": "localhost", "host": "localhost",
"live": "true",
"metric_type": "timing", "metric_type": "timing",
}, },
map[string]interface{}{
"count": 10,
"lower": float64(3),
"mean": float64(3),
"stddev": float64(0),
"sum": float64(30),
"upper": float64(3),
},
time.Now(),
),
},
},
{
name: "empty tag set",
line: "cpu:42|c|#",
expected: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{
"metric_type": "counter",
},
map[string]interface{}{
"value": 42,
},
time.Now(),
),
},
},
} }
for _, line := range lines { for _, tt := range tests {
err := s.parseStatsdLine(line) t.Run(tt.name, func(t *testing.T) {
if err != nil { var acc testutil.Accumulator
t.Errorf("Parsing line %s should not have resulted in an error\n", line)
}
}
actualTags := map[string]map[string]string{ s := NewTestStatsd()
"my_gauge": tagsForItem(s.gauges), s.DataDogExtensions = true
"my_counter": tagsForItem(s.counters),
"my_set": tagsForItem(s.sets),
"my_timer": tagsForItem(s.timings),
}
for name, tags := range expectedTags {
for expectedK, expectedV := range tags {
if expectedV != actualTags[name][expectedK] {
t.Errorf("failed: expected: %#v != %#v", tags, actualTags[name])
}
}
}
}
func tagsForItem(m interface{}) map[string]string { err := s.parseStatsdLine(tt.line)
switch m.(type) { require.NoError(t, err)
case map[string]cachedcounter: err = s.Gather(&acc)
for _, v := range m.(map[string]cachedcounter) { require.NoError(t, err)
return v.tags
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(),
testutil.SortMetrics(), testutil.IgnoreTime())
})
} }
case map[string]cachedgauge:
for _, v := range m.(map[string]cachedgauge) {
return v.tags
}
case map[string]cachedset:
for _, v := range m.(map[string]cachedset) {
return v.tags
}
case map[string]cachedtimings:
for _, v := range m.(map[string]cachedtimings) {
return v.tags
}
}
return nil
} }
// Test that statsd buckets are parsed to measurement names properly // Test that statsd buckets are parsed to measurement names properly