Azure Cosmos DB template variables
Instead of hard-coding details such as databases, containers, partition keys, and WHERE clause filters, you can use variables. Grafana lists these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard. Grafana refers to such variables as template variables.
Before you begin
Before you create template variables:
- Configure the Azure Cosmos DB data source.
- Familiarize yourself with Grafana template variables. For an introduction, refer to the following topics:
Supported variable types
The Azure Cosmos DB data source provides its own query variable editor. Other variable types, such as Custom, Constant, Interval, and Data source, are standard Grafana features and are also available.
Add a query variable
To add an Azure Cosmos DB query variable:
- Navigate to Dashboard settings > Variables.
- Click Add variable.
- Select Query as the variable type.
- Select your Azure Cosmos DB data source.
- Select a Query Type.
- Click Run query, then confirm the values under Preview of values.
The following query types are available:
Query variable examples
For the Query query type, write a query that returns a single field. Use the VALUE keyword to return a flat list of scalar values instead of objects.
Return the distinct set of regions to populate a region variable:
SELECT DISTINCT VALUE c.region FROM cReturn the distinct set of status values:
SELECT DISTINCT VALUE c.status FROM cCreate dependent variables
Because the Containers query type requires a database, you can chain variables so that one variable’s options depend on another.
To create a container variable that depends on a database variable:
- Create a Databases query variable, for example named
database. - Create a second query variable and select the Containers query type.
- In the container variable’s Database field, select the
databasevariable.
When you change the database variable on the dashboard, the container variable updates its options automatically.
Use variables in queries
Reference variables in the query editor using the $variable syntax. The data source interpolates variables in the query text and in the Database, Container, and PartitionKey fields.
Filter a query by a single-value variable:
SELECT c.timestamp, c.temperature
FROM c
WHERE c.region = '$region' AND $__timeFilter(c.timestamp)For a multi-value variable, the data source formats the selected values as a quoted, comma-separated list, so you can use the variable with the IN operator:
SELECT c.timestamp, c.status
FROM c
WHERE c.status IN ($status) AND $__timeFilter(c.timestamp)

