Grafana Variables

Variables make dashboards interactive and reusable. Instead of building a separate dashboard for every server, you build one dashboard with a variable that lets you pick the server from a dropdown at the top. The entire dashboard updates instantly to show data for the chosen server.

The TV Remote Analogy

A variable works like a TV remote control. The remote does not change the TV hardware — it changes what the TV displays. A Grafana variable does not change the dashboard structure — it changes which data the panels show. One dashboard, unlimited views.

Dashboard with variable:
┌─────────────────────────────────────────────────┐
│ Server: [server-01 ▼]  Region: [US-East ▼]      │  ← dropdowns
├─────────────────────────────────────────────────┤
│  CPU Usage for server-01   Memory for server-01 │
│  [live chart]              [live chart]         │
└─────────────────────────────────────────────────┘

Change dropdown to server-02:
┌─────────────────────────────────────────────────┐
│ Server: [server-02 ▼]  Region: [US-East ▼]      │
├─────────────────────────────────────────────────┤
│  CPU Usage for server-02   Memory for server-02 │
│  [live chart]              [live chart]         │
└─────────────────────────────────────────────────┘

Types of Variables

Query Variable

A query variable runs a query against your data source and populates the dropdown with the results. For example, a Prometheus query can return all instance names currently being monitored, and those names fill the dropdown automatically. When you add a new server, it appears in the dropdown without any manual update.

Query: label_values(up, instance)
Result:
  server-01
  server-02
  server-03

Custom Variable

A custom variable holds a fixed list of values you type manually, separated by commas. Use it when the list does not come from a data source — for example, environment names like dev,staging,production.

Constant Variable

A constant variable stores a single fixed value used across the dashboard. It is invisible to users but useful for storing values like a base URL or an org name that every panel references.

Text Box Variable

A text box variable shows a free-text input field at the top of the dashboard. Users type any value and queries use that typed value as a filter. Useful for filtering by a specific job name or customer ID.

Interval Variable

An interval variable lets users choose the time grouping for queries — for example, 1 minute, 5 minutes, or 1 hour. Use it in Prometheus rate() functions so users can adjust chart smoothness.

Data Source Variable

A data source variable lists all connected data sources of a given type. Users can switch between different Prometheus instances (for example, prod vs dev) using one dropdown on the dashboard.

Creating a Query Variable – Step by Step

Step 1 – Open Dashboard Settings

Click the gear icon (⚙) at the top of the dashboard to open Settings. In Grafana 10 and later, click the settings menu button in the dashboard toolbar.

Step 2 – Navigate to Variables

Click Variables in the left settings menu. Click Add variable.

Step 3 – Configure the Variable

Variable type:  Query
Name:           instance
Label:          Server
Data source:    Prometheus
Query:          label_values(up, instance)

The Name is used inside queries with a dollar sign prefix: $instance. The Label is the friendly text displayed above the dropdown on the dashboard.

Step 4 – Enable Multi-Select (Optional)

Toggle Multi-value to allow users to select more than one option at once. Toggle Include All option to add an "All" choice that selects every value simultaneously.

Step 5 – Save the Variable

Click Update and then save the dashboard. The dropdown now appears at the top of the dashboard.

Using Variables in Queries

Reference a variable inside any query using the $variable_name syntax. When the user selects a value from the dropdown, Grafana substitutes the variable with the selected value before sending the query.

Query without variable (hardcoded):
  rate(node_cpu_seconds_total{instance="server-01"}[5m])

Query with variable (dynamic):
  rate(node_cpu_seconds_total{instance="$instance"}[5m])

Switching the dropdown from server-01 to server-02 instantly replaces $instance with server-02 in every panel that uses this variable.

Chained Variables

Variables can depend on each other. A Region variable filters the list shown in a Server variable. When the user picks a region, the server dropdown refreshes to show only servers in that region.

User picks Region → [US-East]
   │
   ▼
Server dropdown refreshes → shows only US-East servers
   │
   ▼
Dashboard panels show data for selected server in US-East

To chain variables, reference the parent variable in the child variable's query:

Region variable query:  label_values(up, region)
Server variable query:  label_values(up{region="$region"}, instance)

Using Variables in Panel Titles

You can embed variables in panel titles, making it clear which data the panel currently shows. Set the panel title to:

CPU Usage — $instance

When the user selects server-02, the panel title automatically updates to CPU Usage — server-02.

Repeating Panels with Variables

Grafana can automatically repeat a panel for each selected value in a multi-value variable. Enable this in the panel editor under Panel options → Repeat options → Repeat by variable. Select your variable. Grafana generates one copy of the panel per selected value, arranged in a grid.

Variable: $instance = [server-01, server-02, server-03]
Repeat panel enabled:

[CPU: server-01]  [CPU: server-02]  [CPU: server-03]

Leave a Comment

Your email address will not be published. Required fields are marked *