Using Flux from Grafana

After Getting started with Flux, you might be asking how to finally use it from Grafana?

Grafana datasource plugin for Flux (InfluxDB)

Introduction

Use this datasource if you want to use Flux to query your InfluxDB. Feel free to run this datasource side-by-side with the non-Flux datasource. If you point both datasources to the same InfluxDB instance, you can switch query mode by switching the datasources.

State of the onion

While we already assumed to require the »Grafana datasource plugin for Flux (InfluxDB)«, there are currently two versions around:

We should use the most recent one. However, there is another commit Updated db: to bucket: to support new changes in Flux · grafana/influxdb-flux-datasource@ac49cf7 · GitHub sitting in the branch “bucket-updates” which most notably includes the switchover regarding addressing databases from within a Flux expression. Flux switched from

  • from(db: "${database}")

to

  • from(bucket: "${database}")

So, databases will be called buckets now. That’s ok.

Setup

Prerequisites

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt update
apt install yarn

https://linuxize.com/post/how-to-install-yarn-on-debian-9/

Build

git clone https://github.com/daq-tools/grafana-influxdb-flux-datasource /opt/grafana-influxdb-flux-datasource

cd /opt/grafana-influxdb-flux-datasource
yarn install
yarn dev

Activate

Note: Please make sure you uninstalled the GA release from the Grafana Plugin Repository by using e.g.

grafana-cli plugins uninstall grafana-influxdb-flux-datasource

Then, link the DEV release into the Grafana plugin directory:

ln -s /opt/grafana-influxdb-flux-datasource /var/lib/grafana/plugins/
systemctl restart grafana-server

Configure

Datasource

Panel metrics

Note

Please note there’s no need to configure a default database (so we used "null" there), as the database (bucket) is addressed from within the Flux query expression anyway. Thus, there will be probably no need to configure a different Grafana data source for each InfluxDB database - every query can be run through the canonical one, right?

Important note about Flux (again)

Flux requires a time range when querying time series data. “Unbounded” queries are very resource-intensive and as a protective measure, Flux will not query the database without a specified range.

https://docs.influxdata.com/flux/v0.7/introduction/getting-started/query-influxdb/#2-specify-a-time-range

When sending an “unbounded” query to InfluxDB through Flux, like from(bucket:"hiveeyes_open_hive_clemens") without the range($range) part, it will respond appropriately:

Failed to create physical plan: result 'from0' is unbounded. 
Add a 'range' call to bound the query,

Data sources

Data sources for submitting Flux query expressions through Grafana have been added to both InfluxDB instances:

image

Example dashboard

[amo] Open Hive Clemens with Flux is an example dashboard which already uses Flux to query beekeeping data.

1 Like

This could be of interest for everyone playing with Flux in Grafana.

Wrong variable expansion format #13

https://github.com/grafana/influxdb-flux-datasource/issues/13

Only _value column gets formatted #14

https://github.com/grafana/influxdb-flux-datasource/issues/14

After asking upstream, promptly there’s a new release of the Grafana Flux plugin:

https://github.com/grafana/influxdb-flux-datasource/commits/5.2.7

Thanks a bunch, David!


Update: Not quite there yet, see Getting Network Error: Not Found(404) · Issue #22 · grafana/influxdb-flux-datasource · GitHub.


Update: After some minor adjustments, installing the version 5.2.9 release bundle works like a charm.

https://github.com/grafana/influxdb-flux-datasource/commits/5.2.9

Thanks again, David!

1 Like

Hi there,

as I’m sitting on the notification feed for this repository, I would like to share this piece with you:

https://github.com/grafana/influxdb-flux-datasource/pull/26

Do some of the Flux experts from us have any opinion about that? @wtf, maybe?

Background

As I’ve already been attending there [1,2,3] to gently push things forward while helping some others who have been running into similar issues we had when setting this up on our systems, there’s a slight chance this could also be interesting for us.

However, as I’m personally not into writing Flux queries yet but heard from @wtf that this is a detail he is currently into, I would need your statement about that. Just let me know if you would like to play with the amendments by Vadim, will be happy to install a fresh build from master as I did the other day.

Cheers,
Andreas.


References

[1] https://github.com/grafana/influxdb-flux-datasource/pull/16
[2] adding datasource getting Network Error: Not Found(404) · Issue #21 · grafana/influxdb-flux-datasource · GitHub
[3] Getting Network Error: Not Found(404) · Issue #22 · grafana/influxdb-flux-datasource · GitHub