fix(outputs.warp10): Support Infinity/-Infinity/NaN values (#13126)
This commit is contained in:
parent
e7f2669bb8
commit
1d3afd469f
|
|
@ -198,7 +198,38 @@ func boolToString(inputBool bool) string {
|
||||||
return strconv.FormatBool(inputBool)
|
return strconv.FormatBool(inputBool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Warp10 supports Infinity/-Infinity/NaN
|
||||||
|
<'
|
||||||
|
// class{label=value} 42.0
|
||||||
|
0// class-1{label=value}{attribute=value} 42
|
||||||
|
=1// Infinity
|
||||||
|
'>
|
||||||
|
PARSE
|
||||||
|
|
||||||
|
<'
|
||||||
|
// class{label=value} 42.0
|
||||||
|
0// class-1{label=value}{attribute=value} 42
|
||||||
|
=1// -Infinity
|
||||||
|
'>
|
||||||
|
PARSE
|
||||||
|
|
||||||
|
<'
|
||||||
|
// class{label=value} 42.0
|
||||||
|
0// class-1{label=value}{attribute=value} 42
|
||||||
|
=1// NaN
|
||||||
|
'>
|
||||||
|
PARSE
|
||||||
|
*/
|
||||||
func floatToString(inputNum float64) string {
|
func floatToString(inputNum float64) string {
|
||||||
|
switch {
|
||||||
|
case math.IsNaN(inputNum):
|
||||||
|
return "NaN"
|
||||||
|
case math.IsInf(inputNum, -1):
|
||||||
|
return "-Infinity"
|
||||||
|
case math.IsInf(inputNum, 1):
|
||||||
|
return "Infinity"
|
||||||
|
}
|
||||||
return strconv.FormatFloat(inputNum, 'f', 6, 64)
|
return strconv.FormatFloat(inputNum, 'f', 6, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package warp10
|
package warp10
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
|
|
@ -24,6 +25,39 @@ func TestWriteWarp10(t *testing.T) {
|
||||||
require.Exactly(t, "1257894000000000// unit.testtest1.value{source=telegraf,tag1=value1} 1.000000\n", payload)
|
require.Exactly(t, "1257894000000000// unit.testtest1.value{source=telegraf,tag1=value1} 1.000000\n", payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteWarp10ValueNaN(t *testing.T) {
|
||||||
|
w := Warp10{
|
||||||
|
Prefix: "unit.test",
|
||||||
|
WarpURL: "http://localhost:8090",
|
||||||
|
Token: config.NewSecret([]byte("WRITE")),
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := w.GenWarp10Payload(testutil.MockMetricsWithValue(math.NaN()))
|
||||||
|
require.Exactly(t, "1257894000000000// unit.testtest1.value{source=telegraf,tag1=value1} NaN\n", payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteWarp10ValueInfinity(t *testing.T) {
|
||||||
|
w := Warp10{
|
||||||
|
Prefix: "unit.test",
|
||||||
|
WarpURL: "http://localhost:8090",
|
||||||
|
Token: config.NewSecret([]byte("WRITE")),
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := w.GenWarp10Payload(testutil.MockMetricsWithValue(math.Inf(1)))
|
||||||
|
require.Exactly(t, "1257894000000000// unit.testtest1.value{source=telegraf,tag1=value1} Infinity\n", payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteWarp10ValueMinusInfinity(t *testing.T) {
|
||||||
|
w := Warp10{
|
||||||
|
Prefix: "unit.test",
|
||||||
|
WarpURL: "http://localhost:8090",
|
||||||
|
Token: config.NewSecret([]byte("WRITE")),
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := w.GenWarp10Payload(testutil.MockMetricsWithValue(math.Inf(-1)))
|
||||||
|
require.Exactly(t, "1257894000000000// unit.testtest1.value{source=telegraf,tag1=value1} -Infinity\n", payload)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteWarp10EncodedTags(t *testing.T) {
|
func TestWriteWarp10EncodedTags(t *testing.T) {
|
||||||
w := Warp10{
|
w := Warp10{
|
||||||
Prefix: "unit.test",
|
Prefix: "unit.test",
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,13 @@ func MockMetrics() []telegraf.Metric {
|
||||||
return metrics
|
return metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MockMetricsWithValue(value float64) []telegraf.Metric {
|
||||||
|
metrics := make([]telegraf.Metric, 0)
|
||||||
|
// Create a new point batch
|
||||||
|
metrics = append(metrics, TestMetric(value))
|
||||||
|
return metrics
|
||||||
|
}
|
||||||
|
|
||||||
// TestMetric Returns a simple test point:
|
// TestMetric Returns a simple test point:
|
||||||
//
|
//
|
||||||
// measurement -> "test1" or name
|
// measurement -> "test1" or name
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue