Skip to main content

Module metering

Module metering 

Source
Expand description

Append-only metering log (Phase 2 design §5).

One JSON object per authenticated request, appended to the log file. The log is never served over HTTP and no field of it is echoed in a response: metering is a server-side accounting concern, and an agent learns its own usage from the status code and the record count, not from a usage header.

Each record carries the key’s plan (paid-path plan S1), which is what makes the log readable as billing input: a key’s monthly counters are read straight off the file — count its records in the month, with POST /v1/index lines as index operations and the rest as queries — and the plan attributes them. Since S3 that count is also the quota: the auth layer refuses a hosted key’s query when its count for the current UTC month has reached its plan’s limit (see Metering::usage_in_month and crate::limits). The rule stays log-shaped — a month’s usage is what a text search of the file would find — because the log is the only metering store there is.

§Deviation from the design

The design places metering at the request boundary so that every request is counted, including unauthenticated ones. Metering runs inside the auth middleware here instead (see crate::auth), so a 401 is not metered. Rationale: a 401 carries no key_id, and a metered record without a key_id cannot be attributed to any key — it would only inflate the row count with un-billable noise that no key can ever be blamed for. Rejected requests are still observable: they appear in the process log as 401s, and repeated rejections are visible in any reverse-proxy access log.

Structs§

Metering
Appends Records to a log file, one JSON object per line.
MonthUsage
One key’s usage in one month, counted from the log.
Record
One metered request.

Functions§

current_month
The current UTC calendar month as YYYY-MM — the prefix a record’s ts carries when it falls in this month. “Month” in every limit means this month (see crate::limits).
now_rfc3339_millis
Formats time as RFC 3339 UTC with millisecond precision.