0Pricing
MLOps Academy · Lesson

Expose Metrics with Prometheus

Track request rate, errors, and duration.

Expose Metrics with Prometheus is a free MLOps Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Logs vs Metrics

Logs tell the story of one request. Metrics are numbers aggregated over many requests, so you can see trends like rate and error count at a glance. 📈

Meet Prometheus

Prometheus is the standard tool for collecting these numbers. Your service exposes metrics, and Prometheus regularly reads them for you.

Pull, Not Push

Prometheus uses a pull model: it scrapes a metrics URL on your service every few seconds. Your job is just to expose that endpoint.

The Python Client

The prometheus_client library lets your Python service define and serve metrics with almost no boilerplate.

from prometheus_client import Counter, Histogram, start_http_server

A Counter Counts Up

A Counter only ever increases. It is perfect for totals like how many predictions your model has served since startup.

predictions = Counter("predictions_total", "Total predictions served")
predictions.inc()

A Histogram Times Things

A Histogram records a distribution, ideal for request duration. It buckets values so you can later read p50 and p99 latency.

latency = Histogram("predict_seconds", "Prediction latency in seconds")
with latency.time():
    model.predict(x)

Labels Add Dimensions

Labels split one metric by category, like model version or status. Then you can compare error rates per version side by side.

errors = Counter("errors_total", "Errors", ["model"])
errors.labels(model="churn-v3").inc()

Expose the /metrics Endpoint

Prometheus scrapes a path conventionally named /metrics. The client can serve it for you on its own port.

start_http_server(8000)
# metrics now live at http://localhost:8000/metrics

What a Metric Looks Like

On that endpoint each metric is plain text: a name, optional labels, and a value. Prometheus reads this format on every scrape.

predictions_total 1423.0
predict_seconds_bucket{le="0.1"} 1390.0

Configure the Scrape

Tell Prometheus where to look by adding your service as a target in its config. Then it pulls metrics on a fixed interval.

scrape_configs:
  - job_name: model-api
    static_configs:
      - targets: ["model-api:8000"]

Pick the Right Type

Rule of thumb: use a Counter for things that only grow, a Histogram for durations, and a Gauge for values that go up and down.

Quick Check

You want to track request latency so you can later read p99. Which Prometheus metric type fits?

Recap

You exposed a /metrics endpoint with prometheus_client, using Counters and Histograms plus labels. Prometheus now pulls live numbers from your model service. ✅

Frequently asked questions

Is the “Expose Metrics with Prometheus” lesson free?

Yes — the full text of “Expose Metrics with Prometheus” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.

What will I learn in “Expose Metrics with Prometheus”?

Track request rate, errors, and duration. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MLOps Academy?

No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Expose Metrics with Prometheus” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MLOps Academy lesson?

Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Structured Logs for Predictions
  2. Expose Metrics with Prometheus
  3. Build a Grafana Dashboard
  4. Alert on Latency and Error Spikes
← Back to MLOps Academy