Skip to content

Commit

Permalink
Convert NaN or Infinity value metrics to string (#271)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
heidmotron authored and luckyj5 committed Apr 6, 2021
1 parent 7c85271 commit d645677
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
19 changes: 16 additions & 3 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"encoding/json"
"fmt"
"math"
"sort"
"strings"

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

}
}

Expand Down
41 changes: 41 additions & 0 deletions events/events_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d645677

Please sign in to comment.