Connecting to Prometheus Data Sources¶
Grafana provides native support for querying Prometheus servers, enabling you to visualize metrics from local, remote write, or federated Prometheus instances. This section guides you through configuring Grafana to connect to a Prometheus data source.
Adding a Prometheus Data Source¶
To add a Prometheus data source in Grafana, follow these steps:
-
Navigate to Data Source Settings
In the Grafana UI, go to Configuration > Data Sources and click Add data source. -
Select Prometheus
Choose Prometheus from the list of available data sources. -
Configure Connection Settings
Enter the following parameters: - URL: The Prometheus server endpoint (e.g.,
http://localhost:9090for a local instance orhttps://prometheus.example.comfor a remote server). - Access: Choose between Server (default), Proxy, or Browser. Use Server for direct queries, Proxy for Grafana to act as an intermediary, or Browser for browser-based access (less common).
- HTTP Method: Select
GETorPOSTbased on your Prometheus setup (typicallyGETfor most use cases). -
Authentication: Configure basic auth, API keys, or TLS certificates if required. For example:
-
Test the Connection
Click Save & Test to verify the connection. Grafana will attempt to query a sample metric (e.g.,up{job="prometheus"}).
Advanced Configuration Options¶
1. Remote Write and Federated Queries¶
For remote write setups, ensure the Prometheus URL points to the remote write endpoint (e.g., http://remote-write-server:9009). Federated queries can be configured using the federate endpoint, with the URL formatted as http://prometheus-federate:9090/api/v1/federate.
2. TLS/SSL Configuration¶
Enable TLS by specifying the CA certificate path or using a trusted certificate bundle:
[datasources]
my-prometheus = {
type = prometheus
url = https://secure-prometheus.example.com
tls_server_name = prometheus.example.com
tls_skip_verify = false
tls_ca_cert = /path/to/ca.crt
}
3. Query Timeout and Max Samples¶
Adjust query timeouts and sample limits in the Grafana configuration file (grafana.ini):
Example: Querying Prometheus Metrics¶
Once connected, use PromQL to query metrics. For example:
This query calculates the rate of HTTP requests over the last 5 minutes for theapi-server job.
Best Practices¶
- Use Proxy Mode for Security: Avoid exposing Prometheus directly to the internet by using Grafana as a proxy.
- Monitor Query Performance: Track slow queries in Prometheus using the
query_duration_secondsmetric. - Secure Authentication: Always use HTTPS and disable insecure HTTP methods unless necessary.
- Version Compatibility: Ensure Grafana and Prometheus versions are compatible (e.g., Grafana 10.x supports Prometheus 2.38+).
Key takeaways¶
- Configure Prometheus data sources via Grafana's UI or
grafana.inifile. - Use
Serveraccess for direct queries,Proxyfor security, andBrowserfor limited use cases. - Enable TLS and authentication to secure remote connections.
- Test connections with sample metrics and validate query performance.
- Leverage PromQL for advanced metric analysis and visualization.