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

Ignored Nan and Infinite float values #73

Merged
merged 4 commits into from
Sep 2, 2020
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
19 changes: 16 additions & 3 deletions internal/integration/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package integration

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand All @@ -30,10 +31,16 @@ type Emitter interface {
Emit([]Metric) error
}

// Harvester aggregates and reports metrics and spans
type harvester interface {
RecordMetric(m telemetry.Metric)
HarvestNow(ct context.Context)
}

// TelemetryEmitter emits metrics using the go-telemetry-sdk.
type TelemetryEmitter struct {
name string
harvester *telemetry.Harvester
harvester harvester
deltaCalculator *cumulative.DeltaCalculator
}

Expand Down Expand Up @@ -161,14 +168,19 @@ func NewTelemetryEmitter(cfg TelemetryEmitterConfig) (*TelemetryEmitter, error)
deltaExpirationCheckInterval,
)

harvester, err := telemetry.NewHarvester(cfg.HarvesterOpts...)
var h harvester
h, err := telemetry.NewHarvester(cfg.HarvesterOpts...)
if err != nil {
return nil, errors.Wrap(err, "could not create new Harvester")
}

// Wrap the harvester so we can filter out invalid float values: NaN and Infinity.
// If we do send them, the harvester will always output these as errors
h = harvesterDecorator{h}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice approach!


return &TelemetryEmitter{
name: "telemetry",
harvester: harvester,
harvester: h,
deltaCalculator: dc,
}, nil
}
Expand All @@ -187,6 +199,7 @@ func (te *TelemetryEmitter) Emit(metrics []Metric) error {
// the measurement that already took place.
now := time.Now()
for _, metric := range metrics {

switch metric.metricType {
case metricType_GAUGE:
te.harvester.RecordMetric(telemetry.Gauge{
Expand Down
6 changes: 6 additions & 0 deletions internal/integration/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ func TestTelemetryEmitterEmit(t *testing.T) {
value: summary,
attributes: labels.Set{},
},
{
name: "not a number, NAN",
metricType: metricType_COUNTER,
value: math.NaN(),
attributes: labels.Set{},
},
}

var rawMetrics []interface{}
Expand Down
46 changes: 46 additions & 0 deletions internal/integration/harvester_decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package integration

import (
"context"
"math"

"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry"
"github.com/sirupsen/logrus"
)

// harvesterDecorator is a layer on top of another harvester that filters out NaN and Infinite float values.
type harvesterDecorator struct {
innerHarvester harvester
}

func (ha harvesterDecorator) RecordMetric(m telemetry.Metric) {
switch a := m.(type) {
case telemetry.Count:
ha.processMetric(a.Value, m)
case telemetry.Summary:
ha.processMetric(a.Sum, m)
case telemetry.Gauge:
ha.processMetric(a.Value, m)
default:
logrus.Debugf("Unexpected metric in harvesterDecorator: #%v", m)
ha.innerHarvester.RecordMetric(m)
}
}

func (ha harvesterDecorator) HarvestNow(ctx context.Context) {
ha.innerHarvester.HarvestNow(ctx)
}

func (ha harvesterDecorator) processMetric(f float64, m telemetry.Metric) {
if math.IsNaN(f) {
logrus.Debugf("Ignoring NaN float value for metric: %v", m)
jorik marked this conversation as resolved.
Show resolved Hide resolved
return
}

if math.IsInf(f, 0) {
logrus.Debugf("Ignoring Infinite float value for metric: %v", m)
return
}

ha.innerHarvester.RecordMetric(m)
}