Add counters, gauges and histograms to your code, so you can measure how it behaves.
Metrics are numbers about your system, measured over time: how many requests, how many errors, how long they took. Instrumentation means adding a few lines of code that produce those numbers.
A metric is a measurement with a name, a number, a timestamp and a few labels. For example: http_requests_total, with labels method=GET and status=500, was 1,284 at 10:15:00. A monitoring system collects this number again and again (every 15 or 60 seconds), so you get a line over time.
Instrumentation is how the numbers get created. Your code (or a library, or a proxy next to it) increments a counter on every request, sets a gauge for the queue size, and records every request time into a histogram. Tools such as Prometheus, StatsD, Datadog and OpenTelemetry then collect and store them.
| Type | Behaviour | Example | How you read it |
|---|---|---|---|
| Counter | Only increases | http_requests_total | As a rate: requests per second over the last 5 minutes |
| Gauge | Goes up and down | queue_length, memory_bytes | As the current value, or its min and max |
| Histogram | Counts per bucket, plus a sum and a count | request_duration_seconds | Percentiles estimated from the buckets (p95, p99) |
| Summary | Percentiles computed inside the app | request_duration (client-side p99) | Exact for one instance, but cannot be added across instances |
Google's SRE book names the four golden signals: latency, traffic, errors and saturation. RED is close to the first three, and USE adds the view from the machine side.
Labels split one metric into many time series, for example one per endpoint and status code. This is powerful, but each unique combination of label values is a separate series that must be stored. The number of unique combinations is called cardinality.
1. One sample of 1,000 request times
2. Labels multiply the number of time series
The request times come from a fixed formula, not a real system, and the label counts are illustrative. Real percentiles from a histogram metric are estimates, because they are read from the bucket edges.
requests = Counter("http_requests_total", labels=["method", "endpoint", "status"])
latency = Histogram("http_request_duration_seconds", buckets=[0.05, 0.1, 0.2, 0.5, 1, 2, 5])
def handle(request):
start = now()
response = run(request)
requests.labels(request.method, request.route, response.status_class).inc()
latency.observe(now() - start)
return responseuser_id or request_id creates a new time series for every value. A million users means a million series, and the monitoring system can run out of memory. Put ids in logs and traces instead.| Term | Meaning in easy words |
|---|---|
| Metric | A named number measured over time, such as requests per second |
| Instrumentation | Code (or a library) that records metrics, logs or traces from your application |
| Counter | A value that only goes up (and resets on restart). You read its rate |
| Gauge | A value that can go up and down, such as queue length |
| Histogram | Counts of values per bucket, used to estimate percentiles |
| Percentile (p95, p99) | The value below which that share of requests fall. p99 = 99 of 100 were faster |
| Label / tag | A key and value attached to a metric (for example endpoint=/pay) that splits it into series |
| Cardinality | The number of unique label combinations. High cardinality is expensive |
| Time series | One metric with one set of label values, stored as numbers over time |
| Scrape (pull) vs push | The collector reads the app's numbers on a schedule (pull), or the app sends them out (push) |
| SLI / SLO | A measured quality number (SLI), and the target you promise for it (SLO) |
| Choice | We gain | We lose |
|---|---|---|
| More labels | Finer detail (per endpoint, per region) | More series, more cost, slower queries |
| Fewer labels | A cheap and stable system | Harder to find which part is broken |
| Short scrape interval (5 s) | Sharper graphs, faster alerts | More data and load on the app and the store |
| Long scrape interval (60 s) | Low cost | Short spikes can fall between two samples |
| Histogram buckets | Percentiles across many instances | Estimates, and bad buckets give bad numbers |
| Summary (client percentiles) | Accurate for one instance | Cannot be combined across instances |