Skip to content

Commit

Permalink
Use non-allocating field and tag accessors in datadog output (#4803)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinconaway authored and danielnelson committed Oct 5, 2018
1 parent 030f944 commit 422c142
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
27 changes: 13 additions & 14 deletions plugins/outputs/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"net/url"
"sort"
"strings"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
22 changes: 18 additions & 4 deletions plugins/outputs/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
}
Expand Down

0 comments on commit 422c142

Please sign in to comment.