ENع
←Monitoring
Lesson 17 · Operations

Monitoring and metrics

12 min10 steps5 questionsAdvanced
About this lesson

You cannot fix what you cannot see. Follow one number from a shop's Python code to an engineer's phone: the app counts its failed requests, Prometheus pulls the count every 15 seconds and stores each reading in a time series, a query turns the counter's climb into errors per second, and when a bad release pushes that rate from 0.2 to 3 per second, an alerting rule fires and Alertmanager pages the on-call engineer. Then see the four golden signals worth watching first.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 17 · Interactive
01 / 10

Watch the numbers

Monitoring means collecting numbers about a running service, called metrics, every few seconds. A tool stores them over time, draws graphs and wakes a person when a number crosses a line. Here Prometheus watches a shop's web app.

service  → counts things in its code (metrics)
monitor  → collects them every few seconds
         → stores them over time
         → graphs them, checks rules
person   ← woken only when a rule trips
Loading figure…
or ← →
Read the lesson step by step
  1. Watch the numbers

    Monitoring means collecting numbers about a running service, called metrics, every few seconds. A tool stores them over time, draws graphs and wakes a person when a number crosses a line. Here Prometheus watches a shop's web app.

    service  → counts things in its code (metrics)
    monitor  → collects them every few seconds
             → stores them over time
             → graphs them, checks rules
    person   ← woken only when a rule trips
  2. Count inside the code

    The app keeps its own numbers. Lines 1 to 3 use the prometheus_client library to make a counter: a metric that only goes up, and resets to zero when the app restarts. Its label, code, splits it by HTTP status, such as 200 or 500.

    pip install prometheus-client
    
    import prometheus_client as prom
    reqs = prom.Counter('http_requests',
        'Requests served', ['code'])
    
    counter  only goes up (0 again on restart)
    gauge    goes up and down
    histogram  counts values into buckets
  3. Show them on /metrics

    Line 4 starts a small web server on port 8000. Its /metrics page lists every metric as plain text, one line per label value. Each failed request runs line 5, which adds one: the 500 line now reads 120.

    GET http://localhost:8000/metrics
    
    # HELP http_requests_total Requests served
    # TYPE http_requests_total counter
    http_requests_total{code="200"} 5880.0
    http_requests_total{code="500"} 120.0
    
    the library adds _total to the name
  4. Prometheus pulls

    Prometheus fetches the numbers itself. Every 15 seconds it requests /metrics and reads the reply; this is called a scrape. If a scrape fails, Prometheus sets a metric called up to 0 for that app, which can raise an alert too.

    # prometheus.yml
    global:
      scrape_interval: 15s     # default: 1m
      evaluation_interval: 15s
    scrape_configs:
      - job_name: shop
        static_configs:
          - targets: ['shop:8000']
    
    up{job="shop"}  1 = scrape worked, 0 = failed
  5. A time series grows

    Each scrape saves one sample, a time and a value. The samples for one metric name with one set of labels form a time series. After three scrapes, the 500 series holds 120, 123 and 126.

    series  http_requests_total{code="500"}
    
      t =  0 s   120
      t = 15 s   123
      t = 30 s   126
    
    one name + one label set = one series
    code="200" is a different series
  6. The rise per second

    A counter's total means little; how fast it climbs matters. From 123 to 126 in 15 seconds is 3 ÷ 15 = 0.2 errors per second. PromQL, Prometheus's query language, works this out with rate().

    (126 − 123) ÷ (30 s − 15 s)
      = 3 ÷ 15 = 0.2 errors per second
    
    PromQL:
      rate(http_requests_total{code="500"}[1m])
      per-second rise, averaged over the last minute
  7. A bad release

    A new release breaks checkout. Line 5 now runs far more often, and the next two scrapes read 171 and 216: up 45 every 15 seconds, or 3 errors per second, fifteen times the normal rate.

      t =  0 s   120 
      t = 15 s   123   +3
      t = 30 s   126   +3
      t = 45 s   171   +45
      t = 60 s   216   +45
    
    before  3 ÷ 15 = 0.2 /s
    after  45 ÷ 15 = 3 /s   (× 15)
  8. The rule trips

    An alerting rule checks rate > 1 per second every 15 seconds. It is true now, so the alert turns pending. Its for: 1m waits a whole minute; the rate is still high, so it fires. The wait ignores short blips.

    groups:
      - name: shop
        rules:
          - alert: HighErrorRate
            expr: rate(http_requests_total{code="500"}[1m]) > 1
            for: 1m
    
    ok → pending (true once) → firing (true for 1m)
  9. Page the on-call

    Prometheus sends the firing alert to Alertmanager. It groups related alerts, drops duplicates and routes them, here by paging the on-call engineer's phone. A person now looks, and can roll the bad release back.

    HighErrorRate firing
      → Alertmanager
          group      related alerts in one message
          dedupe     one page, not one per check
          route      to the right receiver
      → page the on-call engineer
  10. What to measure

    Google's SRE book names four golden signals to watch first. Latency, how long requests take, is usually a histogram. Traffic and errors are counters, as here. Saturation, how full the system is, is a gauge: a number that goes up and down.

    four golden signals (Google SRE book)
      Latency     how long requests take     histogram
      Traffic     how many requests come in  counter
      Errors      how many requests fail     counter
      Saturation  how full the system is     gauge
    
    our errors counter is one of the four

Quiz Unlocks after the last step.