record

suspend fun record(updates: List<MetricUpdate>): MetricExportResult

Apply a batch of updates in order, then make one durable write per metric kind the batch touched — rather than one per update.

Each mutator on this exporter re-encodes every series of its kind and writes the result, so N single-point calls cost N whole-map encodes and N writes, and an SDK export of N points through KuiltMetricExporter grew quadratically in the series count (#3284). A batch pays that fixed cost once per kind: a batch of a thousand sum increments is one encode of the sums and one write.

The durability contract is unchanged — it is the single-point mutators' contract, stated for a batch, and those mutators are now this method with one update:

  • Returns MetricExportResult.Success only once every kind the batch touched is durably written. Nothing is held back waiting for more updates; the batch is whatever the caller had in hand, and it is on disk before this returns.

  • A kind whose persisted series no read has joined yet — recover was refused, or never called — is read first, or, inside the kind's backoff window (see recover), refused without a read. While that read is refused or skipped, the batch's updates of that kind are not counted: not applied, and the kind's write is never attempted. The other kinds go ahead. The result is a MetricExportResult.Partial whose uncounted is exactly those updates, in batch order, so recording that list again counts each of them once; its causes holds a MetricKindUnreadException per unread kind.

  • Every write of a kind that was read is attempted, even after one is refused. Stopping at the first refusal would leave the later kinds off disk, which applying the updates one by one never does. A refused write makes the result a MetricExportResult.Partial too, with the store's refusal among its causes, after the unread kinds' and in the fixed kind order sums, double sums, gauges, cardinalities, histograms.

  • A refused write means "not yet durable", not "not counted": its updates stay in memory, are not in uncounted, and the next successful write of a refused kind persists them. So the batch is not atomic on disk — a kind the store took is durable even when another kind was refused — and retrying an update of a refused write counts it twice, exactly as retrying an incrementSum whose write was refused does.

  • On health each kind's write is its own turn: a batch counts one turn per kind it touched, and a partial refusal moves both acknowledged and failed. An unread kind's turn counts as failed. A refused batch is no turn of any kind and moves nothing.

In memory the exporter ends where applying updates one by one through incrementSum, incrementSumDouble, setGauge, addCardinality and recordHistogram — or, for a MetricUpdate.AddExponentialHistogram, through a batch of that one update — would leave it, eviction included: the maxMetrics cap is decided per update, in order. On disk, a touched kind holds its state as of the end of the batch. The one place that differs from the single-point sequence is a kind whose series a later update of another kind evicted: written one by one, the evicted series lingers on disk until that kind's next write; here it is already gone. A kind the batch never touched is not rewritten, as before.

An empty updates is MetricExportResult.Success and touches neither memory nor the store. Each MetricUpdate validates its own arguments at construction. Before any update is applied, the batch is checked twice more, in this order:

  1. What only this exporter's configuration can judge — a MetricUpdate.AddExponentialHistogram coarser than histogramScale, or one whose count lists the caller has since made negative — is checked for every update and thrown as IllegalArgumentException, since it is a caller's mistake. Nothing in memory or on disk changes.

  2. Whether an update would carry a sum or histogram series' total over every replica past Long.MAX_VALUE is checked for the whole batch, counting its earlier updates to the same series, against the series as they stand. For a MetricUpdate.IncrementSum that is its sum series' total — every replica's slot, a peer's merged in included — which is the value the OTLP bridge exports (#3445); this replica's slot is part of it (#3318). For a MetricUpdate.RecordHistogram or MetricUpdate.AddExponentialHistogram it is the series' total count — summed over every replica's slots, a peer's merged in included, across both signs' buckets and the zero counter — which is the count the OTLP bridge exports (#3403); and with it this replica's slot in each bucket the update touches, in the zero and overflow counters, and the sum of counts that fold onto one bucket (#3354). A weighted count can get there on its own. Such a batch is refused: it returns a MetricExportResult.Refused whose overflow names the series, a MetricSumOverflowException or a MetricHistogramOverflowException, and nothing from it is applied or written, the kinds that were read included. The refusal is logged at WARN the first time a series causes one and at DEBUG after that.

So neither a bad histogram scale nor an overflow leaves a batch applied half-way, and the one-by-one equivalence above holds for every batch this accepts.

Throws

if an update holds an exponential histogram whose scale is below histogramScale; nothing in the batch is applied.

Samples

val exporter = WarpMetricExporter(
    replica = us.tractat.kuilt.crdt.ReplicaId("server-1"),
    store = InMemoryDurableStore(),
)
val ok = MetricKey("http.requests", MetricKind.SUM, mapOf("status" to "200"))
val notFound = MetricKey("http.requests", MetricKind.SUM, mapOf("status" to "404"))
val inFlight = MetricKey("http.in_flight", MetricKind.GAUGE)

// Three updates over two kinds: one write of the sums, one of the gauges.
val result = exporter.record(
    listOf(
        MetricUpdate.IncrementSum(ok, by = 40L),
        MetricUpdate.IncrementSum(notFound, by = 2L),
        MetricUpdate.SetGauge(inFlight, value = 7.0, timestamp = 1_000L),
    ),
)
check(result == MetricExportResult.Success) // every kind it touched is on disk
check(exporter.sumValue(ok) == 40L)
check(exporter.gaugeValue(inFlight) == 7.0)