@@ -51,21 +51,31 @@ func (f *flush) addMetricf(metricType metricType, value float64, source gostatsd
5151}
5252
5353// addMetric adds a metric to the series.
54+ // If the value is non-numeric (in the case of NaN and Inf values), the value is coerced into a numeric value.
5455func (f * flush ) addMetric (metricType metricType , value float64 , source gostatsd.Source , tags gostatsd.Tags , name string ) {
55- if math .IsInf (value , 1 ) || math .IsInf (value , - 1 ) || math .IsNaN (value ) {
56- // The value can not be represented within the JSON payload so it is to be discarded.
57- return
58- }
5956 f .ts .Series = append (f .ts .Series , metric {
6057 Host : string (source ),
6158 Interval : f .flushIntervalSec ,
6259 Metric : name ,
63- Points : [1 ]point {{f .timestamp , value }},
60+ Points : [1 ]point {{f .timestamp , coerceToNumeric ( value ) }},
6461 Tags : tags ,
6562 Type : metricType ,
6663 })
6764}
6865
66+ // coerceToNumeric will convert non-numeric NaN and Inf values to a numeric value.
67+ // If v is a numeric, the same value is returned.
68+ func coerceToNumeric (v float64 ) float64 {
69+ if math .IsNaN (v ) {
70+ return - 1
71+ } else if math .IsInf (v , 1 ) {
72+ return math .MaxFloat64
73+ } else if math .IsInf (v , - 1 ) {
74+ return - math .MaxFloat64
75+ }
76+ return v
77+ }
78+
6979func (f * flush ) maybeFlush () {
7080 if uint (len (f .ts .Series ))+ 20 >= f .metricsPerBatch { // flush before it reaches max size and grows the slice
7181 f .cb (f .ts )
0 commit comments