Skip to content

Commit 96e6524

Browse files
authored
AzureMonitor: Support decimal (as float64) type in analytics/logs (grafana#28480)
loss of precision but will make the response work instead of erroring follows work already done in the ADX plugin fixes grafana#28278
1 parent 4d2b20f commit 96e6524

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

pkg/tsdb/azuremonitor/azure-log-analytics-table-frame.go

+39
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var converterMap = map[string]data.FieldConverter{
6969
"long": longConverter,
7070
"real": realConverter,
7171
"bool": boolConverter,
72+
"decimal": decimalConverter,
7273
}
7374

7475
var stringConverter = data.FieldConverter{
@@ -194,3 +195,41 @@ var longConverter = data.FieldConverter{
194195
return &out, err
195196
},
196197
}
198+
199+
// decimalConverter converts the Kusto 128-bit type number to
200+
// a float64. We do not have 128 bit numbers in our dataframe
201+
// model yet (and even if we did, not sure how javascript would handle them).
202+
// In the future, we may want to revisit storing this will proper precision,
203+
// but for now it solves the case of people getting an error response.
204+
// If we were to keep it a string, it would not work correctly with calls
205+
// to functions like sdk's data.LongToWide.
206+
var decimalConverter = data.FieldConverter{
207+
OutputFieldType: data.FieldTypeNullableFloat64,
208+
Converter: func(v interface{}) (interface{}, error) {
209+
var af *float64
210+
if v == nil {
211+
return af, nil
212+
}
213+
214+
jS, sOk := v.(string)
215+
if sOk {
216+
out, err := strconv.ParseFloat(jS, 64)
217+
if err != nil {
218+
return nil, err
219+
}
220+
return &out, err
221+
}
222+
223+
// As far as I can tell this always comes in a string, but this is in the
224+
// ADX code, so leaving this in case values do sometimes become a number somehow.
225+
jN, nOk := v.(json.Number)
226+
if !nOk {
227+
return nil, fmt.Errorf("unexpected type, expected json.Number or string but got type %T with a value of %v", v, v)
228+
}
229+
out, err := jN.Float64() // Float64 calls strconv.ParseFloat64
230+
if err != nil {
231+
return nil, err
232+
}
233+
return &out, nil
234+
},
235+
}

pkg/tsdb/azuremonitor/azure-log-analytics-table-frame_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ func TestLogTableToFrame(t *testing.T) {
112112
data.NewField("XLong", nil, []*int64{pointer.Int64(9223372036854775807)}),
113113
data.NewField("XReal", nil, []*float64{pointer.Float64(1.797693134862315708145274237317043567981e+308)}),
114114
data.NewField("XTimeSpan", nil, []*string{pointer.String("00:00:00.0000001")}),
115+
data.NewField("XDecimal", nil, []*float64{pointer.Float64(79228162514264337593543950335)}),
115116
)
116117
frame.Meta = &data.FrameMeta{
117118
Custom: &LogAnalyticsMeta{ColumnTypes: []string{"bool", "string", "datetime",
118-
"dynamic", "guid", "int", "long", "real", "timespan"}},
119+
"dynamic", "guid", "int", "long", "real", "timespan", "decimal"}},
119120
}
120121
return frame
121122
},

pkg/tsdb/azuremonitor/testdata/loganalytics/7-log-analytics-all-types-table.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
{
3939
"name": "XTimeSpan",
4040
"type": "timespan"
41+
},
42+
{
43+
"name":"XDecimal",
44+
"type":"decimal"
4145
}
4246
],
4347
"rows": [
@@ -50,10 +54,11 @@
5054
2147483647,
5155
9223372036854775807,
5256
1.7976931348623157e+308,
53-
"00:00:00.0000001"
57+
"00:00:00.0000001",
58+
"79228162514264337593543950335"
5459
]
5560
]
5661
}
5762
]
5863
}
59-
64+

0 commit comments

Comments
 (0)