How To: Building Flux Queries in Chronograf

Originally published at: How To: Building Flux Queries in Chronograf | InfluxData

As you all may or may not know (and if you don’t, you haven’t been reading my posts!), I’ve built an embedded IoT gateway proof of concept device that runs (of course) InfluxDB—really the entire TICK Stack—and collects data from connected Bluetooth, WiFi and LoRa sensors.

The latest releases of InfluxDB and Chronograf are now available, and with each of these, comes the technical preview of Flux, the new query language for working with time series data. I thought it might be instructive to see how difficult it was going to be to convert the queries that I use to build my dashboard from InfluxQL to Flux and generally explore the language itself. So here goes.

The Dashboard

That’s all the sensor data—well, the radiation sensor is off-line right now—and how it looks on-screen. Each of those dashboard elements is created and updated via an InfluxQL query via Chronograf. So let’s look at some of the queries and what they look like in the Chronograf Dashboard Builder today. We’ll start with that CO2 reading at the upper left:

To be perfectly clear: I did not have to write that query. I simply started exploring the database, clicking on measurements, tags and values, and adjusting the settings until I got the visualization I was looking for. Easy enough.

So let’s try building that same query in using InfluxDB 1.7, Chronograf 1.7 and using the new Flux Builder along with the Schema Explorer:

Wow! That was easy!

It’s very, very similar. In the Flux example, we start with where we’re going to get the data—the ‘from bucket’—and then give it the time interval, and finally filter by the measurement and field.

Next, we add the window(every: autoInterval) to the query. Flux’s window() function groups records based on a time value. Using the every: parameter along with autoInterval allows Chronograf to dynamically determine the size of the window based on the screen real-estate that is available for visualizing the results.

And here’s what the Flux query looks like in the end:

from ( bucket: "telegraf/autogen" )
   |> range(start: dashboardTime)
   |> filter (fn: (r) => r._measurement == "k30_reader" and (r_field == "co2" )) 
   |> window (every: autoInterval)

Flux takes input tables, performs a function, and produces one or more output tables. So far, we’ve defined an input table. Now, we add the mean() aggregate function to get the mean CO2 reading within each window. Each window is returned as an output table. By adding the group() function with the none:true parameter, we remove the window partitions and combine the aggregates into a single output table.

The resulting query looks like this:

from(bucket: “telegraf/autogen”)
  |> range(start: dashboardTime)
  |> filter(fn:(r =>r._measurement == “k30_reader” and (r._field == “co2”))
  |> window(every: autoInterval)
  |> mean()
  |> group(none:true)

The original query used the template variable :dashboardTime: to allow the user to adjust the time range of the query through the user interface controls provided within Chronograf without forcing them to re-adjust the query itself. This same functionality is supported within Flux too. The ScriptWizard drops this into the |> range filter by default.

Now we can visually compare the results by flipping back and forth between the InfluxQL and the Flux version of these queries to see that they indeed yield the same results!

What about FILL?

In the technical preview of Flux, the fill functionality hasn’t been implemented. We understand this is an important part of how to visualize sparse data and rest assured we are working to implement this in a future update.

1 Like

Thanks @davidgs for a really helpful post! I have a somewhat basic question: I have a host with Chronograf 1.7.3 connected to InfluxDB 1.7.1. I’ve enabled Flux in the Influx config (per Flux 0.7 Technical Preview | InfluxData) and restarted the service, etc, yet the “Flux” button under “Explore” in Chronograf is still grayed out, with a message “The current source does not support Flux”. Any ideas what I can do to troubleshoot?

Some data points:

  • Chronograf can successfully query the server using the normal QL.
  • The Influx server is running with SSL and authentication enabled.
  • I don’t see any seemingly relevant log messages in either the Influx on the Chronograf logs (nothing mentioning Flux)
  • It’s not clear to me how to test if Flux is actually running.
    • Trying curl -X GET https://servername:8086/v2 (as mentioned here: Can't get flux to work - #6 by tim.hall) returns 404, which is probably not a good sign.
    • Running the influx CLI with -type flux does get me a Flux prompt, but I don’t know if that’s evidence of a successful connection (it wasn’t clear to me how to construct a query in the CLI)

I’d appreciate any pointers! We’ve been using Influx for a while and are really excited about the potential of Flux to unlock some serious capabilities, but it’s clearly baby steps for now.

Thanks in advance!

Did you manage to get this working? I have the same issue?

Yes, it worked in the end. But I’m going to give you a really frustrating response, which is that I don’t know what it is that was needed to make it work. I fiddled with some settings (and also upgraded) and at some point, after not having paid attention for a while, I noticed it’s working. Hope yours works also!

I have the same (or at least very similar) issues with

  • influxdb-1.7.4-1.x86_64
  • chronograf-1.7.8-1.x86_64

upgraded from influxdb-1.5.2-1 and chronograf-1.4.4.1-1

so if anyone got ideas, I will appreciate them.

And, yes, I have:
[http] flux-enabled = true https-enabled = true

influx -host $(hostname) -ssl -username admin -password XYZZY -execute 'SELECT usage_system FROM telegraf.autogen.cpu WHERE time > now() - 5m'
gives output
while
influx -host $(hostname) -ssl -username admin -password XYZZY -type flux -execute 'from(bucket: "telegraf") |> range(start: -5m) |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system")'
gives
client error: 401 Unauthorized

same in REPL mode:

Connected to https://…:8086 version 1.7.4
InfluxDB shell version: 1.7.4
Enter a Flux query
> from(bucket: “telegraf/autogen”)
Error: client error: 401 Unauthorized
>

I assume that any user that can run influxql-query also can run (the corresponding) flux-query?

The 401 Unauthorized error suggests the credentials are incorrect. Just want to make sure you are certain you’re using the same credentials in both commands.