Sub topic 7
Configuring Prometheus to scrape targets
This chapter assumes you can edit YAML, manage systemd services, and already have node_exporter listening on port 9100 on at least one server. It also assumes you know the pull model: Prometheus fetches metrics over HTTP from endpoints that exporters expose. Here you will write the configuration that tells Prometheus what to fetch, how often, and with which labels.
What a scrape is
A scrape is one HTTP GET request from Prometheus to a target's metrics endpoint. A target is a single host:port address. By default Prometheus requests /metrics over HTTP, parses the text response, attaches labels, and stores each sample with the scrape timestamp.
Prometheus does this on a fixed schedule for every target. The configuration file, normally prometheus.yml, defines the schedule and the target list.
The structure of prometheus.yml
The file has a few top-level sections. This chapter focuses on global and scrape_configs. The other two connect to later chapters: rule_files for recording and alerting rules, and alerting for Alertmanager.
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
- rules/*.yml
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
- job_name: node
static_configs:
- targets: ['server1.example.com:9100', 'server2.example.com:9100']The global section
scrape_intervalis how often each target is scraped. The default is 1 minute. This file uses 15 seconds, a common choice for host metrics.evaluation_intervalis how often rules are evaluated. It does not affect scraping.scrape_timeoutis how long Prometheus waits for a response. The default is 10 seconds. It must not exceed the scrape interval.
Settings in global are defaults. Any job can override them.
Jobs and targets
A job is a group of targets that serve the same purpose, such as all nodeexporter instances. Each entry in `scrapeconfigs needs a unique job_name. The job name becomes the job` label on every series from that job.
static_configs lists targets by hand. Every target in the list is scraped. Each target also gets an instance label set to its host:port. That is why queries such as avg by (instance) can separate servers.
The first job in the example makes Prometheus scrape itself. This is a quick way to confirm that scraping works before you add other servers.
Labels added during a scrape
Prometheus attaches two labels to every scraped series:
| Label | Value | Example |
|---|---|---|
| `job` | The `job_name` of the scrape config | `node` |
| `instance` | The target address | `server1.example.com:9100` |
It also writes synthetic series for every scrape. The most important is up, which is 1 when the scrape succeeded and 0 when it failed. Others include scrape_duration_seconds and scrape_samples_scraped. You do not configure these. They exist for every target, which makes them reliable health signals.
You can add your own labels to a group of targets. Use this to record the environment or role:
scrape_configs:
- job_name: node
scrape_interval: 30s
scrape_timeout: 10s
static_configs:
- targets: ['server1.example.com:9100']
labels:
env: production
role: web
- targets: ['server2.example.com:9100']
labels:
env: staging
role: databaseThe labels block applies to the targets in that group only. Later queries can then filter with {env="production"}. Here the job overrides scrape_interval to 30 seconds. The timeout stays below the interval.
Keep the number of custom label values small. Each distinct label combination creates a separate time series, which costs memory and disk.
Other scrape options
Three per-job settings come up often:
metrics_pathchanges the URL path. The default is/metrics.schemeishttporhttps. The default ishttp.basic_authorauthorizationsupplies credentials if the target requires them.
- job_name: app
metrics_path: /internal/metrics
scheme: https
basic_auth:
username: prometheus
password_file: /etc/prometheus/app_password
static_configs:
- targets: ['app1.example.com:8443']Use password_file rather than a plain password, so the secret stays out of a file you might commit to version control.
Targets from files
A static list means editing prometheus.yml and reloading every time a server is added. File-based service discovery avoids this. Prometheus reads target lists from separate files and picks up changes on its own.
scrape_configs:
- job_name: node
file_sd_configs:
- files:
- /etc/prometheus/targets/node-*.ymlEach target file contains a list of groups:
- targets:
- server1.example.com:9100
- server2.example.com:9100
labels:
env: productionConfiguration management tools such as Ansible can generate these files. Prometheus re-reads them when they change, with no reload needed. Files may be YAML or JSON.
Validating and applying changes
A syntax error in prometheus.yml prevents a clean start and causes a failed reload. Always check the file first with promtool, which ships with Prometheus:
promtool check config /etc/prometheus/prometheus.ymlThe command also checks any rule files listed in rule_files. Once the check passes, reload without restarting. A reload keeps the stored data in memory and avoids a gap in scraping.
# Option 1: send SIGHUP to the process
sudo systemctl kill -s HUP prometheus
# Option 2: HTTP endpoint (requires the --web.enable-lifecycle flag)
curl -X POST http://localhost:9090/-/reloadIf the new file is invalid, Prometheus logs the error and keeps running with the old configuration. Check journalctl -u prometheus to see whether the reload took effect.
Confirming the result
Open http://localhost:9090/targets in a browser. Each target shows a state of UP or DOWN, the time since its last scrape, and the last error. You can also run this query in the expression browser:
up{job="node"}A value of 1 means the last scrape worked. A value of 0 means it failed. The /targets page shows the reason for the failure, such as connection refused or context deadline exceeded.
Common mistakes
- Including the scheme in a target. Write
server1:9100, nothttp://server1:9100. Set the scheme withscheme:. - Setting a timeout longer than the interval. Prometheus rejects the configuration.
- Bad indentation.
targetsmust sit insidestatic_configsas a list item.promtoolreports the line. - Duplicate job names. Each
job_namemust be unique. - Forgetting the firewall. The Prometheus server must reach each target on port 9100. A DOWN state with a timeout often points to a blocked port.
Exercise
A job has scrape_interval: 15s and scrape_timeout: 30s. What happens when you run promtool check config, and how do you fix it?
Show answer
The check fails, because the scrape timeout cannot be longer than the scrape interval. Either lower the timeout to 15s or less (the default of 10s is fine), or raise the interval to at least 30s.
Exercise
You add a new server to a job that uses static_configs. After saving the file, which two steps should you take, and how can you confirm the target is healthy?
Show answer
First run promtool check config /etc/prometheus/prometheus.yml, then reload Prometheus with SIGHUP or curl -X POST http://localhost:9090/-/reload. Confirm health on the /targets page, or by querying up{instance="newserver:9100"} and checking that it returns 1.