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", "endpoint": "/:tenant?/oauth/ro",
"environment": "prod", "environment": "prod",
"endpoint": "/:tenant?/oauth/ro", "host": "localhost",
"metric_type": "counter", "metric_type": "counter",
},
map[string]interface{}{
"value": 1,
},
time.Now(),
),
},
}, },
{
"my_gauge": { name: "gauge",
"live": "true", line: "my_gauge:10.1|g|#live",
"metric_type": "gauge", expected: []telegraf.Metric{
testutil.MustMetric(
"my_gauge",
map[string]string{
"live": "true",
"metric_type": "gauge",
},
map[string]interface{}{
"value": 10.1,
},
time.Now(),
),
},
}, },
{
"my_set": { name: "set",
"host": "localhost", line: "my_set:1|s|#host:localhost",
"metric_type": "set", expected: []telegraf.Metric{
testutil.MustMetric(
"my_set",
map[string]string{
"host": "localhost",
"metric_type": "set",
},
map[string]interface{}{
"value": 1,
},
time.Now(),
),
},
}, },
{
"my_timer": { name: "timer",
"live": "true", line: "my_timer:3|ms|@0.1|#live,host:localhost",
"host": "localhost", expected: []telegraf.Metric{
"metric_type": "timing", testutil.MustMetric(
"my_timer",
map[string]string{
"host": "localhost",
"live": "true",
"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(),
case map[string]cachedgauge: testutil.SortMetrics(), testutil.IgnoreTime())
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