Usage log
Sometimes you need to know who did what, and when: who deleted this
reservation, which client calls this resource a thousand times an hour,
which user has not signed in for months. The usage plugin writes one
entry per resource call, in a table of the database, where you can query
it with SQL.
An entry says:
- what was called: the method, the URL, the resource, the operation, the object, the query parameters;
- by whom: the user, the client’s addresses, its user agent, the referrer;
- when it was called, how long it took, and what status it answered;
- for the resources that ask for it, the body of the request.
Coming from Django: Django has no usage log of its own. This is what an audit or request-logging package provides (
django-request,drf-api-tracking’sLoggingMixin), or a middleware written by hand that saves a row per request. Unlike most of them, rind’s plugin never slows a request down: it writes in the background, in batches.
Turning it on
Turn on the usage feature of rind in your Cargo.toml:
rind = { version = "…", features = ["usage"] }
Then install the plugin in app():
#![allow(unused)]
fn main() {
// sample/src/lib.rs
.with(rind::usage::Usage::new().body::<booking_api::reservations::ReservationResource>())
}
The railway logs every call, and keeps the body of the calls to the reservations. The plugin reads no environment variable.
The plugin brings its own table, rind_usage, with its migration. Run
migrate once after installing it:
$ cargo run -p sample -- migrate
From then on, each call adds a row. For example, after a client changed a reservation:
SELECT timestamp, method, url, operation, status, user_id FROM rind_usage ORDER BY id DESC LIMIT 1;
timestamp | method | url | operation | status | user_id
------------------------+--------+----------------------+----------------+--------+---------
2026-11-27 10:00:00+00 | PATCH | /reservations/12 | partial_update | 200 | 3
Choosing what is logged
For each resource, the plugin uses one of four levels:
| Level | What is logged |
|---|---|
NoBody | every call, without its body (the default) |
Body | every call, with its body |
SkipReads | the writes only, not the lists, retrieves and GET actions |
Skip | nothing |
You choose the level in the application, when the plugin is built:
#![allow(unused)]
fn main() {
use rind::usage::{Level, Usage};
Usage::new()
.body::<ReservationResource>() // Body
.skip_reads::<StationResource>() // SkipReads
.skip::<RegionResource>() // Skip
.default_level(Level::NoBody) // the resources that say nothing
}
Or on the resource itself, with options(usage = "…") (see
Plugins). The values are "skip",
"skip_reads", "body" and "no_body":
#![allow(unused)]
fn main() {
#[resource(model = Region, path = "regions", out = RegionOut, /* … */ options(usage = "skip"))]
pub struct RegionResource;
}
When the application and the resource both say something, the application wins.
Bodies, and their secrets
A body is kept without its secrets. The fields named password, token
and secret are removed, at any depth, whatever their case. Add your
own with Usage::secret:
#![allow(unused)]
fn main() {
Usage::new().body::<ReservationResource>().secret("iban")
}
A body larger than 16 KiB is not kept: only its size is, as
{"truncated": 20480}. A body that is not JSON (a form with files) is
not kept either: only its size is, as {"not_json": 104857}.
The client’s address
The address logged is the client’s, as the server sees it. Behind a
proxy, that is the proxy’s address. The real client’s address is then
in the X-Forwarded-For header, which the proxy sets. But any client can
send that header, so the plugin only reads it from peers you trust:
#![allow(unused)]
fn main() {
// from these proxies only:
Usage::new().trusted_proxies(vec!["10.0.0.5".parse().unwrap()])
// from any peer, when every request comes through a proxy that sets it:
Usage::new().behind_proxy()
}
The entry’s ips then lists the addresses of X-Forwarded-For, then
the peer’s.
Written without slowing the requests
A request does not write its own entry. It puts the entry in a channel,
and a task writes the entries in batches: every second, or every 500
entries, whichever comes first. Change both with Usage::batches.
The channel has a bounded size (10,000 entries by default). When it is
full, because the database is slow or down, new entries are dropped and
counted: a request is never slowed by its own log. The plugin logs a
warning under the target rind::usage for the first entry dropped, and
then every 1,000. usage.dropped() gives the count. A batch that cannot
be written is lost, and logged as an error.
usage.flush().await waits until every entry logged so far is written:
use it in a test, or before a process stops. The plugin is a service, so
you find it with ctx.service::<rind::usage::Usage>(), or keep a clone
of it before installing it (its clones share one channel).
Where the entries go
By default, the entries go into the plugin’s own table, rind_usage.
The plugin adds a domain of the same name to the application: its
migration is applied with the application’s, by migrate (see
Migrations).
The table grows forever unless you prune it. The plugin adds a command for that, which deletes the entries older than a number of days (90 by default):
$ cargo run -p sample -- usage-prune --days 90
1234 entries deleted
Run it from a scheduled job or from cron.
A sink of your own
An application that has a usage table already (from a Django application it replaces, for example), or that wants its entries elsewhere, gives a sink of its own: a type that writes a batch of entries.
#![allow(unused)]
fn main() {
use rind::api::BoxFuture;
use rind::db::Db;
use rind::usage::{Entry, Sink, Usage};
struct Logs;
impl Sink for Logs {
fn write<'a>(&'a self, db: &'a Db, entries: &'a [Entry]) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move { /* insert into logs_usage, or push to Redis */ Ok(()) })
}
}
.with(Usage::new().sink(Logs))
}
With a sink of your own, the plugin’s table is not created, and the
usage-prune command is not added.
What is not logged
The routes that are not resources are not logged: /status, /ready,
/metrics, a plugin’s routes, and routes added by hand.
The usage log is a hook (see Plugins). A hook given before it that answers by itself stops the call before it reaches the usage log: this is what the cache does when it answers from its store. To log the calls the cache answers too, give the usage plugin before the cache.
Reference
Usage:: | Meaning |
|---|---|
new() | every call logged, without its body, into rind_usage |
.level::<R>(level) | what is logged of R |
.body::<R>(), .skip_reads::<R>(), .skip::<R>() | the same, for each level |
.default_level(level) | what is logged of the resources that say nothing (NoBody by default) |
.secret(field) | a field never kept from a body, at any depth |
.trusted_proxies(vec![…]) | X-Forwarded-For is read from these peers only |
.behind_proxy() | X-Forwarded-For is read from any peer |
.batches(batch, interval, capacity) | entries written by batch (500), at least every interval (1 s); past capacity waiting (10,000), they are dropped |
.sink(sink) | writes the entries with sink, not into rind_usage |
.dropped(), .written() | how many entries were dropped, and written |
.flush().await | waits until the entries logged so far are written |
Column of rind_usage | Meaning |
|---|---|
timestamp | when the call started |
method | GET, POST… |
url | the path and its query string |
resource | the resource’s path (stations) |
operation | list, retrieve, create, update, partial_update, destroy, or an action’s name |
object | the object’s id, for a call on one object |
params | the query parameters, as an object of lists |
status | the status answered |
duration | in microseconds |
user_id | the user, when signed in |
ips | the client’s addresses |
user_agent, referrer | the request’s User-Agent and Referer |
data | the body, when the resource’s level is Body |
| Command | Meaning |
|---|---|
usage-prune [--days N] | deletes the entries older than N days (90 by default) |
The rows of rind_usage are the model rind::usage::UsageRow, and the
default sink is rind::usage::TableSink. rind::usage::prune(db, days)
does what the command does.