InfluxDB query editor
This topic explains querying specific to the InfluxDB data source. For general documentation on querying data sources in Grafana, see Query and transform data.
Choose a query editing mode
The InfluxDB data source’s query editor has two modes depending on your choice of query language in the data source configuration:
InfluxQL query editor
The InfluxQL query editor helps you select metrics and tags to create InfluxQL queries.
To enter edit mode:
-
Hover over any part of the panel to display the actions menu on the top right corner.
-
Click the menu and select Edit.
image::influxql-query-editor-8-0.png
Filter data (WHERE)
To add a tag filter, click the plus icon to the right of the WHERE
condition.
To remove tag filters, click the tag key, then select –remove tag filter–.
Field and aggregation functions
In the SELECT
row, you can specify which fields and functions to use.
If you have a group by time, you must have an aggregation function. Some functions like derivative
also require an aggregation function.
The editor helps you build this part of the query. For example:
image::select_editor.png
This query editor input generates an InfluxDB SELECT
clause:
SELECT derivative(mean("value"), 10s) / 10 AS "REQ/s"
FROM....
To select multiple fields:
-
Click the plus button.
-
Select Field > field to add another
SELECT
clause.You can also
SELECT
an asterisk (*
) to select all fields.
Group query results
To group results by a tag, define it in a “Group By”.
To group by a tag:
-
Click the plus icon at the end of the GROUP BY row.
-
Select a tag from the dropdown that appears.
To remove the “Group By”:
-
Click the tag.
-
Click the “x” icon.
Text editor mode (RAW)
You can write raw InfluxQL queries by switching the editor mode. However, be careful when writing queries manually.
If you use raw query mode, your query must include at least WHERE $timeFilter
.
Also, always provide a group by time and an aggregation function. Otherwise, InfluxDB can easily return hundreds of thousands of data points that can hang your browser.
To switch to raw query mode:
-
Click the hamburger icon.
-
Toggle Switch editor mode.
Alias patterns
Alias pattern | Replaced with |
---|---|
|
Measurement name. |
|
Measurement name. |
|
Part of measurement name (if you separate your measurement name with dots). |
|
Column name. |
|
The value of the |
You can also use [[tag_hostname]]
pattern replacement syntax.
For example, entering the value Host: [[tag_hostname]]
in the ALIAS BY field replaces it with the hostname
tag value for each legend value. An example legend value would be Host: server1
.
SQL query editor
Grafana support SQL querying language with InfluxDB v3.0 and higher.
Macros
You can use macros within the query to replace them with the values from Grafana’s context.
Macro example | Replaced with |
---|---|
|
The start of the currently active time selection, such as |
|
The end of the currently active time selection, such as |
|
The time range that applies the start and the end of currently active time selection. |
|
An interval string that corresponds to Grafana’s calculated interval based on the time range of the active time selection, such as |
|
Applies date_bin function. Column must be timestamp. |
|
Applies date_bin function with suffix |
Examples:
// with macro 1. SELECT * FROM cpu WHERE time >= $__timeFrom AND time <= $__timeTo 2. SELECT * FROM cpu WHERE $__timeFilter(time) 3. SELECT $__dateBin(time) from cpu // interpolated 1. SELECT * FROM iox.cpu WHERE time >= cast('2023-12-15T12:38:30Z' as timestamp) AND time <= cast('2023-12-15T18:38:30Z' as timestamp) 2. SELECT * FROM cpu WHERE time >= '2023-12-15T12:41:28Z' AND time <= '2023-12-15T18:41:28Z' 3. SELECT date_bin(interval '15 second', time, timestamp '1970-01-01T00:00:00Z') from cpu
Flux query editor
Grafana supports Flux when running InfluxDB v1.8 and higher. If your data source is configured for Flux, you can use the Flux query and scripting language in the query editor, which serves as a text editor for raw Flux queries with macro support.
For more information and connection details, refer to 1.8 compatibility.
Use macros
You can enter macros in the query to replace them with values from Grafana’s context. Macros support copying and pasting from Chronograf.
Macro example | Replaced with |
---|---|
|
The start of the currently active time selection, such as |
|
The end of the currently active time selection, such as |
|
An interval string compatible with Flux that corresponds to Grafana’s calculated interval based on the time range of the active time selection, such as |
|
The data source configuration’s “Default Bucket” setting. |
|
The data source configuration’s “Organization” setting. |
For example, the query editor interpolates this query:
from(bucket: v.defaultBucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "cpu" or r["_measurement"] == "swap")
|> filter(fn: (r) => r["_field"] == "usage_system" or r["_field"] == "free")
|> aggregateWindow(every: v.windowPeriod, fn: mean)
|> yield(name: "mean")
Into this query to send to InfluxDB, with interval and time period values changing according to the active time selection:
from(bucket: "grafana")
|> range(start: 2020-06-11T13:59:07Z, stop: 2020-06-11T14:59:07Z)
|> filter(fn: (r) => r["_measurement"] == "cpu" or r["_measurement"] == "swap")
|> filter(fn: (r) => r["_field"] == "usage_system" or r["_field"] == "free")
|> aggregateWindow(every: 2s, fn: mean)
|> yield(name: "mean")
To view the interpolated version of a query with the query inspector, refer to Panel Inspector.
Query logs
You can query and display log data from InfluxDB in Explore and with the Logs panel for dashboards.
Select the InfluxDB data source, then enter a query to display your logs.
Create log queries
The Logs Explorer next to the query field, accessed by the Measurements/Fields button, lists measurements and fields. Choose the desired measurement that contains your log data, then choose which field to use to display the log message.
Once InfluxDB returns the result, the log panel lists log rows and displays a bar chart, where the x axis represents the time and the y axis represents the frequency/count.
Apply annotations
Annotations overlay rich event information on top of graphs. You can add annotation queries in the Dashboard menu’s Annotations view.
For InfluxDB, your query must include WHERE $timeFilter
.
If you select only one column, you don’t need to enter anything in the column mapping fields.
The Tags field’s value can be a comma-separated string.