Prometheus is a powerful system monitoring and alerting toolkit that:
Step 1: Download Prometheus
Step 2: Install Prometheus
prometheus
and promtool
.Step 3: Configure Prometheus
prometheus.yml
. Here’s an example configuration:global:
scrape_interval: 15s # Set the scrape interval to 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] # The Prometheus server itself.
Step 4: Start Prometheus
./prometheus --config.file=prometheus.yml
http://localhost:9090
.Prometheus scrapes metrics from HTTP endpoints. Applications need to expose metrics in a format that Prometheus understands.
Step 1: Exporting Metrics
Example (Python)
pip install prometheus-client
from prometheus_client import start_http_server, Summary
import random
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
time.sleep(t)
if __name__ == '__main__':
start_http_server(8000)
while True:
process_request(random.random())
Step 2: Configure Prometheus to Scrape Your Application
prometheus.yml
configuration file:scrape_configs:
- job_name: 'python_app'
static_configs:
- targets: ['localhost:8000']
PromQL is a powerful query language used to aggregate and retrieve time-series data.
Basic Queries
up
up[5m]
sum(rate(http_requests_total[1m]))
http_requests_total{job="python_app"}
Step 1: Access Prometheus UI
Graph
tab in the Prometheus web UI.Step 2: Run a Query
rate(http_requests_total[5m])
Prometheus allows you to define alerting rules and integrates with Alertmanager for handling alerts.
Step 1: Define Alerting Rules
alert.rules.yml
:groups:
- name: example
rules:
- alert: HighErrorRate
expr: rate(http_requests_total{status="500"}[5m]) > 0.05
for: 10m
labels:
severity: page
annotations:
summary: "High error rate detected"
description: "Error rate is greater than 5% for the last 10 minutes."
Step 2: Configure Prometheus to Use the Alerting Rules
prometheus.yml
:rule_files:
- "alert.rules.yml"
Step 3: Install and Configure Alertmanager
alertmanager.yml
:global:
resolve_timeout: 5m
route:
receiver: 'email'
receivers:
- name: 'email'
email_configs:
- to: 'you@example.com'
from: 'alertmanager@example.com'
smarthost: 'smtp.example.com:587'
auth_username: 'alertmanager@example.com'
auth_identity: 'alertmanager@example.com'
auth_password: 'password'
Step 4: Start Alertmanager
./alertmanager --config.file=alertmanager.yml
Step 5: Configure Prometheus to Send Alerts to Alertmanager
prometheus.yml
:alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
Prometheus does not include advanced visualization capabilities. Instead, it integrates seamlessly with Grafana for advanced dashboarding.
Step 1: Install Grafana
Step 2: Start Grafana
Step 3: Add Prometheus as a Data Source
http://localhost:3000
, admin/admin).http://localhost:9090
) and save.Step 4: Create a Dashboard