Skip to content

Commit

Permalink
InfluxDB: Fix aliasing with $measurement or $m on backend mode (#76917)
Browse files Browse the repository at this point in the history
* better interpolation $measurement aliasing

* unit tests

(cherry picked from commit 5eb0b2b)
  • Loading branch information
itsmylife authored and grafana-delivery-bot[bot] committed Oct 30, 2023
1 parent cd62af5 commit 47f036a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/tsdb/influxdb/influxql/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 44 additions & 2 deletions pkg/tsdb/influxdb/influxql/response_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `
{
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 47f036a

Please sign in to comment.