Lesson 5 of 9 · 23m
Scrape Configuration and Service Discovery That Survive Deploys
How targets become series, what relabeling really does, and the scrape settings that matter when infrastructure changes underneath you.
Prometheus does not magically know your architecture. It discovers targets, rewrites their labels, scrapes them on an interval, and stores successful samples. When you understand that pipeline, mysterious duplicate instances and missing jobs stop being mysteries.
The target pipeline
scrape_configs:
- job_name: api
scrape_interval: 15s
scrape_timeout: 10s
static_configs:
- targets:
- api-1.internal:9100
- api-2.internal:9100For each target, Prometheus constructs labels such as __address__, then applies relabel_configs before scraping. Final target labels commonly include job and instance; metric labels from the exporter are then merged with target labels, with conflict behavior controlled by honor_labels.
A scrape failing does not erase history. It produces gaps and up == 0. That distinction matters during deploys and network partitions.
Relabeling is routing, not cosmetics
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
target_label: service
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
- source_labels: [__meta_kubernetes_pod_label_monitor]
regex: "false"
action: dropRelabeling can drop targets, keep targets, rewrite labels, or synthesize new ones. The dangerous part is that it happens before scraping — a bad rule can make Prometheus silently stop monitoring the very service you think is protected.
Use the Prometheus UI's service-discovery and target pages to compare discovered labels with final labels. Most relabel bugs are obvious when you inspect one target instead of reading YAML and hoping.
Static, file, and dynamic discovery
Static configs are fine for stable infrastructure. File service discovery is a useful middle ground: automation writes JSON target files and Prometheus reloads them without restarting. Native discovery for Kubernetes, EC2, Consul, and similar systems is powerful but can import far too much inventory.
A disciplined target set is more valuable than a comprehensive one. Monitor what you operate, not every object that happens to exist in the cloud account.
Intervals and timeouts are capacity decisions
global:
scrape_interval: 15s
evaluation_interval: 30sA 15-second interval gives decent resolution at a cost of four scrapes per minute per target. Lower intervals multiply ingestion and storage. Longer intervals make alerts sluggish and rates noisy. There is no universal right number; there is a bill and a responsiveness requirement.
scrape_timeout must be lower than the interval. A timeout equal to the interval can cause overlapping or skipped behavior depending on implementation details and load. If exporters regularly approach the timeout, reduce metric volume or fix the exporter — do not merely increase timeouts until the pipeline hides the problem.
Protect the scrape path
Metrics endpoints often expose detailed internals. Bind them to internal interfaces, restrict with network policy, and use authentication/TLS when crossing trust boundaries. Prometheus supports bearer tokens, basic auth, and TLS settings in scrape config; use real secrets management rather than committing plaintext credentials.
Field notes
upis not application health. It means the scrape succeeded; a deadlocked service can still return a beautiful/metricspage.- Keep target labels stable across restarts where possible. Churn creates many short-lived series and makes dashboards noisy.
- Test relabel changes against one environment before fleet rollout. A bad
droprule is silent data loss. - The scrape path needs its own monitoring: scrape duration, sample limit hits, target count, and failed scrapes.
Was this lesson helpful?