The ClickHouse data source plugin allows you to query and visualize ClickHouse data from within Grafana.
This plugin is currently in Beta development and subject to change.
-
Navigate to ClickHouse plugin homepage.
-
From the left-hand menu, click the Install plugin button.
The Installation tab is displayed.
Set up an ClickHouse user account with readonly
permission and access to databases and tables you want to query. Grafana does not validate that queries are safe. Queries can contain any SQL statement. For example, statements like ALTER TABLE system.users DELETE WHERE name='sadUser'
and DROP TABLE sadTable;
would be executed.
Follow these instructions to add a new ClickHouse data source, and enter configuration options:
It is possible to configure data sources using configuration files with Grafana’s provisioning system. To read about how it works, including and all the settings that you can set for this data source, refer to Provisioning Grafana data sources.
Here are some provisioning examples for this data source using basic authentication:
apiVersion: 1
datasources:
- name: ClickHouse
type: grafana-clickhouse-datasource
jsonData:
defaultDatabase: database
port: 9000
server: localhost
username: username
tlsSkipVerify: false
secureJsonData:
password: password
The query editor allows you to query ClickHouse to return time series or tabular data. Queries can contain macros which simplify syntax and allow for dynamic parts.
Time series visualization options are selectable after adding a datetime
field type to your query. This field will be used as the timestamp You can select time series visualizations using the visualization options. Grafana interprets timestamp rows without explicit time zone as UTC. Any column except time is treated as a value column.
To create multi-line time series, the query must return at least 3 fields in the following order:
- field 1:
datetime
field with an alias oftime
- field 2: value to group by
- field 3+: the metric values
For example:
SELECT log_time as time, machine_group, avg(disk_free) as avg_disk_free
from mgbench.logs1
group by machine_group, log_time
order by log_time
Table visualizations will always be available for any valid ClickHouse query.
To use the Logs panel your query must return a timestamp and string values.
For example:
SELECT log_time as time, machine_group, toString(avg(disk_free)) as avg_disk_free
from logs1
group by machine_group, log_time
order by log_time
To simplify syntax and to allow for dynamic parts, like date range filters, the query can contain macros.
Here is an example of a query with a macro that will use Grafana's time filter:
SELECT
date_time,
data_stuff,
FROM test_data
WHERE $__timeFilter(date_time)
Macro | Description | Output example |
---|---|---|
$__timeFilter(columnName) | Replaced by a conditional that filters the data (using the provided column) based on the time range of the panel in seconds | time >= '1480001790' AND time <= '1482576232' ) |
$__timeFilter_ms(columnName) | Replaced by a conditional that filters the data (using the provided column) based on the time range of the panel in milliseconds | time >= '1480001790671' AND time <= '1482576232479' ) |
$__fromTime | Replaced by the starting time of the range of the panel casted to DateTime | toDateTime(intDiv(1415792726371,1000)) |
$__toTime | Replaced by the ending time of the range of the panel casted to DateTime | toDateTime(intDiv(1415792726371,1000)) |
The plugin also supports notation using braces {}. Use this notation when queries are needed inside parameters.
To add a new ClickHouse query variable, refer to Add a query variable. Use your ClickHouse data source as your data source for the following available queries:
After creating a variable, you can use it in your ClickHouse queries by using Variable syntax. For more information about variables, refer to Templates and variables.
Follow these instructions for importing a dashboard.
Imported dashboards can be found in Configuration > Data Sources > select your ClickHouse data source > select the Dashboards tab to see available pre-made dashboards.
A second helper variable must be created in addition to the standard ad-hoc filter type variable of any name. It should be a constant
type named clickhouse_adhoc_query
and contain a valid ClickHouse query. The query results will be used to populate your ad-hoc filter's selectable filters. You may choose to hide this variable from view as it serves no further purpose.
If clickhouse_adhoc_query
is set to SELECT DISTINCT machine_name FROM mgbench.logs1
you would be able to select which machine names are filtered for in the dashboard.
- Add Annotations.
- Configure and use Templates and variables.
- Add Transformations.
- Set up alerting; refer to Alerts overview.