Stage 6 · Operate
Metrics with Prometheus
Scrape Configuration
Configuring scrape_configs, relabel_configs, scrape intervals, timeouts, and target labels.
Scrape Config Structure
The scrape_configs section in prometheus.yml defines how Prometheus discovers and scrapes targets. Each entry is a job that groups targets sharing the same collection configuration.
scrape_configs:
- job_name: "node-exporter"
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- "10.0.0.1:9100"
- "10.0.0.2:9100"
labels:
environment: "production"Intervals and Timeouts
Scrape interval controls how often Prometheus pulls metrics from a target. Timeout controls how long Prometheus waits for a response. Both have global defaults that can be overridden per job.
| Setting | Default | Recommended Range |
|---|---|---|
| scrape_interval | 15s | 10s - 60s |
| scrape_timeout | 10s | 5s - scrape_interval |
| evaluation_interval | 15s | 15s - 60s |
If scrape_timeout exceeds scrape_interval, Prometheus may start a new scrape before the previous one finishes, causing overlapping requests and wasted resources.
Static Configs
Static configs list targets directly in the configuration file. This is useful for infrastructure that does not change frequently, such as databases, message queues, or dedicated monitoring endpoints.
static_configs:
- targets:
- "postgres-primary:5432"
- "postgres-replica:5432"
labels:
team: "database"
environment: "production"Relabel Configs
Relabel configs modify labels before a target is scraped. They run on the __address__ label and can add, remove, or transform labels. This is the primary mechanism for enriching targets with metadata.
relabel_configs:
- source_labels: [__address__]
regex: "(.+):\d+"
target_label: instance
replacement: "$1"
- source_labels: [__meta_kubernetes_pod_label_team]
target_label: teamMetric Relabeling
Metric relabel configs run after metrics are scraped but before they are stored. They can drop metrics, rename labels, or modify label values. This is useful for filtering noisy metrics or fixing label inconsistencies.
metric_relabel_configs:
- source_labels: [__name__]
regex: "go_.*"
action: drop
- source_labels: [le]
target_label: bucket
replacement: "$1"Dropping unwanted metrics with metric_relabel_configs before they hit storage is far cheaper than storing them and dropping them at query time. Use this aggressively to control costs.
Best Practices
- Use consistent scrape intervals across jobs to simplify PromQL queries.
- Set scrape_timeout to 80% of scrape_interval to leave headroom.
- Add job and environment labels to every target for consistent filtering.
- Use file_sd_config for dynamic environments instead of static_configs.
- Monitor Prometheus self-monitoring metrics like up and scrape_duration_seconds.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.