Class MailAnsweringSpendTracker
- Namespace
- MailFathom.Infrastructure.Observability
- Assembly
- MailFathom.Infrastructure.dll
Admits a question against what answering has cost the deployment this period, and publishes both as instruments.
public sealed class MailAnsweringSpendTracker : IMailAnsweringSpendLedger
- Inheritance
-
MailAnsweringSpendTracker
- Implements
- Inherited Members
Remarks
The period is the fixed window PeriodStartAt(DateTimeOffset) places, and it is the key the ledger's row is held under. Because the window is a function of the clock rather than of when a process started, every replica derives the same key, nothing has to be stored to say where a period begins, and an instance that answered nothing for a day is not owed the windows that passed while it was idle. Nothing here schedules a roll-over: a later window is simply a different key.
Deployment-wide and durable, through IMailAnsweringSpendPeriodStore. It is where this diverges from what it used to be, and the reason is the one ADR 0031 records: a ceiling worded as the deployment's and counted in each process is multiplied by the replica count, and the two ceilings here are the two that cost money. What that costs is two writes for a run that would not otherwise have opened one — the admission, and the charge as the run ends — beside a run that is already about to spend a provider's tokens. Not a write per provider call, which is the shape that ADR refused and which a tool loop turning once per message of a backfill is what makes expensive.
A refusal is counted as well as measured, because the two questions an operator asks are opposite: the counter says how often the ceiling was reached, and the gauges say how close the deployment is to reaching it now. A ceiling that is met constantly is a ceiling to raise or a client to look at, and neither is visible from a single number.
The gauges publish what this instance last read of the deployment's figures rather than a reading taken when they are collected, because a gauge's callback is synchronous and the figures are a row. Every admission and every spend refreshes them, so an instance answering questions publishes current numbers and one answering none publishes what it last saw — which is the honest reading and is what a deployment aggregating several replicas' instruments has to know about them.
Only the first refusal of a period is written to the log, and that bound is the point rather than tidiness: a client that keeps asking is exactly what spends a period's allowance, so a line per refusal would put the log's volume on how enthusiastic that client is. The counter carries how often it happened; the line says that it started. What the line names is the ceiling rather than the counts, because the replica writing it need not be the one that spent the period.
Nothing recorded here is mail or derived from it. The values are a run count and a token count, and the one tag is an outcome from a closed set of two — which is a cardinality rule as much as a privacy one, since anything per caller or per question would open a time series that grows with use.
Constructors
- MailAnsweringSpendTracker(MailAnsweringPeriodBounds, IServiceScopeFactory, TimeProvider, ILogger<MailAnsweringSpendTracker>)
Initializes a tracker over the deployment's ledger, and the instruments it publishes through.
Methods
- Read()
Reads what the current period has cost, as this instance last observed the deployment's figures.
- RecordSpendAsync(ChatTokenUsage, CancellationToken)
Adds what one run consumed to the current period.
- TryAdmitRunAsync(CancellationToken)
Takes an allowance for one run, if the current period has one left.