Allow graphite parser to create Inf and NaN values (#6420)

This commit is contained in:
Daniel Nelson
2019-09-19 20:03:10 -07:00
committed by GitHub
parent e553341879
commit 8d96dd71c7
4 changed files with 41 additions and 30 deletions

View File

@@ -1,14 +0,0 @@
package graphite
import "fmt"
// An UnsupposedValueError is returned when a parsed value is not
// supposed.
type UnsupposedValueError struct {
Field string
Value float64
}
func (err *UnsupposedValueError) Error() string {
return fmt.Sprintf(`field "%s" value: "%v" is unsupported`, err.Field, err.Value)
}

View File

@@ -9,9 +9,8 @@ import (
"strings"
"time"
"github.com/influxdata/telegraf/internal/templating"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/templating"
"github.com/influxdata/telegraf/metric"
)
@@ -121,10 +120,6 @@ func (p *GraphiteParser) ParseLine(line string) (telegraf.Metric, error) {
return nil, fmt.Errorf(`field "%s" value: %s`, fields[0], err)
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return nil, &UnsupposedValueError{Field: fields[0], Value: v}
}
fieldValues := map[string]interface{}{}
if field != "" {
fieldValues[field] = v

View File

@@ -1,14 +1,14 @@
package graphite
import (
"reflect"
"math"
"strconv"
"testing"
"time"
"github.com/influxdata/telegraf/internal/templating"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -355,14 +355,40 @@ func TestParse(t *testing.T) {
func TestParseNaN(t *testing.T) {
p, err := NewGraphiteParser("", []string{"measurement*"}, nil)
assert.NoError(t, err)
require.NoError(t, err)
_, err = p.ParseLine("servers.localhost.cpu_load NaN 1435077219")
assert.Error(t, err)
m, err := p.ParseLine("servers.localhost.cpu_load NaN 1435077219")
require.NoError(t, err)
if _, ok := err.(*UnsupposedValueError); !ok {
t.Fatalf("expected *ErrUnsupportedValue, got %v", reflect.TypeOf(err))
}
expected := testutil.MustMetric(
"servers.localhost.cpu_load",
map[string]string{},
map[string]interface{}{
"value": math.NaN(),
},
time.Unix(1435077219, 0),
)
testutil.RequireMetricEqual(t, expected, m)
}
func TestParseInf(t *testing.T) {
p, err := NewGraphiteParser("", []string{"measurement*"}, nil)
require.NoError(t, err)
m, err := p.ParseLine("servers.localhost.cpu_load +Inf 1435077219")
require.NoError(t, err)
expected := testutil.MustMetric(
"servers.localhost.cpu_load",
map[string]string{},
map[string]interface{}{
"value": math.Inf(1),
},
time.Unix(1435077219, 0),
)
testutil.RequireMetricEqual(t, expected, m)
}
func TestFilterMatchDefault(t *testing.T) {