AlgoViz

Prometheus

medium 28 min

Prometheus is an open source monitoring server.

A small scrape, store and rate

Read it, run it, change a line and run it again.

Loading editor…

A model of three ideas: labelled series, a rate that survives restarts and extrapolates, and an alert with a waiting time. Runs without Prometheus. The extrapolation follows the main rule of the server in simplified form. Client code is in the variants.

The same in Python
# pip install prometheus-clientimport random, timefrom prometheus_client import Counter, Gauge, Histogram, start_http_serverREQUESTS = Counter("shop_requests_total", "Requests served", ["method", "status"])IN_FLIGHT = Gauge("shop_in_flight", "Requests being served")DURATION = Histogram("shop_request_seconds", "Request duration",                     buckets=(0.05, 0.1, 0.25, 0.5, 1, 2.5))def handle():    with IN_FLIGHT.track_inprogress(), DURATION.time():        time.sleep(random.random() / 5)    REQUESTS.labels(method="GET", status="200").inc()if __name__ == "__main__":    start_http_server(8000)          # http://localhost:8000/metrics    while True:        handle()
The same in YAML
# Kubernetes: scrape pods that ask for it, and drop a costly labelscrape_configs:  - job_name: pods    kubernetes_sd_configs:      - role: pod    relabel_configs:      - source_labels: [__meta_kubernetes_pod_label_metrics]        regex: "on"        action: keep      - source_labels: [__meta_kubernetes_namespace]        target_label: namespace    metric_relabel_configs:      - regex: session_id        action: labeldropremote_write:  - url: https://metrics.example/api/v1/push
The same in Shell
# Are the files valid?promtool check config prometheus.ymlpromtool check rules rules/*.yml# Which metrics have the most series?promtool tsdb analyze /var/lib/prometheus# Ask from the command linepromtool query instant http://localhost:9090 'up == 0'