Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4699 Improve how the datadog plugin uses Metric.Fields and Metric.Tags #4803

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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