Skip to content

Commit

Permalink
Allow graphite parser to create Inf and NaN values (influxdata#6420)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and idohalevi committed Sep 23, 2020
1 parent 87b3193 commit 98d1b76
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
14 changes: 0 additions & 14 deletions plugins/parsers/graphite/errors.go

This file was deleted.

7 changes: 1 addition & 6 deletions plugins/parsers/graphite/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
42 changes: 34 additions & 8 deletions plugins/parsers/graphite/parser_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions testutil/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func IgnoreTime() cmp.Option {
}

// MetricEqual returns true if the metrics are equal.
func MetricEqual(expected, actual telegraf.Metric) bool {
func MetricEqual(expected, actual telegraf.Metric, opts ...cmp.Option) bool {
var lhs, rhs *metricDiff
if expected != nil {
lhs = newMetricDiff(expected)
Expand All @@ -138,7 +138,8 @@ func MetricEqual(expected, actual telegraf.Metric) bool {
rhs = newMetricDiff(actual)
}

return cmp.Equal(lhs, rhs)
opts = append(opts, cmpopts.EquateNaNs())
return cmp.Equal(lhs, rhs, opts...)
}

// RequireMetricEqual halts the test with an error if the metrics are not
Expand All @@ -154,6 +155,7 @@ func RequireMetricEqual(t *testing.T, expected, actual telegraf.Metric, opts ...
rhs = newMetricDiff(actual)
}

opts = append(opts, cmpopts.EquateNaNs())
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
Expand All @@ -172,6 +174,8 @@ func RequireMetricsEqual(t *testing.T, expected, actual []telegraf.Metric, opts
for _, m := range actual {
rhs = append(rhs, newMetricDiff(m))
}

opts = append(opts, cmpopts.EquateNaNs())
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
Expand Down

0 comments on commit 98d1b76

Please sign in to comment.