Azure: Handle real type nan/inf values in Log/Insights Analytics (#26342)

Before this, if the user were to divide by 0.0, "Infinity" would be returned in the result and the user would get an error: "unexpected type, expected json.Number but got string". Now these values are properly set as Inf values (and also made sure to handle NaN as well).

(cherry picked from commit 590702c497)
This commit is contained in:
Kyle Brandt
2020-07-16 13:04:17 +02:00
committed by Dominik Prokop
parent 3655752bae
commit 1ab8109892
3 changed files with 61 additions and 1 deletions
@@ -3,6 +3,7 @@ package azuremonitor
import (
"encoding/json"
"fmt"
"math"
"strconv"
"time"
@@ -115,7 +116,21 @@ var realConverter = data.FieldConverter{
}
jN, ok := v.(json.Number)
if !ok {
return nil, fmt.Errorf("unexpected type, expected json.Number but got %T", v)
s, sOk := v.(string)
if sOk {
switch s {
case "Infinity":
f := math.Inf(0)
return &f, nil
case "-Infinity":
f := math.Inf(-1)
return &f, nil
case "NaN":
f := math.NaN()
return &f, nil
}
}
return nil, fmt.Errorf("unexpected type, expected json.Number but got type %T for value %v", v, v)
}
f, err := jN.Float64()
if err != nil {
@@ -2,6 +2,7 @@ package azuremonitor
import (
"encoding/json"
"math"
"os"
"path/filepath"
"testing"
@@ -119,6 +120,21 @@ func TestLogTableToFrame(t *testing.T) {
return frame
},
},
{
name: "nan and infinity in real response",
testFile: "loganalytics/8-log-analytics-response-nan-inf.json",
expectedFrame: func() *data.Frame {
frame := data.NewFrame("",
data.NewField("XInf", nil, []*float64{pointer.Float64(math.Inf(0))}),
data.NewField("XInfNeg", nil, []*float64{pointer.Float64(math.Inf(-2))}),
data.NewField("XNan", nil, []*float64{pointer.Float64(math.NaN())}),
)
frame.Meta = &data.FrameMeta{
Custom: &LogAnalyticsMeta{ColumnTypes: []string{"real", "real", "real"}},
}
return frame
},
},
}
for _, tt := range tests {
@@ -0,0 +1,29 @@
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "XInf",
"type": "real"
},
{
"name": "XInfNeg",
"type": "real"
},
{
"name": "XNan",
"type": "real"
}
],
"rows": [
[
"Infinity",
"-Infinity",
"NaN"
]
]
}
]
}