Prometheus functions on a pull model, where the Prometheus server actively scrapes metrics endpoints from configured targets at regular intervals. Its core architecture involves a time-series database for storing scraped data, an HTTP server for exposing its own metrics and API, a scraper component, and a rule processing engine for alerts and recording rules. Applications and infrastructure components don't push metrics to Prometheus; instead, they expose an HTTP endpoint (typically /metrics) where Prometheus can fetch them. This is often achieved using 'exporters' (e.g., Node Exporter for host metrics, cAdvisor for container metrics) or by instrumenting applications directly with client libraries. For short-lived jobs, a Pushgateway can act as an intermediary, allowing transient services to push metrics to it, which Prometheus then scrapes.
The process of scraping is fundamental: Prometheus reads its scrape_configs from prometheus.yml, which define jobs, intervals, and target endpoints. For each target, it makes an HTTP request to its /metrics path, retrieves the data in Prometheus text format (or OpenMetrics), and stores it as time-series data, adding relevant labels. Each scraped metric includes a timestamp, a value, and a set of key-value pairs called labels that uniquely identify the characteristics of that metric (e.g., instance="webserver-01", job="api-service"). This label-based data model is incredibly powerful for querying and aggregating metrics.
In dynamic environments like Kubernetes clusters or cloud deployments, manually configuring every scrape target in prometheus.yml is impractical and error-prone. This is where service discovery comes in. Prometheus integrates with various service discovery mechanisms (e.g., Kubernetes API, AWS EC2, Consul, DNS) to automatically discover and track scrape targets. Instead of static IP addresses, you configure Prometheus to query a service discovery backend. This backend then provides a list of potential targets, which are then processed by Prometheus's relabeling rules. Relabeling allows you to dynamically modify target labels, filter targets, or even alter the scrape endpoint path based on metadata provided by the service discovery system, ensuring Prometheus always knows what to scrape, even as your infrastructure scales and changes.
Key Takeaways
- Prometheus uses a pull model, actively scraping metrics from targets.
- Targets expose metrics via HTTP endpoints (e.g.,
/metrics), often facilitated by exporters. - Service discovery automates finding and tracking targets in dynamic environments.
- Relabeling rules are crucial for transforming discovered targets and their metadata into valid scrape configurations.
Code Example
scrape_configs:
- job_name: 'node_exporter'
# Example of static targets, useful for fixed infrastructure.
# In a dynamic environment, service discovery would generate these.
static_configs:
- targets: ['localhost:9100', 'server-02:9100']
labels:
group: 'test'
# Defines how often Prometheus should scrape this job.
scrape_interval: 15s
scrape_timeout: 10s
# Example of a Kubernetes service discovery config (simplified).
# Prometheus automatically discovers pods/services based on roles.
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
# Relabeling rules would typically follow here to filter, rewrite labels,
# and set the correct __address__ for scraping based on pod metadata.How this code works
This configuration defines how Prometheus discovers and collects metrics from various services. The first section sets up a scrape_configs entry with job_name: 'node_exporter'. It uses static_configs to explicitly list targets like localhost:9100 and server-02:9100, which is ideal for fixed infrastructure. These targets also get a label group: 'test' for easy organization. scrape_interval: 15s tells Prometheus to try fetching data every 15 seconds, and scrape_timeout: 10s ensures it stops waiting for a response after 10 seconds, preventing hung scrapes.
The second scrape_configs entry, job_name: 'kubernetes-pods', demonstrates dynamic service discovery. It uses kubernetes_sd_configs with role: pod, instructing Prometheus to automatically discover all running pods in a Kubernetes cluster by watching its API. A subtle but crucial point for beginners is that while Prometheus discovers these pods, the example mentions that relabeling rules would typically follow. These rules are essential for filtering specific pods, rewriting labels, and especially for setting the correct __address__ for Prometheus to actually scrape the pod's metrics endpoint, as the raw discovery data isn't always directly scrapable.