Bounded recoverable delta-to-cumulative metrics processing and event-time reordering
moon add vivid-oi/metricstitch@0.4.0moon update
moon run --target wasm cli -- replay-to examples/session.json session-receipt.json
moon run --target wasm cli -- inspect session-receipt.json
moon run --target wasm cli -- export session-receipt.json session-otlp.json
moon run checks/protocol.mbtx -- session-otlp.jsonmoon update
moon run checks/replay.mbtx -- moon wasm
moon run --target wasm cli -- input.json
moon run --target wasm cli -- next.json checkpoint.json
moon run --target wasm cli -- apply-to input.json first.json
moon run --target wasm cli -- apply-to next.json second.json first.jsonpub suberror BufferError {
ReorderInvalid(String)
LatePoint(String)
BufferFull
WindowExceeded(String)
ReorderConflict(String)
MissingInterval(String)
}pub(all) enum BufferedPoint {
Counter(Sample)
DoubleCounter(DoubleSample)
Histogram(HistogramSample)
Exponential(ExponentialSample)
} derive(Eq)pub(all) struct Diagnostic {
pointer : String
message : String
}pub(all) struct ExponentialSample {
series : String
start : Int64
end : Int64
scale : Int
zero_count : Int64
zero_threshold : Double
positive : ExponentialBuckets
negative : ExponentialBuckets
count : Int64
sum : Double?
min : Double?
max : Double?
} derive(Eq)fn HistogramSample::coarsen(self : HistogramSample, bounds : Array[Double]) -> HistogramSample raise StitchErrortest {
let p : @metricstitch.HistogramSample = {
series: "latency",
start: 1L,
end: 2L,
bounds: [1.0, 2.0],
counts: [1L, 2L, 3L],
count: 6L,
sum: None,
min: None,
max: None,
}
assert_eq(p.coarsen([2.0]).counts, [3L, 3L])
}pub(all) enum IssueCode {
InvalidInput
UnsupportedData
NoncontiguousInterval
ConflictingRetry
SeriesLimit
NumericOverflow
KindChanged
} derive(Eq)pub struct NdjsonDecoder {
// private fields
}pub struct OtlpReorderBuffer {
// private fields
}fn OtlpReorderBuffer::advance(self : OtlpReorderBuffer, watermark : Int64) -> OtlpReorderStep raise BufferErrorfn OtlpReorderBuffer::new(state : State, watermark~ : Int64, max_pending? : Int, max_delay_ns~ : Int64) -> OtlpReorderBuffer raise BufferErrortest {
let b = @metricstitch.OtlpReorderBuffer::new(
@metricstitch.State::new(),
watermark=1L,
max_delay_ns=10L,
)
let step = b.push({ "resourceMetrics": [] }, watermark=2L)
assert_eq(
@metricstitch.decode_reorder_receipt(step.receipt()).buffer.pending_count(),
0,
)
}fn OtlpReorderBuffer::pending_report(self : OtlpReorderBuffer, target : Int64) -> Json raise BufferErrorfn OtlpReorderBuffer::push(self : OtlpReorderBuffer, request : Json, watermark~ : Int64) -> OtlpReorderStep raise BufferErrorpub(all) struct PendingGap {
series : String
start : Int64
end : Int64
next_end : Int64
blocks_target : Bool
} derive(Eq)pub(all) struct PendingReport {
watermark : Int64
target : Int64
pending : Int
ready : Int
capacity : Int
max_delay_ns : Int64
oldest_end : Int64?
newest_end : Int64?
unanchored : Array[String]
gaps : Array[PendingGap]
advance_error : String?
}pub struct ReorderBuffer {
// private fields
}fn ReorderBuffer::new(state : State, watermark~ : Int64, max_pending? : Int, max_delay_ns~ : Int64) -> ReorderBuffer raise BufferErrortest {
let b = @metricstitch.ReorderBuffer::new(
@metricstitch.State::new(),
watermark=1L,
max_delay_ns=10L,
)
assert_eq(b.advance(2L).buffer.pending_count(), 0)
}fn ReorderBuffer::pending_report(self : ReorderBuffer, target : Int64) -> PendingReport raise BufferErrortest {
let b = @metricstitch.ReorderBuffer::new(
@metricstitch.State::new(),
watermark=1L,
max_delay_ns=10L,
)
assert_eq(b.pending_report(2L).advance_error, None)
assert_eq(b.pending_count(), 0)
}fn ReorderBuffer::push(self : ReorderBuffer, batch : Array[BufferedPoint], watermark~ : Int64) -> ReorderStep raise BufferErrorpub(all) struct ReorderStep {
buffer : ReorderBuffer
points : Array[BufferedPoint]
duplicates : Int
}pub(all) struct ReplayRecord {
session : ReplaySession
request : Json
duplicates : Int
ordinal : Int64
}pub struct ReplaySession {
// private fields
}test {
let s = @metricstitch.ReplaySession::new(@metricstitch.State::new())
let r = s.apply_line("{\"resourceMetrics\":[]}")
assert_eq(r.ordinal, 1L)
assert_eq(s.record_count(), 0L)
}fn ReplaySession::verify_record(self : ReplaySession, line : String, receipt : Json) -> ReplaySession raise StitchErrortest {
let s = @metricstitch.ReplaySession::new(@metricstitch.State::new())
let line = "{\"resourceMetrics\":[]}"
let record = s.apply_line(line)
let verified = s.verify_record(line, record.receipt())
assert_eq(verified.checkpoint(), record.session.checkpoint())
assert_eq(s.record_count(), 0L)
}pub struct State {
// private fields
}test {
let s = @metricstitch.State::new()
let p : @metricstitch.Sample = {
series: "requests",
start: 1L,
end: 2L,
value: 7L,
}
let first = s.apply([p])
assert_eq(first.state.apply([p]).duplicates, 1)
}test {
let p : @metricstitch.DoubleSample = {
series: "duration",
start: 1L,
end: 2L,
value: 1.25,
}
let first = @metricstitch.State::new().apply_doubles([p])
assert_eq(first.state.apply_doubles([p]).duplicates, 1)
}fn State::apply_exponentials(self : State, batch : Array[ExponentialSample]) -> ExponentialStep raise StitchErrortest {
let p : @metricstitch.ExponentialSample = {
series: "zeros",
start: 1L,
end: 2L,
scale: 0,
positive: { offset: 0, counts: [], },
negative: { offset: 0, counts: [], },
zero_count: 1L,
zero_threshold: 0.0,
count: 1L,
sum: Some(0.0),
min: None,
max: None,
}
let state = @metricstitch.State::new().apply_exponentials([p]).state
assert_eq(state.apply_exponentials([p]).duplicates, 1)
}fn State::apply_histograms(self : State, batch : Array[HistogramSample]) -> HistogramStep raise StitchErrortest {
let p : @metricstitch.HistogramSample = {
series: "latency",
start: 1L,
end: 2L,
bounds: [1.0],
counts: [1L, 1L],
count: 2L,
sum: Some(2.0),
min: Some(0.0),
max: Some(2.0),
}
let first = @metricstitch.State::new().apply_histograms([p])
assert_eq(first.state.apply_histograms([p]).duplicates, 1)
}fn State::coarsen_histogram(self : State, series : String, bounds : Array[Double]) -> State raise StitchErrorfn State::diff(self : State, other : State, max_changes? : Int, max_paths? : Int) -> StateDiff raise StitchErrortest {
let a = @metricstitch.State::new()
assert_eq(a.diff(a).changes.length(), 0)
}test {
let a : @metricstitch.Sample = { series: "a", start: 1L, end: 2L, value: 5L, }
let b = { ..a, series: "b", }
let left = @metricstitch.State::new().apply([a]).state
let right = @metricstitch.State::new().apply([b]).state
let joined = left.merge_disjoint(right)
assert_eq(joined.apply([a, b]).duplicates, 2)
}test {
let p : @metricstitch.Sample = {
series: "requests",
start: 1L,
end: 2L,
value: 7L,
}
let state = @metricstitch.State::new().apply([p]).state
let split = state.partition_before(3L)
assert_eq(split.archived.checkpoint(), state.checkpoint())
}pub(all) struct StateChange {
series : String
kind : StateChangeKind
paths : Array[String]
before : Json?
after : Json?
}pub(all) struct StateDiff {
before_capacity : Int
after_capacity : Int
unchanged : Int
changes : Array[StateChange]
}test {
let empty : Json = { "resourceMetrics": [] }
assert_eq(@metricstitch.compact_otlp(empty), empty)
}test {
let step = @metricstitch.process_otlp(@metricstitch.State::new(), {
"resourceMetrics": [],
})
let restored = @metricstitch.decode_receipt(step.receipt())
assert_eq(restored.state.checkpoint(), step.state.checkpoint())
}test {
let d = @metricstitch.diagnose_otlp(@metricstitch.State::new(), {
"unknown": 1,
})
assert_true(d is Some({ code: UnsupportedData, .. }))
}test {
let state = @metricstitch.State::new()
let result = @metricstitch.process_sequence(state, [{ "resourceMetrics": [] }])
assert_eq(result.state.checkpoint(), state.checkpoint())
}test {
let state = @metricstitch.State::new()
assert_true(
@metricstitch.validate_otlp(state, { "resourceMetrics": [] }) is None,
)
}Install
Download zipBounded recoverable delta-to-cumulative metrics processing and event-time reordering