Prometheus Tutorial: Metrics That Show What Your App Does
Quick answer Prometheus is a monitoring system that pulls numbers from your app on a schedule and stores them as time series, so you can ask how many orders failed in the last hour or how slow checkout became after a deploy. Your app exposes the numbers on a /metrics endpoint, Prometheus scrapes it, and you query the results with PromQL. This post builds that first half with Python: a small shop…
Prometheus is a monitoring system that collects numbers from applications on a regular basis and stores them as time series. These numbers can be queried to answer questions like how many orders failed in the last hour or how slow checkout became after a deployment. The application exposes these numbers on a /metrics endpoint, which Prometheus scrapes and then queries using PromQL.
This article demonstrates how to implement this first portion using Python, specifically for a small online shop in Karachi where orders come in throughout the day.
To illustrate, consider a small online shop in Karachi that handles five orders throughout the day. Ayesha buys a kurta and dupatta for Rs 12,400, Bilal purchases sneakers for Rs 7,800, Hina buys a handbag and wallet without a total, Ayesha returns a dupatta for Rs 3,100, and Bilal finally buys sneakers and socks for Rs 8,600. A small Python script defines two metrics: a Counter for orders placed, labeled by outcome, and a Histogram for the time each order takes to process, with specific buckets.
These metrics are served on http://localhost:8000/metrics in Prometheus exposition format, which can then be scraped by a Prometheus server.
Before implementing Prometheus, consider two key factors. First, determine if anyone will actually look at the numbers. Prometheus stores data and answers queries, but it sends no alerts until you create a rule for it. If the plan is only to check the dashboard after an outage, a log search can serve the same purpose with less infrastructure.
Second, check whether the application can expose metrics. Prometheus pulls data from your app, not data pushed to it. Therefore, a job that runs for a short duration and then exits requires the Pushgateway instead.
Prometheus also requires careful consideration of cardinality, the number of distinct label combinations. For example, a counter labeled by order status has two series: paid and failed. However, a counter labeled by customer ID would create a new series for every customer, causing the Prometheus server to become slow and expensive.
It's important to keep labels limited to a small set of known values and avoid using user IDs or emails in labels. Lastly, consider who will run the Prometheus server. It requires disk space, retention rules, and backups if historical data matters. If no one is responsible for these tasks, a managed metrics service might be a more suitable alternative.
In conclusion, adopt Prometheus when a service runs long enough to be scraped and there is someone who will act on its alerts. Skip it for a simple weekend script. The recommendation changes when labels grow without limit.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.