Templating allows you to create dynamic, reusable dashboards that adapt based on variable values. Template variables can be used throughout your dashboard in queries, panel titles, annotations, and links.
Grafana uses a simple templating syntax for variable interpolation:
// Basic syntax${variable_name} // Standard interpolation$variable_name // Shorthand (no spaces/special chars)[[variable_name]] // Legacy syntax (still supported)// With formatting${variable_name:format} // Apply format to variable value
# Single variableCPU Usage - ${server}# Multiple variables${environment} - ${datacenter} - ${server}# With built-in variablesRequests (${__from:date:YYYY-MM-DD} to ${__to:date:YYYY-MM-DD})# Conditional text (using variable value)${server} Status - Last ${__range_s} seconds
This panel shows metrics for **${server}** in the **${environment}** environment.Data source: ${datasource}Time range: ${__from:date} to ${__to:date}Interval: ${__interval}
// Dashboard link{ title: "Detailed ${server} Dashboard", url: "/d/details?var-server=${server}&var-env=${environment}", keepTime: true}// External link with multiple variables{ title: "View in External System", url: "https://monitoring.example.com/${environment}/${datacenter}/${server}", targetBlank: true}// Data link in panel{ title: "Drill down to ${__field.labels.instance}", url: "/d/host-details?var-host=${__field.labels.instance}&${__url_time_range}"}
${variable:text} // Display text instead of value${variable:percentencode} // URL percent encoding${variable:raw} // Raw value without escaping${variable:query} // Query parameter format
// Built-in date formats${__from:date:iso} // 2024-01-01T00:00:00Z${__from:date:seconds} // 1704067200 (Unix timestamp)${__from:date:YYYY-MM-DD} // 2024-01-01${__from:date:MM/DD/YYYY} // 01/01/2024${__from:date:YYYY-MM-DD HH:mm:ss} // 2024-01-01 00:00:00// Relative times${__from:date:iso} // Absolute ISO time${__to:date:iso} // Absolute ISO time// Range calculations ${__range_s} // Range in seconds${__range_ms} // Range in milliseconds${__range} // Range as "5m", "1h", etc.
// Define base variable{ name: "cluster", type: "custom", query: "prod,staging"}// Use in another variable's query{ name: "namespace", type: "query", query: "label_values(kube_namespace_labels{cluster='${cluster}'}, namespace)"}// Use both in panel querykube_pod_info{cluster="${cluster}", namespace="${namespace}"}
# Different queries based on environment# In "production" environmentrate(production_metrics{instance="${server}"}[5m])# Create a constant variable for conditional logic# constant variable: is_production = ${environment:regex} == "production"
$__from // Start time (milliseconds)$__to // End time (milliseconds) $__interval // Dynamic interval (e.g., "1m")$__interval_ms // Interval in milliseconds$__range // Time range (e.g., "6h")$__range_s // Range in seconds$__range_ms // Range in milliseconds// Formatted times${__from:date:iso} // ISO 8601 format${__from:date:seconds} // Unix timestamp${__from:date:YYYY-MM-DD} // Custom format
${__field.name} // Field name${__field.labels.labelname} // Specific label value ${__field.labels.__name__} // Metric name${__value.raw} // Raw field value${__value.numeric} // Numeric value${__value.text} // Text representation${__value.time} // Time value (if applicable)
$__dashboard // Current dashboard name${__dashboard.uid} // Dashboard UID$__org_id // Organization ID$__org_name // Organization name $__user_id // Current user ID$__user_login // Current user login$__user_email // Current user email
# Instead of template math, use PromQLrate(metric[${interval}]) * ${multiplier}# Where multiplier is a constant variable{ name: "multiplier", type: "constant", query: "100"}
// Good variable namesserver_name // Clear and descriptiveenvironment // Full wordtime_interval // Underscores for readability// Avoidsrv // Too abbreviatede // Single letterServerName // Use lowercaseserver-name // Use underscores not hyphens