Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Metrics

Logs tell you what happened to one request. Metrics tell you how the whole application behaves over time: how many requests each resource gets, how long they take, how many fail, how many statements they run, how busy the connection pools are. The metrics plugin counts all of this and serves it at /metrics, in the format Prometheus reads. From there, Grafana or any tool that reads Prometheus can draw it and raise alerts.

Coming from Django: this is what django-prometheus does, with its middleware and its database wrapper. In rind it is one plugin: it counts the resource calls with a hook, and the statements with an observer (see Plugins). It also counts the statements of jobs and commands, not only those of requests.

Turning it on

Turn on the metrics feature of rind in your Cargo.toml:

rind = { version = "…", features = ["metrics"] }

Then install the plugin in app():

#![allow(unused)]
fn main() {
// sample/src/lib.rs
.with(rind::metrics::Metrics::new())
}

It reads no environment variable. Point Prometheus at the application’s /metrics.

What you get

After a few requests to the railway, /metrics answers:

GET /metrics
rind_requests_total{resource="stations",operation="list",method="GET",status="200"} 1
rind_requests_total{resource="stations",operation="partial_update",method="PATCH",status="200"} 1
rind_writes_committed_total{database="default",table="network_station"} 1
…

The answer is in the OpenMetrics text format (application/openmetrics-text).

The series fall into three groups.

The resource calls. rind_requests_total counts the calls, by resource, operation, method and status. rind_request_duration_seconds is a histogram of their times, by resource, operation and method. The resource label is the resource’s path (stations), and the operation label is the name the permission rules use (list, partial_update, or an action’s name).

A label is always a resource’s path, never a request’s path: there is one series per route, not one per object. GET /stations/1 and GET /stations/2 are both counted as resource="stations", operation="retrieve". This keeps the number of series small, which Prometheus needs.

The statements. rind_statements_total counts them, by database, kind (select, insert, update, delete, raw), table, and outcome (ok or error). rind_statement_duration_seconds is a histogram of their times. rind_rows_total counts the rows read by a select, or changed by a write. rind_writes_committed_total counts the writes that were kept, per table: a transaction that changes a table several times counts once for that table, and a rolled-back write does not count.

The connection pools. rind_db_connections says, for each server of each database, how many connections are open, how many are idle, and how many may be open at most. It is read each time /metrics is asked.

The durations are in seconds, in buckets from 1 ms to about 16 s.

Keeping /metrics private

/metrics is a route like the others: anyone who can reach the application can read it. It needs no credentials, and it is not in the OpenAPI document. Keep it private behind your proxy (a proxy that does not forward it), or move it to another path:

#![allow(unused)]
fn main() {
.with(rind::metrics::Metrics::new().path("/internal/metrics"))
}

Adding series of your own

An application can add its own series to the same registry, so that they are served at the same /metrics. The series types come from the prometheus-client crate, which your application then depends on, in the same version as rind (0.24).

Metrics is cheap to clone, and its clones share one registry. So you can register a series before installing the plugin:

#![allow(unused)]
fn main() {
use prometheus_client::metrics::counter::Counter;

let bookings = Counter::<u64>::default();
let metrics = rind::metrics::Metrics::new();
metrics.register("bookings", "Seats booked", bookings.clone());

let app = bare_app().with(metrics);
// Later, where a seat is booked:
bookings.inc();
}

The name is prefixed with rind_, like rind’s own series: this counter is served as rind_bookings_total.

The plugin is also a service, so code that runs inside a request can find it:

#![allow(unused)]
fn main() {
let metrics = ctx.service::<rind::metrics::Metrics>().unwrap();
}

Register each series once, when the application starts, not in each request.

What is not counted

The routes that are not resources do not go through the plugin’s hook: /metrics itself, /status and /ready, a plugin’s routes, and routes you added by hand with Api::axum or Api::actix. To count those, use your web framework’s own middleware.

Reference

SeriesLabels
rind_requests_totalresource, operation, method, status
rind_request_duration_secondsresource, operation, method
rind_statements_totaldatabase, kind (select, insert, update, delete, raw), table, outcome (ok, error)
rind_statement_duration_secondsdatabase, kind, table
rind_rows_totaldatabase, kind, table: rows read, or changed
rind_writes_committed_totaldatabase, table: writes kept, a transaction counted once per table
rind_db_connectionsdatabase, server (0 the primary, then the replicas), state (open, idle, max)

Hand-written SQL has an empty table label, since rind does not read SQL.

Metrics::Meaning
new()the series above, served at /metrics
.path(path)serves them at path instead
.register(name, help, metric)adds a series of the application’s own (its name prefixed with rind_)
.encode()the series as text, as /metrics answers them