Azure Cosmos DB alerting
The Azure Cosmos DB data source supports Grafana alerting. Because the plugin runs queries in the backend, you can use Azure Cosmos DB for NoSQL queries as the source for Grafana-managed alert rules.
Before you begin
Before you create an alert rule, ensure you have:
- Configured the Azure Cosmos DB data source.
- A query that returns numeric data. Alert rules evaluate numeric values, so your query must return one or more numeric fields.
- Familiarity with Grafana alerting.
How alerting works with the data source
Grafana-managed alert rules run a query on a schedule and evaluate the results against a condition. For the Azure Cosmos DB data source:
- The rule runs your Azure Cosmos DB for NoSQL query in the backend on the rule’s evaluation interval.
- The query result passes through one or more expressions, such as Reduce or Threshold, to produce a single numeric value per series.
- Grafana compares that value to the alert condition to determine whether the alert fires.
Create an alert rule
To create an alert rule that uses the Azure Cosmos DB data source:
- Navigate to Alerting > Alert rules.
- Click New alert rule.
- Enter a name for the rule.
- Under Define query and alert condition, select the Azure Cosmos DB data source.
- In the query editor, select the Database and Container, and optionally enter a value in the PartitionKey field.
- Enter an Azure Cosmos DB for NoSQL query that returns numeric data. Use the
$__timeFilter,$__timeFrom, and$__timeTomacros to scope the query to the evaluation time range. - Add expressions to reduce the query result to a single value and define the threshold that triggers the alert.
- Set the evaluation behavior, including the evaluation group and interval.
- Add labels and notifications, then click Save rule and exit.
Alert query examples
Alert queries must return numeric data. Aggregate functions such as COUNT, MAX, and AVG require a single-partition query, so enter a value in the PartitionKey field when you use them.
Alert when the number of error events in the time range exceeds a threshold. This query returns a single numeric value, which you compare with a Threshold expression:
SELECT VALUE COUNT(1)
FROM c
WHERE c.level = "error" AND $__timeFilter(c.timestamp)Alert on the maximum temperature reported by a device:
SELECT VALUE MAX(c.temperature)
FROM c
WHERE c.deviceId = "device-01" AND $__timeFilter(c.timestamp)Return a numeric time series and let a Reduce expression collapse it to a single value before the threshold check:
SELECT c.timestamp, c.temperature
FROM c
WHERE c.deviceId = "device-01" AND $__timeFilter(c.timestamp)Query considerations
Keep the following considerations in mind when you write alerting queries:
- Return numeric data. The query must return at least one numeric field for the alert condition to evaluate.
- Scope the time range with macros. Use
$__timeFilter(column),$__timeFrom(column), or$__timeTo(column)so the query only evaluates data in the rule’s time range. For more information, refer to Azure Cosmos DB query editor. - Set a partition key when possible. Multi-partition queries don’t support the
TOP,ORDER BY,OFFSET,LIMIT,Aggregates,DISTINCT, andGROUP BYkeywords. Enter a value in the PartitionKey field to run a single-partition query when your alert query needs these keywords. - Limit the result set. Narrow the time range and filter the data to reduce request units (RU/s) consumed on each evaluation.


