From 717a936cba244a53a50073dcfd87b3f5f75a391b Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:17:44 +0100 Subject: [PATCH] [v10.2.x] InfluxDB: Fix aliasing with $measurement or $m on backend mode (#77383) InfluxDB: Fix aliasing with $measurement or $m on backend mode (#76917) * better interpolation $measurement aliasing * unit tests (cherry picked from commit 5eb0b2bedb20fb66726c7520a14651dfe4a48267) Co-authored-by: ismail simsek --- pkg/tsdb/influxdb/influxql/response_parser.go | 2 +- .../influxdb/influxql/response_parser_test.go | 46 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/pkg/tsdb/influxdb/influxql/response_parser.go b/pkg/tsdb/influxdb/influxql/response_parser.go index 07471d11bb0..22972d209ca 100644 --- a/pkg/tsdb/influxdb/influxql/response_parser.go +++ b/pkg/tsdb/influxdb/influxql/response_parser.go @@ -227,7 +227,7 @@ func formatFrameName(row models.Row, column string, query models.Query, frameNam aliasFormat = strings.Replace(aliasFormat, "$", "", 1) if aliasFormat == "m" || aliasFormat == "measurement" { - return []byte(query.Measurement) + return []byte(row.Name) } if aliasFormat == "col" { return []byte(column) diff --git a/pkg/tsdb/influxdb/influxql/response_parser_test.go b/pkg/tsdb/influxdb/influxql/response_parser_test.go index cf2da76ed36..b089542f8eb 100644 --- a/pkg/tsdb/influxdb/influxql/response_parser_test.go +++ b/pkg/tsdb/influxdb/influxql/response_parser_test.go @@ -323,6 +323,48 @@ func TestInfluxdbResponseParser(t *testing.T) { } }) + t.Run("Influxdb response parser with $measurement alias when multiple measurement in response", func(t *testing.T) { + response := ` + { + "results": [ + { + "series": [ + { + "name": "cpu.upc", + "columns": ["time","mean"], + "tags": { + "datacenter": "America", + "dc.region.name": "Northeast", + "cluster-name": "Cluster" + }, + "values": [ + [111,222] + ] + }, + { + "name": "logins.count", + "columns": ["time","mean"], + "tags": { + "datacenter": "America", + "dc.region.name": "Northeast", + "cluster-name": "Cluster" + }, + "values": [ + [111,222] + ] + } + ] + } + ] + } + ` + + query := models.Query{Alias: "alias $measurement"} + result := ResponseParse(prepare(response), 200, generateQuery(query)) + assert.Equal(t, "alias cpu.upc", result.Frames[0].Name) + assert.Equal(t, "alias logins.count", result.Frames[1].Name) + }) + t.Run("Influxdb response parser with alias", func(t *testing.T) { response := ` { @@ -373,7 +415,7 @@ func TestInfluxdbResponseParser(t *testing.T) { query = models.Query{Alias: "alias $m $measurement", Measurement: "10m"} result = ResponseParse(prepare(response), 200, generateQuery(query)) - name := "alias 10m 10m" + name := "alias cpu.upc cpu.upc" testFrame.Name = name testFrame.Fields[1].Config.DisplayNameFromDS = name if diff := cmp.Diff(testFrame, result.Frames[0], data.FrameTestCompareOptions()...); diff != "" { @@ -481,7 +523,7 @@ func TestInfluxdbResponseParser(t *testing.T) { query = models.Query{Alias: "alias [[m]] [[measurement]]", Measurement: "10m"} result = ResponseParse(prepare(response), 200, generateQuery(query)) - name = "alias 10m 10m" + name = "alias cpu.upc cpu.upc" testFrame.Name = name testFrame.Fields[1].Config.DisplayNameFromDS = name if diff := cmp.Diff(testFrame, result.Frames[0], data.FrameTestCompareOptions()...); diff != "" {