ClickHouse alerting
The ClickHouse data source supports Grafana Alerting and Grafana-managed recording rules. You can create alert rules that run ClickHouse SQL queries and fire when the result meets a condition (for example, a value is above a threshold or no data is returned).
Alert rules run as background processes. Grafana executes your ClickHouse query on a schedule, then evaluates the result using expressions such as Reduce and Threshold. Your query must return numeric data that Grafana can evaluate.
For an overview of alerting in Grafana, see Alert rules and Create a Grafana-managed alert rule.
Before you begin
- Configure the ClickHouse data source.
- Ensure your ClickHouse user has read access to the tables used in your alert query.
- Familiarize yourself with Grafana Alerting concepts.
Query requirements for alerting
Alert rules need numeric values to evaluate. Your ClickHouse query should return data that Grafana can use in a Reduce expression and then compare in a Threshold expression.
Queries that return only text or non-numeric data cannot be used directly for threshold evaluation. Use count(), avg(), sum(), or similar in your SQL so the result is numeric.
Use the $__timeFilter(column) macro in your WHERE clause so the query respects the alert rule’s evaluation interval and time range. See the ClickHouse query editor Macros section.
Create an alert rule
To create an alert rule using ClickHouse data:
- Go to Alerting > Alert rules.
- Click New alert rule.
- Enter a Name for the rule.
- In Define query and alert condition:
- Select your ClickHouse data source.
- In the Query tab, write a ClickHouse SQL query that returns a time column and a numeric column (or a single numeric value). Use Format > Time series if your query returns time + value; use Table if it returns a single row.
- Add a Reduce expression to aggregate the query result (e.g. Last to use the latest value, Max for the highest, Mean for the average).
- Add a Threshold expression to define when the alert fires (e.g. Is above 80, Is below 3).
- Configure Set evaluation behavior: choose a folder and evaluation group, set the evaluation interval, and set the pending period.
- Add Labels and Annotations for notifications.
- Click Save rule.
For detailed steps, see Create a Grafana-managed alert rule.
Example: Metric threshold alert
This example fires when the average value of a metric exceeds 80. Replace the table and column names with your own.
Query (Time series format):
SELECT
$__timeInterval(timestamp) AS time,
avg(value) AS value
FROM my_app.metrics
WHERE $__timeFilter(timestamp)
AND metric_name = 'cpu_usage'
GROUP BY time
ORDER BY timeIn the alert rule, add Reduce > Last (or Max) and Threshold > Is above 80.
Example: Error count alert
This example fires when the number of error rows in the last 5 minutes exceeds 10.
Query (Table format; single row):
SELECT count() AS value
FROM my_app.events
WHERE $__timeFilter(timestamp)
AND level = 'error'In the alert rule, set the query Format to Table, add Reduce > Last, and Threshold > Is above 10.
Example: No data alert
You can alert when a query returns no rows (for example, a health check that should always return at least one row). Write a query that returns a row when things are healthy, then in the alert rule configure Configure no data and error handling to Alerting when there is no data.
Best practices
- Use an appropriate evaluation interval — Set the alert evaluation interval to match how often your data is written. Avoid intervals shorter than your data resolution to prevent flapping or missed data.
- Reduce multiple series — If your query returns multiple time series (e.g. one per host), use Reduce to aggregate: Last, Max, Mean, or Sum so Grafana can evaluate a single value.
- Restrict the time range — Use $__timeFilter(column) in your WHERE clause so the query only reads data in the evaluation window. Avoid full table scans.
- Handle no data — In Configure no data and error handling, choose whether no data should keep the alert as-is, fire the alert, or resolve it. Use Alerting when no data indicates a problem (e.g. a heartbeat query).
- Test the query first — Run the query in Explore with the ClickHouse data source and confirm it returns the expected numeric data before saving the alert rule.
Troubleshooting
If alerts do not fire or evaluate as expected:
- Query returns no numeric data — Confirm the query returns a time column and a numeric column (or a single numeric value). Test in Explore and check the result format.
- Evaluation interval — Ensure the evaluation interval is long enough for data to be available. Avoid intervals shorter than your data resolution.
- No data handling — In Configure no data and error handling, choose whether no data should fire the alert, resolve it, or keep the current state.
For connection errors, timeouts, or other data source issues, see Troubleshoot ClickHouse data source issues.
Next steps
- ClickHouse query editor — Macros such as
$__timeFilterand$__timeInterval. - Grafana Alerting — Alert rules, contact points, and notification policies.
- Troubleshoot ClickHouse data source issues — Common errors and solutions.


