Scraping and Service Discovery
What You'll Learn
You'll learn how Prometheus actively pulls metrics from HTTP endpoints and automatically discovers targets to scrape across your infrastructure.
The Pull-Based Model
Unlike push-based systems where applications send data to a central collector, Prometheus scrapes metrics—it actively pulls them from HTTP endpoints at regular intervals. Each target exposes a /metrics endpoint that Prometheus requests, receiving text-formatted time-series data in response.
Think of Prometheus as a mail carrier with a route: instead of waiting for letters to arrive at the post office, it walks a predefined path, collecting mail from each mailbox (target) at scheduled times.
How Scraping Works
Prometheus configuration defines scrape intervals (e.g., every 15 seconds) and target endpoints. When the interval fires, Prometheus sends an HTTP GET request to each target's metrics endpoint. The target responds with current metric values, which Prometheus stores with a timestamp.
scrape_configs:
- job_name: 'my-app'
scrape_interval: 15s
static_configs:
- targets: ['localhost:8080']
Service Discovery
Hardcoding targets doesn't scale in dynamic environments like Kubernetes. Service discovery automatically detects scrape targets as they appear and disappear. Prometheus integrates with platforms (Kubernetes, Consul, EC2) to query their APIs and maintain an up-to-date list of targets.
For example, Kubernetes service discovery finds all pods with specific labels or annotations, automatically adding them to Prometheus's scrape list without manual configuration changes.
Why This Matters
The pull model gives Prometheus control over scraping—it detects failed targets, applies rate limiting, and avoids overwhelming the monitoring system. Service discovery ensures you monitor ephemeral infrastructure without constant configuration updates.
Key Takeaway: Prometheus pulls metrics from HTTP endpoints at regular intervals and uses service discovery integrations to automatically maintain its list of scrape targets in dynamic environments.