Sub topic 11
Adding Prometheus as a Grafana data source
This chapter assumes you have a running Prometheus server that scrapes at least one node_exporter target, and a Grafana instance installed as a systemd service. You should be able to edit YAML files under /etc, and you should know that up is the metric Prometheus records for each scrape target. Grafana does not store metrics. It asks Prometheus for them each time a panel refreshes, and the data source is the saved connection that makes this possible.
What a data source does
A Grafana data source is a named connection to a system that holds data. It stores a type, a URL, authentication settings, and options that change how queries are built. Every panel on a dashboard points at one data source, and Grafana sends the panel's query to it.
For Prometheus, Grafana calls the HTTP API on the Prometheus server. Range queries go to /api/v1/query_range. Instant queries, such as a single stat panel, go to /api/v1/query. Label and metric name autocompletion use /api/v1/labels and /api/v1/label/<name>/values.
Access modes
The data source has an access mode that decides who contacts Prometheus.
| Mode | Who sends the query | When it fits |
|---|---|---|
| Server (`proxy`) | The Grafana server | The default and the right choice almost always. Prometheus can stay on a private network. |
| Browser (`direct`) | The user's browser | Legacy. Prometheus must be reachable from every viewer's machine, and CORS must be configured. Recent Grafana versions have removed it for Prometheus. |
Use proxy. It means only the Grafana host needs network access to port 9090, and a firewall can block everyone else.
Adding the data source in the web interface
- Sign in to Grafana (port 3000 by default) as an administrator.
- Open Connections, then Data sources, then Add data source.
- Choose Prometheus.
- Set Prometheus server URL to
http://localhost:9090, or the address of your Prometheus host. - Leave authentication off for now. Set Scrape interval to match your
scrape_interval, which is 15s in the sample configuration. - Select Save & test.
A green banner such as "Successfully queried the Prometheus API" means Grafana reached the server and got a valid response. The exact menu names vary between Grafana versions, but the fields are the same.
Provisioning the data source from a file
Clicking through the interface works once. It does not survive a rebuilt server, and you cannot review it in version control. Grafana can read data sources from YAML files at startup. This is called provisioning.
Create /etc/grafana/provisioning/datasources/prometheus.yaml:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: trueRestart Grafana so it reads the file, then confirm it is healthy:
sudo systemctl restart grafana-server
sudo systemctl status grafana-server --no-pager
curl -s http://localhost:3000/api/healthThe health endpoint returns a small JSON document with "database": "ok" when Grafana is running. The new data source appears under Connections, Data sources.
Provisioned data sources are read-only in the interface by default. This is intentional: the file is the source of truth, and edits in the browser would be overwritten at the next restart. If you need to edit one in the interface, add editable: true to its entry.
Settings worth setting explicitly
The minimal file works, but a few options matter once dashboards grow. They go under jsonData:
apiVersion: 1
datasources:
- name: Prometheus
uid: prometheus-main
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: true
editable: false
jsonData:
timeInterval: 15s
httpMethod: POSTuidis a stable identifier. Dashboards exported as JSON refer to a data source by uid. If you omit it, Grafana generates one, and a dashboard imported on another server may not find its data source.timeIntervaltells Grafana the scrape interval. Grafana uses it as the lower bound for the$__rate_intervalvariable. With a 15 s scrape interval, arate()window must contain at least two samples, so it needs at least four times the interval in practice, which Grafana computes for you.httpMethod: POSTsends queries in the request body instead of the URL. Long queries, such as ones with a big regular expression from a dashboard variable, can exceed URL length limits under GET.
The data source name matters too. If you add a second Prometheus, for example one per site, give each a distinct, descriptive name. Panels show that name, and you will use it later to build a data source variable.
Authentication to Prometheus
The sample Prometheus server has no authentication, which is why the file needs none. If you put a reverse proxy with basic authentication in front of Prometheus, add the credentials to the data source:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: https://prometheus.example.com
basicAuth: true
basicAuthUser: grafana
secureJsonData:
basicAuthPassword: ${PROMETHEUS_PASSWORD}Grafana expands ${PROMETHEUS_PASSWORD} from its environment, so the secret stays out of the file. With the Debian or RPM packages, set the variable in a systemd drop-in created with sudo systemctl edit grafana-server. Values under secureJsonData are encrypted when Grafana stores them.
Troubleshooting a failed test
When Save & test fails, test the same URL from the Grafana host, not from your laptop. Grafana makes the request in proxy mode, so only the Grafana host's view of the network counts.
curl -s 'http://localhost:9090/api/v1/query?query=up'A healthy answer starts with {"status":"success". Match the symptom to a cause:
| Symptom | Likely cause |
|---|---|
| `connection refused` | Prometheus is stopped, or listens on a different address or port. |
| `no such host` | The hostname does not resolve from the Grafana host. |
| `connection refused` with Grafana in a container | `localhost` means the container itself. Use the host's address or a shared network name. |
| HTTP 401 or 403 | A proxy in front of Prometheus requires credentials. |
| Test passes but panels are empty | The connection is fine. The query or time range returns no series. |
The last row trips people most often. A passing test only proves that the API answers. Check the data itself in Explore, which runs a query against a chosen data source with no dashboard around it. Enter up and run it. You should see one series for each scrape target, with a value of 1 for healthy targets. If up returns nothing, the problem is in Prometheus, so go back to the targets page.
Check your understanding
Exercise
A colleague's dashboard shows "No data" on every panel, yet Save & test on the data source succeeds. Which two checks do you make first, and where?
Show answer
First, open Explore, select the Prometheus data source, and run up. If that returns series, the connection and the data are fine, so check the dashboard's time range and the query's label matchers. If up returns nothing, run curl -s 'http://localhost:9090/api/v1/query?query=up' on the Grafana host and open Prometheus's targets page to see whether any scrape is succeeding. A passing test only proves that the API responds, not that it holds data.
Exercise
Why should a provisioned data source set an explicit uid, and what happens to a dashboard JSON file that refers to a uid that does not exist on the server?
Show answer
Dashboards reference a data source by uid. With an explicit uid, the same dashboard JSON works on any Grafana server that provisions that data source. If the uid does not exist, the panels cannot find their data source and show an error or no data until you select a valid data source for them.