Prometheus and PromQL 3 cheatsheet

Prometheus and PromQL 3: Configuration, rules and alerts

4 common Prometheus and PromQL 3 tasks.

Scrape targets

A minimal prometheus.yml that scrapes itself and an application every 15 seconds.

global:
  scrape_interval: 15s

rule_files:
  - "rules/*.yml"

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: api
    metrics_path: /metrics
    static_configs:
      - targets: ["api-1:8080", "api-2:8080"]

Precompute an expensive query

A recording rule stores a query's result as a new metric, so dashboards stay fast.

groups:
  - name: api-rules
    rules:
      - record: job:http_requests:rate5m
        expr: sum by (job) (rate(http_requests_total[5m]))

Define an alert

Fire when the error ratio stays above 5% for ten minutes.

groups:
  - name: api-alerts
    rules:
      - alert: HighErrorRatio
        expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
        for: 10m
        labels:
          severity: page
        annotations:
          summary: "More than 5% of requests are failing"

Route alerts

An Alertmanager configuration that groups alerts and sends pages to one receiver.

route:
  receiver: team-email
  group_by: [alertname, job]
  routes:
    - matchers: [severity="page"]
      receiver: on-call
receivers:
  - name: team-email
    email_configs:
      - to: [email protected]
  - name: on-call
    webhook_configs:
      - url: https://example.com/pager

Was this page helpful?