Flux query to populate variable returns no results

I am populating a dashboard variable using this Flux query, but it stopped working in the last week or so.

When the query is run in the query editor in Explore, I get single-column table with the names of devices used in the specified time range.

In Dashboards | Settings | Variables, the same query returns no values. The same thing happens if I use distinct instead of unique or include yield at the end.

Details

Configuration

  • Grafana 11.1.0
  • InfluxDB 2.7.7 OSS
  • Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-116-generic x86_64)

What I was trying to achieve

To populate a dashboard variable named device using a Flux query.

How I was trying to achieve this

  1. In Dashboards | Settings | Variables, I created a variable called device.
  2. I set variable type as Query.
  3. I applied the query in the code block below.
  4. No regex was applied. Multi-value was selected.
from(bucket: "myBucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => exists r.deviceName)
    |> group()
    |> keep(columns: ["deviceName"])
    |> unique(column: "deviceName")

What happens

Preview of values shows None.

What did I expect

I expected to see a list of devices.

Configuration

Relevant, anonymised selection of the >1.6k line JSON.
  "templating": {
    "list": [
      {
        "current": {
          "selected": true,
          "text": [
            "None"
          ],
          "value": [
            ""
          ]
        },
        "datasource": {
          "type": "influxdb",
          "uid": "01234567-89ab-cdef-0123-456789abcdef"
        },
        "definition": "from(bucket: \"MYBUCKET\")\n    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n    |> filter(fn: (r) => exists r.deviceName)\n    |> group()\n    |> keep(columns: [\"deviceName\"])\n    |> unique(column: \"deviceName\")",
        "hide": 0,
        "includeAll": false,
        "label": "device name",
        "multi": true,
        "name": "device",
        "options": [],
        "query": {
          "0": "f",
          "1": "r",
          "2": "o",
          "3": "m",
          "4": "(",
          "5": "b",
          "6": "u",
          "7": "c",
          "8": "k",
          "9": "e",
          "10": "t",
          "11": ":",
          "12": " ",
          "13": "\"",
...
          "28": "\"",
          "29": ")",
          "30": "\n",
          "31": " ",
          "32": " ",
          "33": " ",
          "34": " ",
          "35": "|",
          "36": ">",
          "37": " ",
          "38": "r",
          "39": "a",
          "40": "n",
          "41": "g",
          "42": "e",
          "43": "(",
          "44": "s",
          "45": "t",
          "46": "a",
          "47": "r",
          "48": "t",
          "49": ":",
          "50": " ",
          "51": "v",
          "52": ".",
          "53": "t",
          "54": "i",
          "55": "m",
          "56": "e",
          "57": "R",
          "58": "a",
          "59": "n",
          "60": "g",
          "61": "e",
          "62": "S",
          "63": "t",
          "64": "a",
          "65": "r",
          "66": "t",
          "67": ",",
          "68": " ",
          "69": "s",
          "70": "t",
          "71": "o",
          "72": "p",
          "73": ":",
          "74": " ",
          "75": "v",
          "76": ".",
          "77": "t",
          "78": "i",
          "79": "m",
          "80": "e",
          "81": "R",
          "82": "a",
          "83": "n",
          "84": "g",
          "85": "e",
          "86": "S",
          "87": "t",
          "88": "o",
          "89": "p",
          "90": ")",
          "91": "\n",
          "92": " ",
          "93": " ",
          "94": " ",
          "95": " ",
          "96": "|",
          "97": ">",
          "98": " ",
          "99": "f",
          "100": "i",
          "101": "l",
          "102": "t",
          "103": "e",
          "104": "r",
          "105": "(",
          "106": "f",
          "107": "n",
          "108": ":",
          "109": " ",
          "110": "(",
          "111": "r",
          "112": ")",
          "113": " ",
          "114": "=",
          "115": ">",
          "116": " ",
          "117": "e",
          "118": "x",
          "119": "i",
          "120": "s",
          "121": "t",
          "122": "s",
          "123": " ",
          "124": "r",
          "125": ".",
          "126": "d",
          "127": "e",
          "128": "v",
          "129": "i",
          "130": "c",
          "131": "e",
          "132": "N",
          "133": "a",
          "134": "m",
          "135": "e",
          "136": ")",
          "137": "\n",
          "138": " ",
          "139": " ",
          "140": " ",
          "141": " ",
          "142": "|",
          "143": ">",
          "144": " ",
          "145": "g",
          "146": "r",
          "147": "o",
          "148": "u",
          "149": "p",
          "150": "(",
          "151": ")",
          "152": "\n",
          "153": " ",
          "154": " ",
          "155": " ",
          "156": " ",
          "157": "|",
          "158": ">",
          "159": " ",
          "160": "k",
          "161": "e",
          "162": "e",
          "163": "p",
          "164": "(",
          "165": "c",
          "166": "o",
          "167": "l",
          "168": "u",
          "169": "m",
          "170": "n",
          "171": "s",
          "172": ":",
          "173": " ",
          "174": "[",
          "175": "\"",
          "176": "d",
          "177": "e",
          "178": "v",
          "179": "i",
          "180": "c",
          "181": "e",
          "182": "N",
          "183": "a",
          "184": "m",
          "185": "e",
          "186": "\"",
          "187": "]",
          "188": ")",
          "189": "\n",
          "190": " ",
          "191": " ",
          "192": " ",
          "193": " ",
          "194": "|",
          "195": ">",
          "196": " ",
          "197": "u",
          "198": "n",
          "199": "i",
          "200": "q",
          "201": "u",
          "202": "e",
          "203": "(",
          "204": "c",
          "205": "o",
          "206": "l",
          "207": "u",
          "208": "m",
          "209": "n",
          "210": ":",
          "211": " ",
          "212": "\"",
          "213": "d",
          "214": "e",
          "215": "v",
          "216": "i",
          "217": "c",
          "218": "e",
          "219": "N",
          "220": "a",
          "221": "m",
          "222": "e",
          "223": "\"",
          "224": ")",
          "query": "from(bucket: \"MYBUCKET\")\n    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n    |> filter(fn: (r) => exists r.deviceName)\n    |> group()\n    |> keep(columns: [\"deviceName\"])\n    |> unique(column: \"deviceName\")"
        },
        "refresh": 2,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query"
      }
    ]
  }

Other errors

None

Any online instructions followed

None

what does that flux query return outside of grafana when using flux web portal to query it?

Thanks for the reply!

It works as expected (through a curl request; I have the web interface disabled).

You’ll see the user is a fan of Supernatural. :grinning:

% curl -X POST -k -H 'Authorization: Token XXX' -H 'Accept: application/csv' -i 'https://XXX/influxdb/api/v2/query?orgID=XXX' --data '{"query": "from(bucket: \"MYBUCKET\")|> range(start: -1y)|> filter(fn: (r) => exists r.deviceName)|> group()|> keep(columns: [\"deviceName\"])|> unique(column: \"deviceName\")"}'
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 24 Jul 2024 14:38:57 GMT
Content-Type: text/csv; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Influxdb-Build: OSS
X-Influxdb-Version: v2.7.7
Strict-Transport-Security: max-age=63072000

,result,table,deviceName
,_result,0,Castiel
,_result,0,Rufus
,_result,0,Rowena
,_result,0,Singer
,_result,0,Fergus
,_result,0,Chris spore
,_result,0,Impala
,_result,0,Colt
1 Like

that seems to be the issue there imo, you got superfluous columns (stream). you need to bring back only one “column” → deviceName

The issue might be the keep function

keep() returns a stream of tables containing only the specified columns.

keep() function | Flux Documentation.

so the issue is not grafana but your flux query.

That doesn’t fit with my observations. I can run a similar query, like the one suggested in another community post.

This is the query:

import "influxdata/influxdb/schema"
schema.tagValues(
    bucket: "MYBUCKET",
    tag: "deviceName",
    start: v.timeRangeStart,
    stop: v.timeRangeStop
)

In Explore in Grafana, I get the expected output:

[
  {
    "schema": {
      "refId": "A",
      "meta": {
        "typeVersion": [
          0,
          0
        ],
        "executedQueryString": "import \"influxdata/influxdb/schema\"\nschema.tagValues(\n    bucket: \"MYBUCKET\",\n    tag: \"deviceName\",\n    start: 2024-01-24T16:55:12.611Z,\n    stop: 2024-07-24T15:55:12.611Z\n)"
      },
      "fields": [
        {
          "name": "_value",
          "type": "string",
          "typeInfo": {
            "frame": "string",
            "nullable": true
          },
          "labels": {},
          "config": {}
        }
      ]
    },
    "data": {
      "values": [
        [
          "Castiel",
          "Chris spore",
          "Colt",
          "Fergus",
          "Impala",
          "Rowena",
          "Rufus",
          "Singer"
        ]
      ]
    }
  }
]

But when setting the query in the variable query options, I get nothing, unless I remove the time range, in which case I get matches from the default -30d.

My interpretation is that variable queries in Grafana are not parsing the time limits correctly.

I’m trying to pin down when the original query stopped working so I could look through what change might have broken the function.

1 Like

Have you looked through this topic?

Thanks! That fits exactly. I thought I had carried out a thorough search of the Grafana and Influx forums, but there you go. Looks like a bug.