Commit 590702c4 by Kyle Brandt Committed by GitHub

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).
parent 3fd81041
......@@ -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 {
......
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "XInf",
"type": "real"
},
{
"name": "XInfNeg",
"type": "real"
},
{
"name": "XNan",
"type": "real"
}
],
"rows": [
[
"Infinity",
"-Infinity",
"NaN"
]
]
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment