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

This commit is contained in:
Daniel Nelson 2019-08-05 14:50:29 -07:00 committed by GitHub
parent de6416ff82
commit b5710a6a21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"math"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -63,9 +64,6 @@ func (d *Datadog) Connect() error {
} }
func (d *Datadog) Write(metrics []telegraf.Metric) error { func (d *Datadog) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
ts := TimeSeries{} ts := TimeSeries{}
tempSeries := []*Metric{} tempSeries := []*Metric{}
metricCounter := 0 metricCounter := 0
@ -75,6 +73,10 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
metricTags := buildTags(m.TagList()) metricTags := buildTags(m.TagList())
host, _ := m.GetTag("host") host, _ := m.GetTag("host")
if len(dogMs) == 0 {
continue
}
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
@ -98,6 +100,10 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
} }
} }
if len(tempSeries) == 0 {
return nil
}
redactedApiKey := "****************" redactedApiKey := "****************"
ts.Series = make([]*Metric, metricCounter) ts.Series = make([]*Metric, metricCounter)
copy(ts.Series, tempSeries[0:]) copy(ts.Series, tempSeries[0:])
@ -166,9 +172,12 @@ func buildTags(tagList []*telegraf.Tag) []string {
} }
func verifyValue(v interface{}) bool { func verifyValue(v interface{}) bool {
switch v.(type) { switch v := v.(type) {
case string: case string:
return false 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 return true
} }

View File

@ -3,15 +3,15 @@ package datadog
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"testing" "testing"
"time" "time"
"github.com/influxdata/telegraf/testutil"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -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)
}