Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

For any signal, fill in one line: "Record it as a ___, query it as ___, alert when ___ for ___."

The share of users flagged as churnersStep 1 of 4
  1. Record

    A Counter churn_predictions_total with label outcome, incremented on every prediction.

The trap is alerting on a single scrape. Short spikes are normal; the "for 30 minutes" part keeps alerts meaningful.

Drills

Answer fast — trust the rule.

quick drill

How does Prometheus usually collect metrics from the churn API?

quick drill

Which metric type fits "number of predictions served"?

quick drill

You need p95 prediction latency. Which metric type should record the timings?

quick drill

Why add a duration such as "for 10 minutes" to an alert rule?

Remember

  • The service exposes, Prometheus scrapes, Grafana shows and alerts.
  • For each signal: record as a type, query it, alert when it crosses a line for a while.
  • Trap: alerts on single samples, which bury real problems under noise.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWire the alert

Explain how a slow churn API ends up paging an engineer: what the service records, how Prometheus gets it, and what Grafana does with it. Include which metric type you would use for latency.