From f86ea602b6ab2fed8fa83f70caa0fef578637fcb Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:40:09 +0000 Subject: [PATCH] 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 --- .../src/core/__tests__/slo.test.ts | 23 ++- packages/common-utils/src/slo.ts | 143 +++++++++++++++++- 2 files changed, 164 insertions(+), 2 deletions(-) diff --git a/packages/common-utils/src/core/__tests__/slo.test.ts b/packages/common-utils/src/core/__tests__/slo.test.ts index 9bb54b6a0..99555830a 100644 --- a/packages/common-utils/src/core/__tests__/slo.test.ts +++ b/packages/common-utils/src/core/__tests__/slo.test.ts @@ -2,7 +2,11 @@ import { buildSloLatencyGoodSql, getSloDefaultDuration, } from '@/core/sloLatency'; -import { SloInputSchema } from '@/slo'; +import { + describeSloAlertRule, + describeSloAlertRuleDetails, + SloInputSchema, +} from '@/slo'; import { SourceKind, TSource } from '@/types'; const traceSource = { @@ -59,6 +63,23 @@ describe('SLO schema', () => { ); }); + it('describes each burn alert in one sentence', () => { + expect(describeSloAlertRule('fastBurn', '30d')).toBe( + '2% of the 30-day error budget is spent within 1 hour (14.4x the allowed rate)', + ); + // The same share of a shorter window's budget is a slower burn. + expect(describeSloAlertRule('slowBurn', '7d')).toBe( + '5% of the 7-day error budget is spent within 6 hours (1.4x the allowed rate)', + ); + expect(describeSloAlertRule('slowBurn')).toBe( + '5% of the error budget is spent within 6 hours', + ); + expect(describeSloAlertRuleDetails('fastBurn')).toBe( + 'Resolves once the last 5 minutes burn slower than that. Needs at ' + + 'least 20 events in 1 hour. Checked every 5 minutes.', + ); + }); + it('derives latency thresholds in the source unit', () => { expect( buildSloLatencyGoodSql({ diff --git a/packages/common-utils/src/slo.ts b/packages/common-utils/src/slo.ts index 1a5e68ef9..df838b5ae 100644 --- a/packages/common-utils/src/slo.ts +++ b/packages/common-utils/src/slo.ts @@ -1,6 +1,6 @@ import { z } from 'zod/v3'; -import { FilterSchema } from '@/types'; +import { AlertInterval, FilterSchema } from '@/types'; // An SLO follows OpenSLO v1 concepts in one flat document: an occurrence // based indicator (good over eligible events), a target percentage and a @@ -24,6 +24,147 @@ export type SloWindow = z.infer; export const SloTemplateSchema = z.enum(['availability', 'latency']); export type SloTemplate = z.infer; +export type SloBurnWindow = '5m' | '30m' | '1h' | '6h'; + +/** The burn checks an SLO can alert on, each an alert of its own. */ +export const SloAlertRuleSchema = z.enum(['fastBurn', 'slowBurn']); +export type SloAlertRule = z.infer; + +export type SloBurnCondition = { + /** The alert that fires on this condition. */ + rule: SloAlertRule; + /** The long window, in which the share of the budget burns. */ + window: SloBurnWindow; + /** A twelfth of the long window, which has to burn as fast: still going. */ + shortWindow: SloBurnWindow; + /** Share of the SLO window's error budget spent within the long window. */ + budgetSpent: number; + /** Long-window events needed before the condition can hold. */ + minEvents: number; +}; + +/** + * The fast and slow burn checks, the multiwindow burn-rate alerts of the + * SRE Workbook (https://sre.google/workbook/alerting-on-slos/). An SLO is + * burning when either holds, and each can be an alert with its own + * channels. The burn rate threshold follows from the SLO window, so a 7d SLO + * alerts on the same share of its budget as a 30d SLO. The short window lets + * an alert resolve soon after a burn stops instead of when the long window + * forgets it. + */ +export const SLO_BURN_CONDITIONS = [ + { + rule: 'fastBurn', + window: '1h', + shortWindow: '5m', + budgetSpent: 0.02, + minEvents: 20, + }, + { + rule: 'slowBurn', + window: '6h', + shortWindow: '30m', + budgetSpent: 0.05, + minEvents: 100, + }, +] as const satisfies readonly SloBurnCondition[]; + +export function getSloBurnCondition(rule: SloAlertRule): SloBurnCondition { + return rule === 'slowBurn' ? SLO_BURN_CONDITIONS[1] : SLO_BURN_CONDITIONS[0]; +} + +/** How often a burn alert is checked: about its short window. */ +export function getSloAlertInterval(rule: SloAlertRule): AlertInterval { + return rule === 'slowBurn' ? '15m' : '5m'; +} + +export function getSloAlertRuleLabel(rule: SloAlertRule): string { + return rule === 'slowBurn' ? 'Slow burn' : 'Fast burn'; +} + +/** "1 hour", "30 minutes": a burn window in a sentence. */ +export function formatSloBurnWindow(window: SloBurnWindow): string { + switch (window) { + case '5m': + return '5 minutes'; + case '30m': + return '30 minutes'; + case '1h': + return '1 hour'; + case '6h': + return '6 hours'; + } +} + +/** "the last hour", "the last 5 minutes": a burn window ending now. */ +export function describeSloLastWindow(window: SloBurnWindow): string { + return window === '1h' + ? 'the last hour' + : `the last ${formatSloBurnWindow(window)}`; +} + +/** "30-day": the SLO window as an adjective. */ +export function formatSloWindowAdjective(window: SloWindow): string { + return window === '7d' ? '7-day' : '30-day'; +} + +/** + * When a burn alert fires: "2% of the 30-day error budget is spent within 1 + * hour (14.4x the allowed rate)", or the share alone without a window. + */ +export function describeSloAlertRule( + rule: SloAlertRule, + window?: SloWindow, +): string { + const condition = getSloBurnCondition(rule); + const share = `${Math.round(condition.budgetSpent * 100)}%`; + const within = formatSloBurnWindow(condition.window); + return window + ? `${share} of the ${formatSloWindowAdjective(window)} error budget ` + + `is spent within ${within} ` + + `(${getSloBurnThreshold(condition, window)}x the allowed rate)` + : `${share} of the error budget is spent within ${within}`; +} + +/** The rest of a burn alert's rule: when it resolves, its minimum, its cadence. */ +export function describeSloAlertRuleDetails(rule: SloAlertRule): string { + const { shortWindow, minEvents, window } = getSloBurnCondition(rule); + const minutes = getSloAlertInterval(rule) === '15m' ? 15 : 5; + return ( + `Resolves once the last ${formatSloBurnWindow(shortWindow)} burn slower ` + + `than that. Needs at least ${minEvents} events in ` + + `${formatSloBurnWindow(window)}. Checked every ${minutes} minutes.` + ); +} + +const HOUR_MS = 60 * 60 * 1000; + +export function getSloWindowMs(window: SloWindow): number { + return (window === '7d' ? 7 : 30) * 24 * HOUR_MS; +} + +export function getSloBurnWindowMs(window: SloBurnWindow): number { + switch (window) { + case '5m': + return HOUR_MS / 12; + case '30m': + return HOUR_MS / 2; + case '1h': + return HOUR_MS; + case '6h': + return 6 * HOUR_MS; + } +} + +/** The burn rate at which a condition spends its share of the budget. */ +export function getSloBurnThreshold( + condition: Pick, + window: SloWindow, +): number { + const ratio = getSloWindowMs(window) / getSloBurnWindowMs(condition.window); + return Math.round(condition.budgetSpent * ratio * 100) / 100; +} + export const SloEligibleSchema = z.object({ where: z.string().max(10000), whereLanguage: z.enum(['lucene', 'sql']),