InfluxDB: Fix handling flux response with no time and value column (#72833)

* Upgrade the influxdb-client-go version

* Handle flux response with no time and value column
This commit is contained in:
ismail simsek 2023-08-08 16:00:52 +03:00 committed by GitHub
parent 533fae4c60
commit 1ac9e7eaab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 354 additions and 14 deletions

2
go.mod
View File

@ -73,7 +73,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0 // @grafana/plugins-platform-backend
github.com/hashicorp/go-plugin v1.4.9 // @grafana/plugins-platform-backend
github.com/hashicorp/go-version v1.6.0 // @grafana/backend-platform
github.com/influxdata/influxdb-client-go/v2 v2.6.0 // @grafana/observability-metrics
github.com/influxdata/influxdb-client-go/v2 v2.12.3 // @grafana/observability-metrics
github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 // @grafana/grafana-app-platform-squad
github.com/jmespath/go-jmespath v0.4.0 // @grafana/backend-platform
github.com/json-iterator/go v1.1.12 // @grafana/backend-platform

2
go.sum
View File

@ -1946,6 +1946,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/influxdata/influxdb v1.7.6/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY=
github.com/influxdata/influxdb-client-go/v2 v2.6.0 h1:bIOaGTgvvv1Na2hG+nIvqyv7PK2UiU2WrJN1ck1ykyM=
github.com/influxdata/influxdb-client-go/v2 v2.6.0/go.mod h1:Y/0W1+TZir7ypoQZYd2IrnVOKB3Tq6oegAQeSVN/+EU=
github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0IpXeMSkY/uJa/O/vC4=
github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=

View File

@ -190,23 +190,11 @@ func (fb *frameBuilder) Init(metadata *query.FluxTableMetadata) error {
if col.IsGroup() {
fb.labels = append(fb.labels, col.Name())
} else {
dataType := col.DataType()
name := col.Name()
isTimestamp := isTimestampType(dataType)
converter, err := getConverter(dataType)
info, isTimestamp, err := getColumnInfo(col)
if err != nil {
return err
}
info := &columnInfo{
name: name,
converter: converter,
shouldGetLabels: true, // we default to get-labels
isTheSimpleValue: false,
isTheSimpleTime: false,
}
if isTimestamp {
timestampCols = append(timestampCols, info)
} else {
@ -243,6 +231,18 @@ func (fb *frameBuilder) Init(metadata *query.FluxTableMetadata) error {
fb.columns = append(fb.columns, *colInfo)
}
// if there is no columns in frame builder we use all the dataColumns
// this can happen with the queries which includes no time and value columns
if len(fb.columns) == 0 {
for _, col := range dataColumns {
info, _, err := getColumnInfo(col)
if err != nil {
return err
}
fb.columns = append(fb.columns, *info)
}
}
return nil
}
@ -254,6 +254,25 @@ func (e maxPointsExceededError) Error() string {
return fmt.Sprintf("max data points limit exceeded (count is %d)", e.Count)
}
func getColumnInfo(col *query.FluxColumn) (info *columnInfo, isTimestamp bool, err error) {
dataType := col.DataType()
isTimestamp = isTimestampType(dataType)
name := col.Name()
converter, err := getConverter(dataType)
if err != nil {
return nil, false, err
}
return &columnInfo{
name: name,
converter: converter,
shouldGetLabels: true, // we default to get-labels
isTheSimpleValue: false,
isTheSimpleTime: false,
}, isTimestamp, nil
}
func getTableID(record *query.FluxRecord, groupColumns []string) []interface{} {
result := make([]interface{}, len(groupColumns))

View File

@ -305,3 +305,13 @@ func TestTimestampFirst(t *testing.T) {
require.Equal(t, "Time", dr.Frames[0].Fields[0].Name)
require.Equal(t, "Value", dr.Frames[0].Fields[1].Name)
}
func TestWithoutTimeColumn(t *testing.T) {
dr := verifyGoldenResponse(t, "without-time-column")
require.Len(t, dr.Frames, 5)
// we make sure the timestamp-column is the first column
// in the dataframe, even if it was not the first column
// in the csv.
require.Equal(t, "cpu", dr.Frames[0].Fields[0].Name)
require.Equal(t, "host", dr.Frames[0].Fields[1].Name)
}

View File

@ -0,0 +1,14 @@
#datatype,string,long,string,string
#group,false,false,true,true
#default,last,,,
,result,table,cpu,host
,,0,cpu-total,cc59eb40ad0f
,,0,cpu-total,cc59eb40ad0f
,,1,cpu0,cc59eb40ad0f
,,1,cpu0,cc59eb40ad0f
,,2,cpu1,cc59eb40ad0f
,,3,cpu2,cc59eb40ad0f
,,3,cpu2,cc59eb40ad0f
,,4,cpu3,cc59eb40ad0f
,,4,cpu3,cc59eb40ad0f
1 #datatype string long string string
2 #group false false true true
3 #default last
4 result table cpu host
5 0 cpu-total cc59eb40ad0f
6 0 cpu-total cc59eb40ad0f
7 1 cpu0 cc59eb40ad0f
8 1 cpu0 cc59eb40ad0f
9 2 cpu1 cc59eb40ad0f
10 3 cpu2 cc59eb40ad0f
11 3 cpu2 cc59eb40ad0f
12 4 cpu3 cc59eb40ad0f
13 4 cpu3 cc59eb40ad0f

View File

@ -0,0 +1,295 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "typeVersion": [
// 0,
// 0
// ]
// }
// Name:
// Dimensions: 2 Fields by 2 Rows
// +------------------------------------------+------------------------------------------+
// | Name: cpu | Name: host |
// | Labels: cpu=cpu-total, host=cc59eb40ad0f | Labels: cpu=cpu-total, host=cc59eb40ad0f |
// | Type: []*string | Type: []*string |
// +------------------------------------------+------------------------------------------+
// | cpu-total | cc59eb40ad0f |
// | cpu-total | cc59eb40ad0f |
// +------------------------------------------+------------------------------------------+
//
//
//
// Frame[1]
// Name:
// Dimensions: 2 Fields by 2 Rows
// +-------------------------------------+-------------------------------------+
// | Name: cpu | Name: host |
// | Labels: cpu=cpu0, host=cc59eb40ad0f | Labels: cpu=cpu0, host=cc59eb40ad0f |
// | Type: []*string | Type: []*string |
// +-------------------------------------+-------------------------------------+
// | cpu0 | cc59eb40ad0f |
// | cpu0 | cc59eb40ad0f |
// +-------------------------------------+-------------------------------------+
//
//
//
// Frame[2]
// Name:
// Dimensions: 2 Fields by 1 Rows
// +-------------------------------------+-------------------------------------+
// | Name: cpu | Name: host |
// | Labels: cpu=cpu1, host=cc59eb40ad0f | Labels: cpu=cpu1, host=cc59eb40ad0f |
// | Type: []*string | Type: []*string |
// +-------------------------------------+-------------------------------------+
// | cpu1 | cc59eb40ad0f |
// +-------------------------------------+-------------------------------------+
//
//
//
// Frame[3]
// Name:
// Dimensions: 2 Fields by 2 Rows
// +-------------------------------------+-------------------------------------+
// | Name: cpu | Name: host |
// | Labels: cpu=cpu2, host=cc59eb40ad0f | Labels: cpu=cpu2, host=cc59eb40ad0f |
// | Type: []*string | Type: []*string |
// +-------------------------------------+-------------------------------------+
// | cpu2 | cc59eb40ad0f |
// | cpu2 | cc59eb40ad0f |
// +-------------------------------------+-------------------------------------+
//
//
//
// Frame[4]
// Name:
// Dimensions: 2 Fields by 2 Rows
// +-------------------------------------+-------------------------------------+
// | Name: cpu | Name: host |
// | Labels: cpu=cpu3, host=cc59eb40ad0f | Labels: cpu=cpu3, host=cc59eb40ad0f |
// | Type: []*string | Type: []*string |
// +-------------------------------------+-------------------------------------+
// | cpu3 | cc59eb40ad0f |
// | cpu3 | cc59eb40ad0f |
// +-------------------------------------+-------------------------------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
{
"status": 200,
"frames": [
{
"schema": {
"meta": {
"typeVersion": [
0,
0
]
},
"fields": [
{
"name": "cpu",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu-total",
"host": "cc59eb40ad0f"
}
},
{
"name": "host",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu-total",
"host": "cc59eb40ad0f"
}
}
]
},
"data": {
"values": [
[
"cpu-total",
"cpu-total"
],
[
"cc59eb40ad0f",
"cc59eb40ad0f"
]
]
}
},
{
"schema": {
"fields": [
{
"name": "cpu",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu0",
"host": "cc59eb40ad0f"
}
},
{
"name": "host",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu0",
"host": "cc59eb40ad0f"
}
}
]
},
"data": {
"values": [
[
"cpu0",
"cpu0"
],
[
"cc59eb40ad0f",
"cc59eb40ad0f"
]
]
}
},
{
"schema": {
"fields": [
{
"name": "cpu",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu1",
"host": "cc59eb40ad0f"
}
},
{
"name": "host",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu1",
"host": "cc59eb40ad0f"
}
}
]
},
"data": {
"values": [
[
"cpu1"
],
[
"cc59eb40ad0f"
]
]
}
},
{
"schema": {
"fields": [
{
"name": "cpu",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu2",
"host": "cc59eb40ad0f"
}
},
{
"name": "host",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu2",
"host": "cc59eb40ad0f"
}
}
]
},
"data": {
"values": [
[
"cpu2",
"cpu2"
],
[
"cc59eb40ad0f",
"cc59eb40ad0f"
]
]
}
},
{
"schema": {
"fields": [
{
"name": "cpu",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu3",
"host": "cc59eb40ad0f"
}
},
{
"name": "host",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
},
"labels": {
"cpu": "cpu3",
"host": "cc59eb40ad0f"
}
}
]
},
"data": {
"values": [
[
"cpu3",
"cpu3"
],
[
"cc59eb40ad0f",
"cc59eb40ad0f"
]
]
}
}
]
}