How to Instrument Metrics with the RED Method
Instrument a service with the RED method: track rate, errors, and duration per endpoint, choose the right metric types, and alert on the signals.
On this page
Introduction
Metrics are the numeric view of a service over time, and the ones worth emitting are the ones that describe health from the caller’s perspective. The RED method names three: rate, the requests per second; errors, the failing requests; and duration, how long the requests take. Per endpoint, these three say whether the service is healthy and where it is degrading, and they are the signals you alert on.
This tutorial instruments a service with the RED method: track rate, errors, and duration per endpoint, choose the right metric type for each, keep the labels bounded, and alert on the signals. The examples use a metrics client, and the method applies to any metrics backend.
Track rate, errors, and duration per endpoint
Record, per endpoint, the count of requests, the count of failures, and the distribution of durations. The count over time gives the rate; the failures give the error rate; the durations give the latency distribution. These three are the minimum for a service, and they are per endpoint so a problem in one endpoint is visible against the healthy others.
Keep the three together under the same labels, so a dashboard or an alert can select one endpoint and see all three. A rate metric labeled by route and a duration metric labeled differently cannot be joined, which fragments the view. Use a consistent label set so the signals for one endpoint line up.
import { Counter, Histogram, Registry } from 'prom-client';
const registry = new Registry();
const requests = new Counter({
name: 'http_requests_total', labelNames: ['route', 'status'], registers: [registry],
});
const duration = new Histogram({
name: 'http_request_duration_seconds', labelNames: ['route'], registers: [registry],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 2, 5],
});
function observe(route, status, seconds) {
requests.inc({ route, status: status + 'xx' }); // errors = the non-2xx series
duration.observe({ route }, seconds); // the latency distribution
}
Choose the right metric type
Use a counter for the request and error counts, because they only increase and the monitoring system computes the per-second rate from the increase. Use a histogram for the duration, because a latency is a distribution and the useful statistic, a percentile, needs the histogram’s buckets. A gauge is for a value that goes up and down, such as the number of in-flight requests or the queue depth.
The histogram buckets matter: they bound the resolution at the percentiles you care about. Set the buckets around your latency targets, so the p95 and p99 are resolvable, rather than a default that does not cover your range. A histogram with buckets in the wrong range reports percentiles that are rounded to the nearest bucket, which hides the change you want to see.
import "github.com/prometheus/client_golang/prometheus"
var requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Buckets: []float64{0.01, 0.05, 0.1, 0.5, 1, 2, 5}, // around SLO targets
}, []string{"route"})
// rate from a counter, percentiles from the histogram
var requests = prometheus.NewCounterVec(
prometheus.CounterOpts{Name: "http_requests_total"}, []string{"route", "status"})
Keep the labels bounded
A label multiplies the metric into a series per value, so an unbounded label, such as the raw path or the user id, creates an explosive number of series and overwhelms the backend. Label by the route template, such as /orders/:id rather than /orders/42, and by the status class rather than the exact status, so the series count is bounded by the number of routes times the number of classes.
The cardinality budget is finite: each series costs memory in the backend and in your process. Bound every label to a known set, and if a value is unbounded, do not use it as a label; put it in a log or a trace attribute instead, where it does not multiply the metric. Watch the series count as a metric itself, so a new high-cardinality label is caught before it saturates the backend.
Compute the right aggregation in the query
The rate is the increase of the counter per second, computed in the query as a rate over a window, not stored as a rate. The error rate is the rate of the non-2xx series divided by the rate of all series. The duration percentile is read from the histogram with a percentile function over the buckets. The metric stores the raw counts, and the query computes the statistic.
A common mistake is to record a pre-computed rate, which loses the samples and the resets. Store the counter and let the query compute the rate, so the window and the smoothing are chosen at query time and the same raw data answers several questions. The histogram likewise stores the observations, and the query computes the percentile.
# rate and error rate, computed at query time from the counters
rate(http_requests_total[5m]) # requests per second
sum(rate(http_requests_total{status!~"2.."}[5m])) # error rate
/ sum(rate(http_requests_total[5m]))
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) # p95
Alert on the signals without fatigue
Alert on a sustained error rate and on a duration percentile that exceeds the target for a sustained window, not on a single spike. A brief error is noise; a rate over five minutes that exceeds the budget is an incident. Use the metrics the instrumentation emits, so the alert is derived from the same data the dashboard shows, and set the threshold and window from the SLO.
Keep the alert count low and the signals meaningful, so each alert is actionable. An alert that fires for a normal fluctuation trains the team to ignore it, which is worse than no alert. Tie each alert to a symptom the caller would notice, from the RED metrics, and reserve the cause-level metrics for the dashboards.
# alert: error rate over 5 minutes exceeds 1%
sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.01
# for: 5m -> sustained, not a single spike
Test that the metrics are emitted
Test that a request increments the rate counter with the right labels, records the duration in the histogram, and that an error increments the error series. Test that the labels are bounded, by asserting the route label is a template and not a concrete path. Test that the series count stays within a budget as the test drives several endpoints, which catches an unbounded label.
# after driving a few requests, assert the metric output has bounded labels
curl -s http://localhost:3000/metrics | grep http_requests_total | head
# assert route is a template like /orders/:id, not /orders/42
Common pitfalls
Recording a pre-computed rate instead of a counter, a histogram with buckets outside the range you care about, unbounded labels from a raw path or a user id, alerting on a single spike so the alert is noisy, and a rate metric and a duration metric labeled inconsistently so they cannot be joined are the recurring mistakes. Counters and histograms, buckets around the targets, bounded labels, sustained-window alerts, and a consistent label set address them.
Conclusion
The RED method is the three signals a service emits per endpoint: rate, errors, and duration. Use a counter for the counts and let the query compute the rate, a histogram for the duration with buckets around the targets, and bounded labels by route template and status class. Alert on a sustained error rate and duration, and test that the metrics are emitted with bounded labels. Self-check by asserting a request increments the right series and the route label is a template. Then the caller’s view of the service is measured.
Sources: Prometheus: Metric types, Google SRE: Monitoring distributed systems, The RED method (Grafana).
FAQ
- What are the RED metrics?
- Rate, errors, and duration: how many requests per second, how many are failing, and how long they take. They are the three signals that describe a service from the caller's perspective, and they pair with the USE method for resources (utilisation, saturation, errors).
- Counter or gauge for the rate?
- A counter, which only increases, and the rate is computed by the monitoring system as the increase per second. A gauge that you set to a rate loses the resets and the samples, so a counter is the correct type for a monotonic count.
Report a problem with this articleOpens a pre-filled issue on GitHub.