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"
"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
}

View File

@ -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{},
},
}