From d6456775fe92c2e9f2d1cb2e888c04064b079cf3 Mon Sep 17 00:00:00 2001 From: Matthew Heidemann Date: Mon, 5 Apr 2021 16:11:48 -0600 Subject: [PATCH] Convert NaN or Infinity value metrics to string (#271) * Convert NaN or Infinity value metrics to string * Fixed test name * Convert NaN or Infinity value metrics to string * Fixed test name enable ignore app setting fix formatting --- events/events.go | 19 ++++++++++++++++--- events/events_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/events/events.go b/events/events.go index 9da19f38..e6ded3b8 100644 --- a/events/events.go +++ b/events/events.go @@ -3,6 +3,7 @@ package events import ( "encoding/json" "fmt" + "math" "sort" "strings" @@ -121,11 +122,21 @@ func LogMessage(msg *events.Envelope) *Event { func ValueMetric(msg *events.Envelope) *Event { valMetric := msg.GetValueMetric() + value := valMetric.GetValue() fields := logrus.Fields{ "name": valMetric.GetName(), "unit": valMetric.GetUnit(), - "value": valMetric.GetValue(), + "value": value, + } + + // Convert special values + if math.IsNaN(value) { + fields["value"] = "NaN" + } else if math.IsInf(value, 1) { + fields["value"] = "Infinity" + } else if math.IsInf(value, -1) { + fields["value"] = "-Infinity" } return &Event{ @@ -226,8 +237,10 @@ func (e *Event) AnnotateWithAppData(appCache cache.Cache, config *Config) { e.Fields["info_splunk_index"] = app_env["SPLUNK_INDEX"] } - //removing cf_ignored_app as per INGEST-17639 - e.Fields["cf_ignored_app"] = cf_ignored_app + if cf_ignored_app != false { + e.Fields["cf_ignored_app"] = cf_ignored_app + } + } } diff --git a/events/events_test.go b/events/events_test.go index ea7379b8..d73a3245 100644 --- a/events/events_test.go +++ b/events/events_test.go @@ -1,6 +1,8 @@ package events_test import ( + "math" + fevents "github.com/cloudfoundry-community/splunk-firehose-nozzle/events" "github.com/cloudfoundry-community/splunk-firehose-nozzle/testing" . "github.com/cloudfoundry/sonde-go/events" @@ -88,6 +90,45 @@ var _ = Describe("Events", func() { Expect(evt.Fields["unit"]).To(Equal(unit)) }) + It("ValueMetric NaN", func() { + msg = NewValueMetric() + nan := math.NaN() + msg.ValueMetric.Value = &nan + evt := fevents.ValueMetric(msg) + Expect(evt).ToNot(BeNil()) + Expect(evt.Fields).ToNot(BeNil()) + Expect(evt.Msg).To(Equal("")) + Expect(evt.Fields["name"]).To(Equal(name)) + Expect(evt.Fields["value"]).To(Equal("NaN")) + Expect(evt.Fields["unit"]).To(Equal(unit)) + }) + + It("ValueMetric +Infinity", func() { + msg = NewValueMetric() + inf := math.Inf(1) + msg.ValueMetric.Value = &inf + evt := fevents.ValueMetric(msg) + Expect(evt).ToNot(BeNil()) + Expect(evt.Fields).ToNot(BeNil()) + Expect(evt.Msg).To(Equal("")) + Expect(evt.Fields["name"]).To(Equal(name)) + Expect(evt.Fields["value"]).To(Equal("Infinity")) + Expect(evt.Fields["unit"]).To(Equal(unit)) + }) + + It("ValueMetric -Infinity", func() { + msg = NewValueMetric() + inf := math.Inf(-1) + msg.ValueMetric.Value = &inf + evt := fevents.ValueMetric(msg) + Expect(evt).ToNot(BeNil()) + Expect(evt.Fields).ToNot(BeNil()) + Expect(evt.Msg).To(Equal("")) + Expect(evt.Fields["name"]).To(Equal(name)) + Expect(evt.Fields["value"]).To(Equal("-Infinity")) + Expect(evt.Fields["unit"]).To(Equal(unit)) + }) + It("CounterEvent", func() { msg = NewCounterEvent() evt := fevents.CounterEvent(msg)