From 2b18fb758c8cd83e4327b2559219e6b0294c5b66 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:43:45 +0000 Subject: [PATCH] feat(common-utils): SLO history and ordering Adds the history behind the SLO charts, computed on read from bucketed counts: the SLI per bucket, the error budget burndown over the displayed window, and the burn rate over the trailing burn window, as the alerts read it. Also adds worst first ordering for SLO lists: by state, then by budget left, then by name. Co-Authored-By: Claude Opus 5.5 --- .changeset/slo-common-utils.md | 15 ++++ .../src/core/__tests__/sloStats.test.ts | 72 +++++++++++++++++ packages/common-utils/src/core/sloStats.ts | 79 +++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 .changeset/slo-common-utils.md diff --git a/.changeset/slo-common-utils.md b/.changeset/slo-common-utils.md new file mode 100644 index 000000000..cbb202838 --- /dev/null +++ b/.changeset/slo-common-utils.md @@ -0,0 +1,15 @@ +--- +'@hyperdx/common-utils': patch +--- + +feat: SLO schema, burn rules and the counts query behind SLOs + +Adds the SLO definition schema (availability or latency indicator, target, +rolling 7 or 30 day window, optional group-by expression, optional fast and +slow burn alerts), the single counts query every SLO number comes from, +overall or by group, and the math for the SLI, error budget and the fast +and slow burn rules (2% of the budget within an hour, 5% within six hours, +each with a short window that has to burn as fast). A grouped SLO tracks its 100 +busiest groups, each with its own numbers; a group with fewer than 100 +events in the window has no state of its own, and the SLO takes the state +of its worst group. Not used until SLOs are enabled. diff --git a/packages/common-utils/src/core/__tests__/sloStats.test.ts b/packages/common-utils/src/core/__tests__/sloStats.test.ts index 393090e2d..2f3399950 100644 --- a/packages/common-utils/src/core/__tests__/sloStats.test.ts +++ b/packages/common-utils/src/core/__tests__/sloStats.test.ts @@ -1,9 +1,11 @@ import { getSloBurnRule, type SloBurnCounts } from '@/core/slo'; import { compareSloGroupsWorstFirst, + compareSlosWorstFirst, computeSloBurnRate, computeSloBurnStats, computeSloGroupBurnFromRows, + computeSloHistory, computeSloStats, deriveGroupedSloState, deriveSloGroupState, @@ -35,6 +37,8 @@ const hour = (partial: Partial): SloBurnStats => ({ ...partial, }); +const HOUR_MS = 60 * 60 * 1000; + describe('SLO stats', () => { it('derives bad events as eligible minus good', () => { // 1000 eligible, 990 good: 10 bad against a 99.5% target. @@ -244,3 +248,71 @@ describe('SLO groups', () => { ).toEqual(['c', 'd', 'a', 'b']); }); }); + +describe('SLO history', () => { + it('builds a burndown that ends at the budget left', () => { + const points = computeSloHistory( + [ + { ts: 0, eligible: 1000, good: 1000 }, + { ts: HOUR_MS, eligible: 1000, good: 990 }, + ], + { target: 99, bucketMs: HOUR_MS, burnWindowMs: HOUR_MS }, + ); + expect(points[0].budgetRemaining).toBeCloseTo(1); + expect(points[1].budgetRemaining).toBeCloseTo(0.5); + expect(points[1].burnRate).toBeCloseTo(1); + }); + + it('reads the burn rate over the trailing burn window', () => { + // Hourly buckets against a 6 hour window: the bad hour keeps counting + // until it is more than 6 hours old, like the slow alert sees it. + const buckets = Array.from({ length: 8 }, (_, i) => ({ + ts: i * HOUR_MS, + eligible: 100, + good: i === 0 ? 40 : 100, + })); + const points = computeSloHistory(buckets, { + target: 99, + bucketMs: HOUR_MS, + burnWindowMs: 6 * HOUR_MS, + }); + // 60 bad of 100 in the first hour: 60x against a 1% budget. + expect(points[0].burnRate).toBeCloseTo(60); + // 60 bad of 600 over six hours: 10x. + expect(points[5].burnRate).toBeCloseTo(10); + // The bad hour has left the window. + expect(points[6].burnRate).toBeCloseTo(0); + }); + + it('measures the trailing window in time, not in buckets', () => { + // A missing bucket (no events) must not stretch the window. + const points = computeSloHistory( + [ + { ts: 0, eligible: 100, good: 0 }, + { ts: 3 * HOUR_MS, eligible: 100, good: 100 }, + ], + { target: 99, bucketMs: HOUR_MS, burnWindowMs: 2 * HOUR_MS }, + ); + expect(points[1].burnRate).toBeCloseTo(0); + }); +}); + +describe('SLO ordering', () => { + it('orders SLOs worst first', () => { + const slo = ( + name: string, + state: 'healthy' | 'breached' | 'burning', + budget: number, + ) => ({ + name, + status: { state, budgetRemaining: budget }, + }); + const sorted = [ + slo('a', 'healthy', 0.9), + slo('b', 'breached', -0.5), + slo('c', 'burning', 0.4), + slo('d', 'breached', -2), + ].sort(compareSlosWorstFirst); + expect(sorted.map(s => s.name)).toEqual(['d', 'b', 'c', 'a']); + }); +}); diff --git a/packages/common-utils/src/core/sloStats.ts b/packages/common-utils/src/core/sloStats.ts index cdc0278a6..4e7539bfa 100644 --- a/packages/common-utils/src/core/sloStats.ts +++ b/packages/common-utils/src/core/sloStats.ts @@ -12,6 +12,7 @@ import { SLO_GROUP_MIN_EVENTS, type SloBurnStats, type SloGroupState, + type SloHistoryBucket, type SloState, type SloStats, type SloWindow, @@ -230,3 +231,81 @@ export function compareSloGroupsWorstFirst( if (budget !== 0 && !Number.isNaN(budget)) return budget; return a.value.localeCompare(b.value); } + +/** Worst first: by state, then by error budget left, then by name. */ +export function compareSlosWorstFirst( + a: { + name: string; + status?: { state: SloState; budgetRemaining: number | null } | null; + }, + b: { + name: string; + status?: { state: SloState; budgetRemaining: number | null } | null; + }, +): number { + const byState = + stateSeverity(b.status?.state ?? 'no_data') - + stateSeverity(a.status?.state ?? 'no_data'); + if (byState !== 0) return byState; + const budget = + (a.status?.budgetRemaining ?? Infinity) - + (b.status?.budgetRemaining ?? Infinity); + if (budget !== 0 && !Number.isNaN(budget)) return budget; + return a.name.localeCompare(b.name); +} + +export type SloHistoryPoint = { + ts: number; + eligible: number; + bad: number; + sli: number | null; + /** Over the trailing burn window that ends with this bucket. */ + burnRate: number | null; + /** Fraction of the window's error budget left after this bucket. */ + budgetRemaining: number | null; +}; + +/** + * Computed on read from bucketed counts, so nothing is stored for charts. + * The burndown measures spend against the budget of the whole displayed + * window, so its last point matches the current budget remaining. The burn + * rate is read over the trailing burn window, as the alert reads it, so a + * point at or above the threshold is one where the long window burned. + */ +export function computeSloHistory( + buckets: SloHistoryBucket[], + { + target, + bucketMs, + burnWindowMs, + }: { target: number; bucketMs: number; burnWindowMs: number }, +): SloHistoryPoint[] { + const totalEligible = buckets.reduce((sum, b) => sum + b.eligible, 0); + const budget = getAllowedBadFraction(target) * totalEligible; + let spent = 0; + const trailing: SloHistoryBucket[] = []; + let trailingEligible = 0; + let trailingGood = 0; + return buckets.map(b => { + const bad = Math.max(0, b.eligible - b.good); + spent += bad; + trailing.push(b); + trailingEligible += b.eligible; + trailingGood += b.good; + // Buckets that ended before the burn window began drop out. + const windowStart = b.ts + bucketMs - burnWindowMs; + while (trailing.length > 0 && trailing[0].ts < windowStart) { + const dropped = trailing.shift(); + trailingEligible -= dropped?.eligible ?? 0; + trailingGood -= dropped?.good ?? 0; + } + return { + ts: b.ts, + eligible: b.eligible, + bad, + sli: b.eligible > 0 ? b.good / b.eligible : null, + burnRate: computeSloBurnRate(trailingEligible, trailingGood, target), + budgetRemaining: budget > 0 ? 1 - spent / budget : null, + }; + }); +}