WarpSpanExporter

class WarpSpanExporter(replica: ReplicaId, store: DurableStore, maxSpans: Int = DEFAULT_MAX_SPANS, bufferPolicy: BufferPolicy = BufferPolicy.DROP_OLDEST, causalClock: WarpCausalClock? = null)

A CRDT-backed span exporter.

Spans are stored as SpanRecord values in an ORSet: adding the same value twice is idempotent. Auto-stamping can give a retry a different value, even with the same SpanRecord.spanId; see export for failed-write retry limits. Reconnecting peers reconcile by sharing their ORSet deltas through the kuilt anti-entropy layer rather than replaying a queue, so only missing spans move over the wire.

Key inversion

export returns ExportResult.Success the moment the span is durably written to the DurableStore — not when it is delivered to any backend. Delivery is asynchronous and eventually consistent; the CRDT merge guarantees that any replica which receives the span will incorporate it correctly, even if the span arrives out of order or more than once.

Buffer cap

When the in-memory CRDT already holds maxSpans spans, export evicts one as it inserts: the earliest by SpanRecord.startEpochNanos under BufferPolicy.DROP_OLDEST, the latest under BufferPolicy.DROP_NEWEST. The victim is chosen from the spans already held, so an export's own eviction never takes the span it is adding, and it is committed only with the add, so an export whose add is refused evicts nothing (#3441). An export builds its add and its eviction inside the durable-write section, writes them, and makes them visible only once the write has returned (#3517), so the bytes an export writes always carry its own span, and an export whose write fails evicts nothing from memory either. A later export's eviction can take it. That is all the cap promises the incoming span. merge does not evict. The read of the stored spans that lands, in recover or in the first write turn after a refused one, evicts whatever its join brought in beyond maxSpans in one batch, by the same policy, before that turn's own insert. Every eviction is logged with enough detail to correlate against a backend's orphan-span index.

Failure reporting

A refused durable write is logged once per outage, not once per attempt (#2593). Every call retries the write the last one could not land, so a store that stays broken is one unchanging condition, and reporting it per call produced a line — and a stack trace — per export, forever. export, merge and clear share one latch because they write one key; the next line comes after the store has taken a write. Every failure is still returned to the caller as ExportResult.Failure carrying the cause, so nothing is hidden from a programmatic reader — only from the log. See durableWriteOutage.

Parameters

replica

The ReplicaId for this device/process. Must be unique and stable across restarts (a UUID is recommended).

store

The DurableStore to persist CRDT state. Use InMemoryDurableStore in tests; wire a platform WAL (JVM file, IndexedDB, etc.) in production.

maxSpans

Maximum number of spans buffered in memory before eviction. Defaults to DEFAULT_MAX_SPANS.

bufferPolicy

What to do when maxSpans is exceeded. Defaults to BufferPolicy.DROP_OLDEST.

Samples

val replica = ReplicaId("device-uuid-abc123")
val store = InMemoryDurableStore()
val exporter = WarpSpanExporter(replica = replica, store = store)

// Recover persisted state from a previous session.
exporter.recover()

// Span ids are raw bytes (OTLP wire format): 16 bytes for trace id, 8 for span id.
val span = SpanRecord(
    traceId = ByteString(ByteArray(16) { it.toByte() }),
    spanId = ByteString(ByteArray(8) { it.toByte() }),
    parentSpanId = null,
    name = "checkout",
    kind = SpanKind.CLIENT,
    startEpochNanos = 1_000_000_000L,
    endEpochNanos = 2_000_000_000L,
)

// This exporter has no causal clock: both calls add the same full span value.
// Auto-stamped retries after an indeterminate write can instead leave distinct copies.
exporter.export(span)
val secondResult = exporter.export(span)
check(secondResult == ExportResult.Success)
check(exporter.snapshot().elements.size == 1) { "duplicate was stored" }

Constructors

Link copied to clipboard
constructor(replica: ReplicaId, store: DurableStore, maxSpans: Int = DEFAULT_MAX_SPANS, bufferPolicy: BufferPolicy = BufferPolicy.DROP_OLDEST, causalClock: WarpCausalClock? = null)

Properties

Link copied to clipboard
val health: StateFlow<PersistenceHealth>

Completed persistence turns and unresolved failures, independent of outage logging. Includes a configured clock's write as part of export/clear, not a second acknowledgement. A turn that skips the clock's write, because its read has not landed, counts per work key instead: its span work as the span write went, and its clock work as one failed turn that only a later clock write resolves (#3426). So does an export that wanted a dot from an exhausted clock (ClockTick.Exhausted) and saved its span unstamped, though its clock write lands; the turn that mints the last dot, a clear, and a span with its own stamp want no dot they cannot get, so they are not counted, and their clock write resolves those failures. Recovery covers the span buffer; WarpTelemetry.recover also reports its clock's recovery.

Functions

Link copied to clipboard
suspend fun clear(): ExportResult

Drop every span this exporter holds and persist the emptied set (#2208).

Link copied to clipboard
suspend fun export(span: SpanRecord): ExportResult

Export one span: insert it into the CRDT and durably flush to store.

Link copied to clipboard
suspend fun merge(remote: ORSet<SpanRecord>): ExportResult

Merge an ORSet received from another replica (via anti-entropy / gossip) into this exporter's state, then flush the merged result to store.

Link copied to clipboard
suspend fun recover()

Recover persisted span state from store. Call once at startup before any calls to export.

Link copied to clipboard

Read a snapshot of the current in-memory ORSet for gossip / anti-entropy.