|
| 1 | +import { getSloBurnRule, type SloBurnCounts } from '@/core/slo'; |
| 2 | +import { |
| 3 | + compareSloGroupsWorstFirst, |
| 4 | + computeSloBurnRate, |
| 5 | + computeSloBurnStats, |
| 6 | + computeSloGroupBurnFromRows, |
| 7 | + computeSloStats, |
| 8 | + deriveGroupedSloState, |
| 9 | + deriveSloGroupState, |
| 10 | + deriveSloState, |
| 11 | + getSloBurnSeverity, |
| 12 | + isSloBurnConditionMet, |
| 13 | + isSloBurning, |
| 14 | +} from '@/core/sloStats'; |
| 15 | +import type { SloBurnStats } from '@/slo'; |
| 16 | + |
| 17 | +// 1h at 14.4x and 6h at 6x for a 30-day SLO. |
| 18 | +const rule = getSloBurnRule('30d'); |
| 19 | + |
| 20 | +const burnCounts = (partial: Partial<SloBurnCounts>): SloBurnCounts => ({ |
| 21 | + eligible: 0, |
| 22 | + good: 0, |
| 23 | + shortEligible: 0, |
| 24 | + shortGood: 0, |
| 25 | + ...partial, |
| 26 | +}); |
| 27 | + |
| 28 | +/** The 1h condition with the given rates and events. */ |
| 29 | +const hour = (partial: Partial<SloBurnStats>): SloBurnStats => ({ |
| 30 | + ...rule[0], |
| 31 | + eligible: 1000, |
| 32 | + bad: 0, |
| 33 | + burnRate: 0, |
| 34 | + shortBurnRate: 0, |
| 35 | + ...partial, |
| 36 | +}); |
| 37 | + |
| 38 | +describe('SLO stats', () => { |
| 39 | + it('derives bad events as eligible minus good', () => { |
| 40 | + // 1000 eligible, 990 good: 10 bad against a 99.5% target. |
| 41 | + const burn = computeSloBurnStats( |
| 42 | + [burnCounts({ eligible: 100, good: 98 })], |
| 43 | + rule, |
| 44 | + 99.5, |
| 45 | + ); |
| 46 | + const stats = computeSloStats({ eligible: 1000, good: 990 }, burn, 99.5); |
| 47 | + expect(stats.bad).toBe(10); |
| 48 | + expect(stats.sli).toBeCloseTo(0.99); |
| 49 | + expect(stats.errorBudget).toBeCloseTo(5); |
| 50 | + expect(stats.budgetRemaining).toBeCloseTo(-1); |
| 51 | + expect(stats.burn[0].burnRate).toBeCloseTo(4); |
| 52 | + // A condition without counts reads as no burn. |
| 53 | + expect(stats.burn[1]).toMatchObject({ window: '6h', burnRate: null }); |
| 54 | + expect(deriveSloState(stats)).toBe('breached'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('computes the burn rate as a multiple of the allowed bad rate', () => { |
| 58 | + // 10 bad of 1000 against a 0.5% budget: 1% bad is a 2x burn. |
| 59 | + expect(computeSloBurnRate(1000, 990, 99.5)).toBeCloseTo(2); |
| 60 | + expect(computeSloBurnRate(0, 0, 99.5)).toBeNull(); |
| 61 | + // More good than eligible (late rows) never reads as a negative burn. |
| 62 | + expect(computeSloBurnRate(10, 12, 99.5)).toBe(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it('separates no data, healthy and burning', () => { |
| 66 | + expect( |
| 67 | + deriveSloState(computeSloStats({ eligible: 0, good: 0 }, [], 99.9)), |
| 68 | + ).toBe('no_data'); |
| 69 | + // 2% bad in both windows of the 1h condition against a 0.1% budget: a |
| 70 | + // 20x burn. |
| 71 | + const burn = computeSloBurnStats( |
| 72 | + [ |
| 73 | + burnCounts({ |
| 74 | + eligible: 1000, |
| 75 | + good: 980, |
| 76 | + shortEligible: 100, |
| 77 | + shortGood: 98, |
| 78 | + }), |
| 79 | + ], |
| 80 | + rule, |
| 81 | + 99.9, |
| 82 | + ); |
| 83 | + const burning = computeSloStats( |
| 84 | + { eligible: 1_000_000, good: 999_500 }, |
| 85 | + burn, |
| 86 | + 99.9, |
| 87 | + ); |
| 88 | + expect(burning.budgetRemaining).toBeGreaterThan(0); |
| 89 | + expect(deriveSloState(burning)).toBe('burning'); |
| 90 | + const calm = computeSloBurnStats([], rule, 99.9); |
| 91 | + expect(deriveSloState({ ...burning, burn: calm })).toBe('healthy'); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('SLO burn-rate rule', () => { |
| 96 | + it('scales the thresholds to the SLO window', () => { |
| 97 | + expect(rule.map(c => [c.window, c.shortWindow, c.threshold])).toEqual([ |
| 98 | + ['1h', '5m', 14.4], |
| 99 | + ['6h', '30m', 6], |
| 100 | + ]); |
| 101 | + expect(getSloBurnRule('7d').map(c => c.threshold)).toEqual([3.36, 1.4]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('needs both windows of a condition at or above its threshold', () => { |
| 105 | + expect( |
| 106 | + isSloBurnConditionMet(hour({ burnRate: 20, shortBurnRate: 15 })), |
| 107 | + ).toBe(true); |
| 108 | + // The burn stopped: the long window still remembers it, the short one |
| 109 | + // does not, so the condition (and the alert) stops holding. |
| 110 | + expect( |
| 111 | + isSloBurnConditionMet(hour({ burnRate: 20, shortBurnRate: 0 })), |
| 112 | + ).toBe(false); |
| 113 | + // No events in the short window is not a burn either. |
| 114 | + expect( |
| 115 | + isSloBurnConditionMet(hour({ burnRate: 20, shortBurnRate: null })), |
| 116 | + ).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('needs enough events in the long window', () => { |
| 120 | + const fast = { burnRate: 100, shortBurnRate: 100 }; |
| 121 | + expect(isSloBurnConditionMet(hour({ ...fast, eligible: 10 }))).toBe(false); |
| 122 | + expect(isSloBurnConditionMet(hour({ ...fast, eligible: 20 }))).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('holds when either condition does', () => { |
| 126 | + const slowBurn: SloBurnStats = { |
| 127 | + ...rule[1], |
| 128 | + eligible: 6000, |
| 129 | + bad: 42, |
| 130 | + burnRate: 7, |
| 131 | + shortBurnRate: 6.5, |
| 132 | + }; |
| 133 | + // 7x for 6 hours misses the 1h condition's 14.4x and meets the 6h one. |
| 134 | + expect( |
| 135 | + isSloBurning([hour({ burnRate: 7, shortBurnRate: 7 }), slowBurn]), |
| 136 | + ).toBe(true); |
| 137 | + expect(isSloBurning([hour({ burnRate: 7, shortBurnRate: 7 })])).toBe(false); |
| 138 | + }); |
| 139 | + |
| 140 | + it('measures how close the rule is to holding', () => { |
| 141 | + // The slower window of a condition over its threshold, the highest wins. |
| 142 | + expect( |
| 143 | + getSloBurnSeverity([hour({ burnRate: 21.6, shortBurnRate: 14.4 })]), |
| 144 | + ).toBe(1); |
| 145 | + expect( |
| 146 | + getSloBurnSeverity([hour({ burnRate: 7.2, shortBurnRate: 9 })]), |
| 147 | + ).toBe(0.5); |
| 148 | + // Not enough events, or none, counts for nothing. |
| 149 | + expect( |
| 150 | + getSloBurnSeverity([ |
| 151 | + hour({ eligible: 5, burnRate: 100, shortBurnRate: 100 }), |
| 152 | + ]), |
| 153 | + ).toBe(0); |
| 154 | + expect(getSloBurnSeverity([])).toBe(0); |
| 155 | + // Just under the threshold never rounds up to holding. |
| 156 | + expect( |
| 157 | + getSloBurnSeverity([hour({ burnRate: 14.39, shortBurnRate: 14.39 })]), |
| 158 | + ).toBe(0.99); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +describe('SLO groups', () => { |
| 163 | + const burning = [hour({ burnRate: 20, shortBurnRate: 20 })]; |
| 164 | + const stats = ( |
| 165 | + eligible: number, |
| 166 | + budgetRemaining: number, |
| 167 | + burn = calmBurn, |
| 168 | + ) => ({ |
| 169 | + eligible, |
| 170 | + budgetRemaining, |
| 171 | + burn, |
| 172 | + }); |
| 173 | + const calmBurn = [hour({})]; |
| 174 | + |
| 175 | + it('gives a group a state once it has enough events', () => { |
| 176 | + expect(deriveSloGroupState(stats(99, 0.5, burning))).toBe( |
| 177 | + 'insufficient_data', |
| 178 | + ); |
| 179 | + expect(deriveSloGroupState(stats(100, 0.5, burning))).toBe('burning'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('rates a grouped SLO by its worst group that has enough events', () => { |
| 183 | + const overall = stats(10_000, 0.9); |
| 184 | + expect( |
| 185 | + deriveGroupedSloState(overall, [ |
| 186 | + { state: 'healthy' }, |
| 187 | + { state: 'burning' }, |
| 188 | + // Too few events to count, however bad. |
| 189 | + { state: 'insufficient_data' }, |
| 190 | + ]), |
| 191 | + ).toBe('burning'); |
| 192 | + // No group with enough events: the overall numbers decide. |
| 193 | + expect( |
| 194 | + deriveGroupedSloState(stats(10_000, -0.1), [ |
| 195 | + { state: 'insufficient_data' }, |
| 196 | + ]), |
| 197 | + ).toBe('breached'); |
| 198 | + expect(deriveGroupedSloState(overall, [])).toBe('healthy'); |
| 199 | + }); |
| 200 | + |
| 201 | + it("computes each group's burn, with none for a group without events", () => { |
| 202 | + const now = new Date('2026-11-01T01:07:00Z'); |
| 203 | + // Twelve 5-minute buckets ending where the burn query ends, 01:05. |
| 204 | + const rows = Array.from({ length: 12 }, (_, i) => ({ |
| 205 | + slo_group: 'checkout', |
| 206 | + __hdx_time_bucket: new Date( |
| 207 | + Date.parse('2026-11-01T00:05:00Z') + i * 5 * 60_000, |
| 208 | + ).toISOString(), |
| 209 | + slo_eligible: '100', |
| 210 | + slo_good: '90', |
| 211 | + })); |
| 212 | + const burn = computeSloGroupBurnFromRows( |
| 213 | + rows, |
| 214 | + { window: '30d', target: 99.5 }, |
| 215 | + now, |
| 216 | + ['checkout', 'cart'], |
| 217 | + ); |
| 218 | + expect([...burn.keys()]).toEqual(['checkout', 'cart']); |
| 219 | + expect(burn.get('checkout')?.[0]).toMatchObject({ |
| 220 | + window: '1h', |
| 221 | + eligible: 1200, |
| 222 | + bad: 120, |
| 223 | + burnRate: expect.closeTo(20), |
| 224 | + }); |
| 225 | + expect(burn.get('cart')?.[0]).toMatchObject({ |
| 226 | + eligible: 0, |
| 227 | + burnRate: null, |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + it('orders groups worst first', () => { |
| 232 | + const groups = [ |
| 233 | + { value: 'a', state: 'healthy' as const, budgetRemaining: 0.9 }, |
| 234 | + { |
| 235 | + value: 'b', |
| 236 | + state: 'insufficient_data' as const, |
| 237 | + budgetRemaining: null, |
| 238 | + }, |
| 239 | + { value: 'c', state: 'breached' as const, budgetRemaining: -0.2 }, |
| 240 | + { value: 'd', state: 'healthy' as const, budgetRemaining: 0.3 }, |
| 241 | + ]; |
| 242 | + expect( |
| 243 | + [...groups].sort(compareSloGroupsWorstFirst).map(g => g.value), |
| 244 | + ).toEqual(['c', 'd', 'a', 'b']); |
| 245 | + }); |
| 246 | +}); |
0 commit comments