Prometheus Scrape Configs and Exporters, Hands-On
Your team adds a PostgreSQL database and asks you to put it on the dashboards. You open Prometheus, and the database is not there: Prometheus visits only the targets its config lists, and the database has no /metrics page to visit anyway. This lesson shows how a Prometheus scrape job is written, how an exporter fills the gap, and how to add one to the lab and debug it.
Anatomy of prometheus.yml
The guard's list is prometheus.yml, which you met in What Is Prometheus?. Its global section holds defaults for every job, and scrape_configs holds the jobs. These are the keys you need, with defaults from the configuration reference:
scrape_interval: how often each target is scraped; 1m by default, 15s in the lab.scrape_timeout: how long one scrape may take before it fails; 10s by default, and never longer than the interval.job_name: the job's name, unique in the file, which becomes thejoblabel.static_configswithtargets: a fixed list ofhost:portaddresses.metrics_pathandscheme:/metricsandhttpunless you set them.
A job can set its own scrape_interval or scrape_timeout to override the global one. Each address under targets becomes one target, and its host:port becomes the instance label, as the diagram shows for the lab's two app jobs.
Exporters: translators for systems without /metrics
Many systems keep their stats in their own form: a Linux machine in files under /proc, a database in the answers to status queries. They do not serve the Prometheus text format, and you usually cannot change their code to add it.
An exporter is a small program that fills the gap. Each time Prometheus scrapes it, it reads the system's stats and returns them as a /metrics page. Prometheus scrapes the exporter, never the system. The Prometheus docs advise one exporter per instance, ideally on the same machine.
The classic one is node_exporter, the official exporter for host metrics on Linux and other Unix-like systems: CPU time, memory, disks, network and load average, in series named node_…. It listens on port 9100 by default. The exporters list has many more, such as the official MySQL server exporter and a community PostgreSQL exporter.
So for anything new, first ask whether it already serves a /metrics page. In the flowchart, notice that both answers end in a scrape job.
In the lab, node_exporter runs in a container with no extra flags. Its CPU, memory and load figures come from the Linux kernel, so they describe the machine Docker runs on; on macOS and Windows, that is Docker Desktop's Linux virtual machine. Its disk and network figures describe the container. On a real server you run it on the host, as its README explains.
Add a node-exporter job and reload Prometheus
The lab in course-labs/open-source-observability-stack/02-prometheus-alertmanager already runs a node exporter. Here is its service in docker-compose.yml, without comments:
node-exporter:
image: prom/node-exporter:v1.13.1
ports:
- "127.0.0.1:9100:9100"
networks:
- obs-lab
restart: unless-stopped
It joins the obs-lab network, so Prometheus can reach it by its service name, node-exporter; the published port lets you read its page from your laptop. But prometheus.yml does not list it yet, so Prometheus never visits. Every config change follows the same four steps:
Step 1: add the job
Open prometheus/prometheus.yml and add these lines at the end, indented exactly like the shop-demo job above them:
- job_name: node-exporter
static_configs:
- targets: ["node-exporter:9100"]
Write node-exporter:9100, not localhost:9100. Inside the Prometheus container, localhost is Prometheus itself, which is why the prometheus job can use it and this job cannot. The container sees your edit at once, because the lab shares the whole folder with it, but Prometheus does not reread the file on its own.
Step 2: check the file with promtool
promtool is a command-line tool, shipped in the Prometheus image, that checks config and rule files without touching the running server. Run it inside the container:
docker compose exec prometheus promtool check config /etc/prometheus/prometheus.yml
A valid file prints the two lines below. A mistake such as a wrong indent prints FAILED: and the problem instead, usually with a line number; fix it and check again.
Checking /etc/prometheus/prometheus.yml
SUCCESS: /etc/prometheus/prometheus.yml is valid prometheus config file syntax
Step 3: reload, then look at the targets
The lab starts Prometheus with --web.enable-lifecycle, which turns on an HTTP endpoint for reloading the config without a restart:
curl -X POST localhost:9090/-/reload
Success prints nothing. If the file is broken, the reply starts with failed to reload config: and Prometheus carries on with the old config. Without the flag, the reply is Lifecycle API is not enabled. Then reload http://localhost:9090/targets: a node-exporter group appears, UNKNOWN until its first scrape, then 1 / 1 up.
Finding targets automatically: service discovery and relabeling
A fixed list suits a lab, but in Kubernetes or a cloud, pods (Kubernetes' groups of running containers) and virtual machines come and go all day. Service discovery lets Prometheus fetch its targets from the platform instead: kubernetes_sd_configs asks the Kubernetes API, ec2_sd_configs asks AWS. Each discovered target arrives with extra labels starting with __meta_, such as __meta_kubernetes_namespace. Relabeling, set with relabel_configs, rewrites or filters targets by those labels before any scrape. This job keeps only pods in the shop namespace, a named group of Kubernetes objects:
- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_namespace]
regex: shop
action: keep
It needs a Kubernetes cluster, so it is not part of the lab. After relabeling, the __meta_ labels are dropped; the Service discovery page under Status shows each target's labels before and after.
Debugging a scrape on the Target health page
When a target is DOWN, a red box under its row starts with Error scraping target: and gives the reason, usually one of these:
| The error contains | Usual cause | What to check |
|---|---|---|
no such host | A misspelt host name, or no such container on obs-lab | The service name, and docker compose ps |
connection refused | Nothing listens on that port, or localhost by mistake | The address in targets |
server returned HTTP status 404 Not Found | The page is not at /metrics | metrics_path, found with curl |
context deadline exceeded | The scrape took longer than scrape_timeout | Why the target is slow |
The Last scrape column shows how long ago the last scrape ran and how long it took; a small button beside them adds the interval and timeout, such as every 15s and after 10s. A duration creeping towards the timeout is an early warning. You can also query it: scrape_duration_seconds is stored for every target, next to up.
Check yourself
You add a job with the target redis:6379, Redis's own port. Redis is running, but the target stays DOWN. Why, and what fixes it?
That port speaks the Redis protocol, not HTTP with a Prometheus /metrics page, so the scrape cannot succeed. Run a Redis exporter next to it and point the job at the exporter's port: Prometheus scrapes the translator, not the guest.
curl localhost:9100/metrics works on your laptop, but a job with the target localhost:9100 is DOWN with connection refused. Why?
Prometheus runs in its own container, where localhost means that container, and nothing in it listens on port 9100. Use the service name node-exporter:9100, which resolves on the obs-lab network.
You save prometheus.yml with a typo and reload without promtool. Does monitoring stop, and why check anyway?
No: the reload answers failed to reload config: and Prometheus keeps the old config. But at the next container restart, Prometheus would refuse to start with that file, so check it first.