Use non-allocating field and tag accessors in datadog output (#4803)

This commit is contained in:
Kevin Conaway 2018-10-05 16:48:18 -04:00 committed by Daniel Nelson
parent 030f944505
commit 422c142463
2 changed files with 31 additions and 18 deletions

View File

@ -7,7 +7,6 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"sort"
"strings" "strings"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -76,6 +75,9 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
for _, m := range metrics { for _, m := range metrics {
if dogMs, err := buildMetrics(m); err == nil { if dogMs, err := buildMetrics(m); err == nil {
metricTags := buildTags(m.TagList())
host, _ := m.GetTag("host")
for fieldName, dogM := range dogMs { for fieldName, dogM := range dogMs {
// name of the datadog measurement // name of the datadog measurement
var dname string var dname string
@ -85,11 +87,9 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
} else { } else {
dname = m.Name() + "." + fieldName dname = m.Name() + "." + fieldName
} }
var host string
host, _ = m.Tags()["host"]
metric := &Metric{ metric := &Metric{
Metric: dname, Metric: dname,
Tags: buildTags(m.Tags()), Tags: metricTags,
Host: host, Host: host,
} }
metric.Points[0] = dogM metric.Points[0] = dogM
@ -144,28 +144,27 @@ func (d *Datadog) authenticatedUrl() string {
func buildMetrics(m telegraf.Metric) (map[string]Point, error) { func buildMetrics(m telegraf.Metric) (map[string]Point, error) {
ms := make(map[string]Point) ms := make(map[string]Point)
for k, v := range m.Fields() { for _, field := range m.FieldList() {
if !verifyValue(v) { if !verifyValue(field.Value) {
continue continue
} }
var p Point var p Point
if err := p.setValue(v); err != nil { if err := p.setValue(field.Value); err != nil {
return ms, fmt.Errorf("unable to extract value from Fields %v error %v", k, err.Error()) return ms, fmt.Errorf("unable to extract value from Fields %v error %v", field.Key, err.Error())
} }
p[0] = float64(m.Time().Unix()) p[0] = float64(m.Time().Unix())
ms[k] = p ms[field.Key] = p
} }
return ms, nil return ms, nil
} }
func buildTags(mTags map[string]string) []string { func buildTags(tagList []*telegraf.Tag) []string {
tags := make([]string, len(mTags)) tags := make([]string, len(tagList))
index := 0 index := 0
for k, v := range mTags { for _, tag := range tagList {
tags[index] = fmt.Sprintf("%s:%s", k, v) tags[index] = fmt.Sprintf("%s:%s", tag.Key, tag.Value)
index += 1 index += 1
} }
sort.Strings(tags)
return tags return tags
} }

View File

@ -74,19 +74,33 @@ func TestAuthenticatedUrl(t *testing.T) {
func TestBuildTags(t *testing.T) { func TestBuildTags(t *testing.T) {
var tagtests = []struct { var tagtests = []struct {
ptIn map[string]string ptIn []*telegraf.Tag
outTags []string 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"}, []string{"one:two", "three:four"},
}, },
{ {
map[string]string{"aaa": "bbb"}, []*telegraf.Tag{
&telegraf.Tag{
Key: "aaa",
Value: "bbb",
},
},
[]string{"aaa:bbb"}, []string{"aaa:bbb"},
}, },
{ {
map[string]string{}, []*telegraf.Tag{},
[]string{}, []string{},
}, },
} }