Skip to content

Commit f86ea60

Browse files
alex-fedotyevclaude
andcommitted
feat(common-utils): SLO fast and slow burn rules
Adds the two burn checks an SLO is judged by: fast burn, when 2% of the window's error budget is spent within an hour, and slow burn, when 5% is spent within 6 hours. Each also needs a short window, a twelfth of the long one, to burn as fast, so it stops holding soon after a burn stops, and a minimum of events. The burn rate threshold follows from the SLO window (14.4x and 6x for 30 days, 3.36x and 1.4x for 7 days), so both windows alert on the same share of their budget. describeSloAlertRule gives each rule as one sentence, for the builder, the Alerts page, notifications and MCP to share. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b1b739a commit f86ea60

2 files changed

Lines changed: 164 additions & 2 deletions

File tree

‎packages/common-utils/src/core/__tests__/slo.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {
22
buildSloLatencyGoodSql,
33
getSloDefaultDuration,
44
} from '@/core/sloLatency';
5-
import { SloInputSchema } from '@/slo';
5+
import {
6+
describeSloAlertRule,
7+
describeSloAlertRuleDetails,
8+
SloInputSchema,
9+
} from '@/slo';
610
import { SourceKind, TSource } from '@/types';
711

812
const traceSource = {
@@ -59,6 +63,23 @@ describe('SLO schema', () => {
5963
);
6064
});
6165

66+
it('describes each burn alert in one sentence', () => {
67+
expect(describeSloAlertRule('fastBurn', '30d')).toBe(
68+
'2% of the 30-day error budget is spent within 1 hour (14.4x the allowed rate)',
69+
);
70+
// The same share of a shorter window's budget is a slower burn.
71+
expect(describeSloAlertRule('slowBurn', '7d')).toBe(
72+
'5% of the 7-day error budget is spent within 6 hours (1.4x the allowed rate)',
73+
);
74+
expect(describeSloAlertRule('slowBurn')).toBe(
75+
'5% of the error budget is spent within 6 hours',
76+
);
77+
expect(describeSloAlertRuleDetails('fastBurn')).toBe(
78+
'Resolves once the last 5 minutes burn slower than that. Needs at ' +
79+
'least 20 events in 1 hour. Checked every 5 minutes.',
80+
);
81+
});
82+
6283
it('derives latency thresholds in the source unit', () => {
6384
expect(
6485
buildSloLatencyGoodSql({

‎packages/common-utils/src/slo.ts‎

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod/v3';
22

3-
import { FilterSchema } from '@/types';
3+
import { AlertInterval, FilterSchema } from '@/types';
44

55
// An SLO follows OpenSLO v1 concepts in one flat document: an occurrence
66
// based indicator (good over eligible events), a target percentage and a
@@ -24,6 +24,147 @@ export type SloWindow = z.infer<typeof SloWindowSchema>;
2424
export const SloTemplateSchema = z.enum(['availability', 'latency']);
2525
export type SloTemplate = z.infer<typeof SloTemplateSchema>;
2626

27+
export type SloBurnWindow = '5m' | '30m' | '1h' | '6h';
28+
29+
/** The burn checks an SLO can alert on, each an alert of its own. */
30+
export const SloAlertRuleSchema = z.enum(['fastBurn', 'slowBurn']);
31+
export type SloAlertRule = z.infer<typeof SloAlertRuleSchema>;
32+
33+
export type SloBurnCondition = {
34+
/** The alert that fires on this condition. */
35+
rule: SloAlertRule;
36+
/** The long window, in which the share of the budget burns. */
37+
window: SloBurnWindow;
38+
/** A twelfth of the long window, which has to burn as fast: still going. */
39+
shortWindow: SloBurnWindow;
40+
/** Share of the SLO window's error budget spent within the long window. */
41+
budgetSpent: number;
42+
/** Long-window events needed before the condition can hold. */
43+
minEvents: number;
44+
};
45+
46+
/**
47+
* The fast and slow burn checks, the multiwindow burn-rate alerts of the
48+
* SRE Workbook (https://sre.google/workbook/alerting-on-slos/). An SLO is
49+
* burning when either holds, and each can be an alert with its own
50+
* channels. The burn rate threshold follows from the SLO window, so a 7d SLO
51+
* alerts on the same share of its budget as a 30d SLO. The short window lets
52+
* an alert resolve soon after a burn stops instead of when the long window
53+
* forgets it.
54+
*/
55+
export const SLO_BURN_CONDITIONS = [
56+
{
57+
rule: 'fastBurn',
58+
window: '1h',
59+
shortWindow: '5m',
60+
budgetSpent: 0.02,
61+
minEvents: 20,
62+
},
63+
{
64+
rule: 'slowBurn',
65+
window: '6h',
66+
shortWindow: '30m',
67+
budgetSpent: 0.05,
68+
minEvents: 100,
69+
},
70+
] as const satisfies readonly SloBurnCondition[];
71+
72+
export function getSloBurnCondition(rule: SloAlertRule): SloBurnCondition {
73+
return rule === 'slowBurn' ? SLO_BURN_CONDITIONS[1] : SLO_BURN_CONDITIONS[0];
74+
}
75+
76+
/** How often a burn alert is checked: about its short window. */
77+
export function getSloAlertInterval(rule: SloAlertRule): AlertInterval {
78+
return rule === 'slowBurn' ? '15m' : '5m';
79+
}
80+
81+
export function getSloAlertRuleLabel(rule: SloAlertRule): string {
82+
return rule === 'slowBurn' ? 'Slow burn' : 'Fast burn';
83+
}
84+
85+
/** "1 hour", "30 minutes": a burn window in a sentence. */
86+
export function formatSloBurnWindow(window: SloBurnWindow): string {
87+
switch (window) {
88+
case '5m':
89+
return '5 minutes';
90+
case '30m':
91+
return '30 minutes';
92+
case '1h':
93+
return '1 hour';
94+
case '6h':
95+
return '6 hours';
96+
}
97+
}
98+
99+
/** "the last hour", "the last 5 minutes": a burn window ending now. */
100+
export function describeSloLastWindow(window: SloBurnWindow): string {
101+
return window === '1h'
102+
? 'the last hour'
103+
: `the last ${formatSloBurnWindow(window)}`;
104+
}
105+
106+
/** "30-day": the SLO window as an adjective. */
107+
export function formatSloWindowAdjective(window: SloWindow): string {
108+
return window === '7d' ? '7-day' : '30-day';
109+
}
110+
111+
/**
112+
* When a burn alert fires: "2% of the 30-day error budget is spent within 1
113+
* hour (14.4x the allowed rate)", or the share alone without a window.
114+
*/
115+
export function describeSloAlertRule(
116+
rule: SloAlertRule,
117+
window?: SloWindow,
118+
): string {
119+
const condition = getSloBurnCondition(rule);
120+
const share = `${Math.round(condition.budgetSpent * 100)}%`;
121+
const within = formatSloBurnWindow(condition.window);
122+
return window
123+
? `${share} of the ${formatSloWindowAdjective(window)} error budget ` +
124+
`is spent within ${within} ` +
125+
`(${getSloBurnThreshold(condition, window)}x the allowed rate)`
126+
: `${share} of the error budget is spent within ${within}`;
127+
}
128+
129+
/** The rest of a burn alert's rule: when it resolves, its minimum, its cadence. */
130+
export function describeSloAlertRuleDetails(rule: SloAlertRule): string {
131+
const { shortWindow, minEvents, window } = getSloBurnCondition(rule);
132+
const minutes = getSloAlertInterval(rule) === '15m' ? 15 : 5;
133+
return (
134+
`Resolves once the last ${formatSloBurnWindow(shortWindow)} burn slower ` +
135+
`than that. Needs at least ${minEvents} events in ` +
136+
`${formatSloBurnWindow(window)}. Checked every ${minutes} minutes.`
137+
);
138+
}
139+
140+
const HOUR_MS = 60 * 60 * 1000;
141+
142+
export function getSloWindowMs(window: SloWindow): number {
143+
return (window === '7d' ? 7 : 30) * 24 * HOUR_MS;
144+
}
145+
146+
export function getSloBurnWindowMs(window: SloBurnWindow): number {
147+
switch (window) {
148+
case '5m':
149+
return HOUR_MS / 12;
150+
case '30m':
151+
return HOUR_MS / 2;
152+
case '1h':
153+
return HOUR_MS;
154+
case '6h':
155+
return 6 * HOUR_MS;
156+
}
157+
}
158+
159+
/** The burn rate at which a condition spends its share of the budget. */
160+
export function getSloBurnThreshold(
161+
condition: Pick<SloBurnCondition, 'window' | 'budgetSpent'>,
162+
window: SloWindow,
163+
): number {
164+
const ratio = getSloWindowMs(window) / getSloBurnWindowMs(condition.window);
165+
return Math.round(condition.budgetSpent * ratio * 100) / 100;
166+
}
167+
27168
export const SloEligibleSchema = z.object({
28169
where: z.string().max(10000),
29170
whereLanguage: z.enum(['lucene', 'sql']),

0 commit comments

Comments
 (0)