Prometheus Data Source
Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability. Grafana provides native support for querying Prometheus metrics using PromQL (Prometheus Query Language).Overview
The Prometheus data source allows you to:- Query time series metrics with PromQL
- Visualize instant vectors and range vectors
- Use metric metadata for query building
- Create alerting rules based on Prometheus queries
- Explore exemplars and tracing integration
Source:
public/app/plugins/datasource/prometheus/ and packages/grafana-prometheus/Configuration
Connection Settings
Configure URL
Set the Prometheus server URL:Use the proxy access mode (default) for server-side queries.
Authentication
Configure authentication if required:
- Basic authentication
- TLS client certificate
- OAuth passthrough
- Azure authentication
Data Source Options
Minimum scrape interval (e.g.,
30s, 1m). Used to calculate $__interval and $__rate_interval.Maximum time to wait for a query response (e.g.,
60s).HTTP method for queries. POST is recommended for long queries.
Disable metric name lookup for improved performance with large metric counts.
Prometheus implementation type:
Prometheus, Cortex, Mimir, or Thanos.Query result cache level:
None, Low, Medium, or High.Query Editor
The Prometheus query editor supports two modes:- Code Mode
- Builder Mode
Write PromQL queries directly:Features:
- Syntax highlighting with Monaco editor
- Auto-completion for metrics, labels, and functions
- Query validation
- Metric browser
PromQL Examples
Instant Query
Get current value of a metric:Range Query with Rate
Calculate per-second rate over 5 minutes:Source:
packages/grafana-prometheus/src/components/PromCheatSheet.tsx:13-16Percentile Calculation
Calculate 95th percentile of request latencies:Source:
packages/grafana-prometheus/src/components/PromCheatSheet.tsx:18-21Aggregation
Sum values and group by label:Alert Tracking
Sum alerts firing over 24 hours:Source:
packages/grafana-prometheus/src/components/PromCheatSheet.tsx:23-26Query Options
Legend Format
Customize series names using label values:localhost:9090 - prometheus
Min Step
Defines the graph resolution using duration format:15s- high resolution, may be slow over large time ranges1m- medium resolution5m- low resolution, faster queries
If no step is specified, resolution is calculated automatically based on panel width and time range.Source:
packages/grafana-prometheus/src/components/PromCheatSheet.tsx:28-31Resolution
Control data point density:1/1- One data point per pixel1/2- One data point per 2 pixels (faster)1/10- Sparse data points
Template Variables
Label Values Query
Populate variable from label values:instance label.
Metric Names Query
List all metrics matching a pattern:Query Result Variable
Use query results as variable values:Using Variables in Queries
Exemplars
Exemplars link metrics to trace IDs for deeper analysis:Query with Exemplars
Enable “Exemplars” in query options. Grafana displays exemplar data points on the graph.
Advanced Features
Recording Rules
Query pre-computed recording rules for faster dashboards:Incremental Querying
Enable incremental querying for live dashboards:Custom Query Parameters
Add custom parameters to all Prometheus requests:Troubleshooting
No data in graph
No data in graph
- Verify time range includes data points
- Check metric exists:
up{job="your-job"} - Confirm Prometheus is scraping the target
- Review Prometheus logs for scrape errors
Query timeout
Query timeout
- Reduce time range
- Increase
queryTimeoutin data source settings - Use recording rules for expensive queries
- Add more specific label filters to reduce cardinality
High cardinality warnings
High cardinality warnings
- Avoid labels with unbounded values (IDs, timestamps)
- Use recording rules to pre-aggregate
- Enable
disableMetricsLookupfor better UI performance - Configure
seriesLimitto prevent overload
Metric autocomplete not working
Metric autocomplete not working
- Check network connectivity to Prometheus
- Verify
/api/v1/label/__name__/valuesendpoint is accessible - Try enabling
disableMetricsLookupand use manual entry
Best Practices
Use Rate for Counters
Always use
rate() or irate() for counter metrics:Avoid Joins
PromQL joins can be expensive. Use recording rules for complex joins.
Label Matchers
Be specific with label matchers to reduce query scope:
Range Selection
Choose appropriate range intervals:
[1m]for real-time metrics[5m]for general dashboards[1h]for long-term trends