Sub topic 11
Adding Prometheus as a Grafana Data Source
Assume you can edit Grafana configuration, manage its systemd service, and reach the Grafana web interface. You should also have a working Prometheus server and know its HTTP address.
Why Grafana needs a data source
Grafana displays data from external systems called data sources. A Prometheus data source tells Grafana where to send PromQL queries.
Grafana does not usually collect server metrics itself. Prometheus collects and stores the metrics. Grafana asks Prometheus for selected time ranges, then renders the results in panels.
The URL must point to Prometheus from Grafana's point of view. If both services run on the same host, use http://localhost:9090. If they run in separate containers, virtual machines, or servers, use a hostname or IP address reachable from the Grafana process.
Add the source in the web interface
- Sign in to Grafana.
- Open Connections or Configuration, depending on the Grafana version.
- Select Data sources, then choose Add data source.
- Select Prometheus.
- Set the URL, such as
http://localhost:9090. - Leave Access set to Server or Proxy. Grafana then makes requests to Prometheus, so a user's browser does not need direct network access.
- Select Save & test.
A successful test confirms that Grafana can reach the Prometheus HTTP API. It does not prove that every metric or scrape target is healthy. Check those separately in Prometheus.
| Setting | Meaning |
|---|---|
| URL | Prometheus HTTP endpoint reachable by Grafana |
| Access: Server/Proxy | Grafana sends the request on the user's behalf |
| Default | New panels use this source unless another source is selected |
Provision the source with YAML
Provisioning creates the data source from a file. It is useful when rebuilding servers or managing configuration consistently.
Create a file such as /etc/grafana/provisioning/datasources/prometheus.yml:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: trueRestart Grafana after adding or changing provisioning files:
sudo systemctl restart grafana-server
sudo systemctl status grafana-server --no-pagerThe isDefault setting makes this source the default for newly created panels. Provisioned sources may be read-only in the web interface, so change the YAML file instead of relying on manual edits.
Troubleshoot connection failures
- Connection refused: Prometheus is stopped, the port is wrong, or a firewall blocks access.
- DNS failure: Grafana cannot resolve the hostname in the URL.
- HTTP 404: The URL may include an incorrect path or reverse-proxy prefix.
- Successful connection but empty panels: The source works, but the selected metric, labels, time range, or Prometheus scrape configuration may be wrong.
Exercise
Grafana and Prometheus run on different servers. Should the data source URL use the browser's address or Grafana server's address?
Show answer
Use the Prometheus address reachable from the Grafana server, because Server/Proxy access makes Grafana send the request.