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

Skip floats that are NaN or Inf in Datadog output. #6198

Merged
merged 1 commit into from
Aug 5, 2019
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
17 changes: 13 additions & 4 deletions plugins/outputs/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -63,9 +64,6 @@ func (d *Datadog) Connect() error {
}

func (d *Datadog) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
ts := TimeSeries{}
tempSeries := []*Metric{}
metricCounter := 0
Expand All @@ -75,6 +73,10 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
metricTags := buildTags(m.TagList())
host, _ := m.GetTag("host")

if len(dogMs) == 0 {
continue
}

for fieldName, dogM := range dogMs {
// name of the datadog measurement
var dname string
Expand All @@ -98,6 +100,10 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
}
}

if len(tempSeries) == 0 {
return nil
}

redactedApiKey := "****************"
ts.Series = make([]*Metric, metricCounter)
copy(ts.Series, tempSeries[0:])
Expand Down Expand Up @@ -166,9 +172,12 @@ func buildTags(tagList []*telegraf.Tag) []string {
}

func verifyValue(v interface{}) bool {
switch v.(type) {
switch v := v.(type) {
case string:
return false
case float64:
// The payload will be encoded as JSON, which does not allow NaN or Inf.
return !math.IsNaN(v) && !math.IsInf(v, 0)
}
return true
}
Expand Down
46 changes: 44 additions & 2 deletions plugins/outputs/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package datadog
import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/influxdata/telegraf/testutil"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -249,3 +249,45 @@ func TestVerifyValue(t *testing.T) {
}
}
}

func TestNaNIsSkipped(t *testing.T) {
plugin := &Datadog{
Apikey: "testing",
URL: "", // No request will be sent because all fields are skipped
}

err := plugin.Connect()
require.NoError(t, err)

err = plugin.Write([]telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": math.NaN(),
},
time.Now()),
})
require.NoError(t, err)
}

func TestInfIsSkipped(t *testing.T) {
plugin := &Datadog{
Apikey: "testing",
URL: "", // No request will be sent because all fields are skipped
}

err := plugin.Connect()
require.NoError(t, err)

err = plugin.Write([]telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": math.Inf(0),
},
time.Now()),
})
require.NoError(t, err)
}