Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/slo-common-utils.md
Original file line number Diff line number Diff line change
@@ -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.
72 changes: 72 additions & 0 deletions packages/common-utils/src/core/__tests__/sloStats.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getSloBurnRule, type SloBurnCounts } from '@/core/slo';
import {
compareSloGroupsWorstFirst,
compareSlosWorstFirst,
computeSloBurnRate,
computeSloBurnStats,
computeSloGroupBurnFromRows,
computeSloHistory,
computeSloStats,
deriveGroupedSloState,
deriveSloGroupState,
Expand Down Expand Up @@ -35,6 +37,8 @@ const hour = (partial: Partial<SloBurnStats>): 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.
Expand Down Expand Up @@ -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']);
});
});
79 changes: 79 additions & 0 deletions packages/common-utils/src/core/sloStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SLO_GROUP_MIN_EVENTS,
type SloBurnStats,
type SloGroupState,
type SloHistoryBucket,
type SloState,
type SloStats,
type SloWindow,
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — Burn window shorter than (or not a multiple of) the bucket silently drops the current bucket, so every burnRate is null

With getSloHistoryGranularity buckets (30m for 7d, 1h for 30d, core/slo.ts:141) and a 5m or 30m burn window (getSloBurnWindowMs, slo.ts:152), windowStart = b.ts + bucketMs - burnWindowMs is later than b.ts, so the loop pops the bucket it just pushed and the burn rate comes back null for every point. A 1h window over 30m buckets is fine, but a 45m window would drop the straddling bucket entirely. Always keep the current bucket (trailing.length > 1), or check that burnWindowMs is a positive multiple of bucketMs and throw if not. Add a test for 30m buckets with a 5m or 30m window.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

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,
};
});
}
Loading