Skip to the workspace
Pebellslearn by building
Monitoring and Observability  ›  Capstone Project

Metrics with Prometheus and Grafana

Grafana 15 minThe service exposes metrics on an endpoint, Prometheus scrapes and stores them as time series, and Grafana turns queries over them into dashboards and alerts.

mlops / Metrics with Prometheus and Grafana

Explore each scene at your own pace.

01 · The rule

The rule

Metrics with Prometheus and Grafana

You now know what to watch on the churn API. The last step is the plumbing: getting numbers out of the running service, storing them over time, drawing them, and waking someone up when one crosses a line. Prometheus and Grafana are the most common open-source pair for this.

The rule

The service exposes, Prometheus scrapes, Grafana shows and alerts.

The service exposes. The Python client library adds counters and timers to your code and serves them as plain text on /metrics:

from prometheus_client import Counter, Histogram, start_http_server

PREDICTIONS = Counter("churn_predictions_total", "Predictions served", ["outcome"])
LATENCY = Histogram("churn_prediction_seconds", "Time to predict one user")

start_http_server(9100)   # /metrics on port 9100

@LATENCY.time()
def predict(features):
    risk = model.predict(features)[0]
    PREDICTIONS.labels(outcome="churn" if risk > 0.5 else "stay").inc()
    return risk

Three metric types cover most needs:

  • Counter — only goes up: predictions served, errors. You look at its rate.
  • Gauge — goes up and down: memory used, today's missing-value share.
  • Histogram — counts observations into buckets, so you can estimate percentiles such as p95 latency.

Prometheus scrapes. Prometheus pulls: every 15 seconds, say, it requests /metrics from each target and stores every value as a time series — a metric name plus labels, with a value at each timestamp. You query it with PromQL:

histogram_quantile(0.95, rate(churn_prediction_seconds_bucket[5m]))

That line reads: "the 95th-percentile prediction time over the last five minutes".

Grafana shows and alerts. Grafana connects to Prometheus as a data source, runs queries like the one above, and draws them on dashboards. Alert rules fire when a query crosses a threshold for a set time, and send a notification to chat, email or an on-call tool.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.