From 422c142463d16912ab033e135ca05a77c8a2be1a Mon Sep 17 00:00:00 2001 From: Kevin Conaway Date: Fri, 5 Oct 2018 16:48:18 -0400 Subject: [PATCH] Use non-allocating field and tag accessors in datadog output (#4803) --- plugins/outputs/datadog/datadog.go | 27 ++++++++++++------------- plugins/outputs/datadog/datadog_test.go | 22 ++++++++++++++++---- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/plugins/outputs/datadog/datadog.go b/plugins/outputs/datadog/datadog.go index 2ab3dcd58..2ef03a7b2 100644 --- a/plugins/outputs/datadog/datadog.go +++ b/plugins/outputs/datadog/datadog.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "net/url" - "sort" "strings" "github.com/influxdata/telegraf" @@ -76,6 +75,9 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error { for _, m := range metrics { if dogMs, err := buildMetrics(m); err == nil { + metricTags := buildTags(m.TagList()) + host, _ := m.GetTag("host") + for fieldName, dogM := range dogMs { // name of the datadog measurement var dname string @@ -85,11 +87,9 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error { } else { dname = m.Name() + "." + fieldName } - var host string - host, _ = m.Tags()["host"] metric := &Metric{ Metric: dname, - Tags: buildTags(m.Tags()), + Tags: metricTags, Host: host, } metric.Points[0] = dogM @@ -144,28 +144,27 @@ func (d *Datadog) authenticatedUrl() string { func buildMetrics(m telegraf.Metric) (map[string]Point, error) { ms := make(map[string]Point) - for k, v := range m.Fields() { - if !verifyValue(v) { + for _, field := range m.FieldList() { + if !verifyValue(field.Value) { continue } var p Point - if err := p.setValue(v); err != nil { - return ms, fmt.Errorf("unable to extract value from Fields %v error %v", k, err.Error()) + if err := p.setValue(field.Value); err != nil { + return ms, fmt.Errorf("unable to extract value from Fields %v error %v", field.Key, err.Error()) } p[0] = float64(m.Time().Unix()) - ms[k] = p + ms[field.Key] = p } return ms, nil } -func buildTags(mTags map[string]string) []string { - tags := make([]string, len(mTags)) +func buildTags(tagList []*telegraf.Tag) []string { + tags := make([]string, len(tagList)) index := 0 - for k, v := range mTags { - tags[index] = fmt.Sprintf("%s:%s", k, v) + for _, tag := range tagList { + tags[index] = fmt.Sprintf("%s:%s", tag.Key, tag.Value) index += 1 } - sort.Strings(tags) return tags } diff --git a/plugins/outputs/datadog/datadog_test.go b/plugins/outputs/datadog/datadog_test.go index 045bf4b43..f21ecc588 100644 --- a/plugins/outputs/datadog/datadog_test.go +++ b/plugins/outputs/datadog/datadog_test.go @@ -74,19 +74,33 @@ func TestAuthenticatedUrl(t *testing.T) { func TestBuildTags(t *testing.T) { var tagtests = []struct { - ptIn map[string]string + ptIn []*telegraf.Tag outTags []string }{ { - map[string]string{"one": "two", "three": "four"}, + []*telegraf.Tag{ + &telegraf.Tag{ + Key: "one", + Value: "two", + }, + &telegraf.Tag{ + Key: "three", + Value: "four", + }, + }, []string{"one:two", "three:four"}, }, { - map[string]string{"aaa": "bbb"}, + []*telegraf.Tag{ + &telegraf.Tag{ + Key: "aaa", + Value: "bbb", + }, + }, []string{"aaa:bbb"}, }, { - map[string]string{}, + []*telegraf.Tag{}, []string{}, }, }