11import { 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>;
2424export const SloTemplateSchema = z . enum ( [ 'availability' , 'latency' ] ) ;
2525export 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+
27168export const SloEligibleSchema = z . object ( {
28169 where : z . string ( ) . max ( 10000 ) ,
29170 whereLanguage : z . enum ( [ 'lucene' , 'sql' ] ) ,
0 commit comments