Skip to content

Commit 2cc06b4

Browse files
alex-fedotyevclaude
andcommitted
feat(app): fast and slow burn alerts in the SLO builder, list and page
The SLO builder gets a burn-rate alerts section with the two alerts. Each is a switch labeled with its rule, for example "Notify when 2% of the 30-day error budget is spent within 1 hour (14.4x the allowed rate)", with when it resolves in its help, and, once on, its own channels. Both start off, and one that is turned on needs a channel. The list gets an alert column with the worst state of the alerts that are on, each alert's state in its tooltip, and whether notifications are silenced. The SLO page gets an alerts panel with each alert's rule, channels and state, and when it fired in the last 7 days, with a link to change them in the builder. A duplicated SLO starts without alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2ebfd1a commit 2cc06b4

11 files changed

Lines changed: 499 additions & 13 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { type Control, type UseFormSetValue, useWatch } from 'react-hook-form';
2+
import {
3+
describeSloAlertRule,
4+
describeSloAlertRuleDetails,
5+
getSloAlertRuleLabel,
6+
SLO_BURN_CONDITIONS,
7+
type SloAlertRule,
8+
} from '@hyperdx/common-utils/dist/slo';
9+
import { Switch } from '@mantine/core';
10+
11+
import { AlertChannelForm } from '@/components/Alerts';
12+
13+
import { newSloAlertTarget, type SloFormValues } from './sloForm';
14+
import { SloFormRow } from './SloFormLayout';
15+
16+
/** One burn alert: its rule as the switch's label, and where it notifies. */
17+
function SloAlertRuleFields({
18+
rule,
19+
control,
20+
setValue,
21+
}: {
22+
rule: SloAlertRule;
23+
control: Control<SloFormValues>;
24+
setValue: UseFormSetValue<SloFormValues>;
25+
}) {
26+
const target = useWatch({ control, name: `alerts.${rule}` });
27+
const sloWindow = useWatch({ control, name: 'window' });
28+
return (
29+
<>
30+
<SloFormRow
31+
label={getSloAlertRuleLabel(rule)}
32+
helpText={describeSloAlertRuleDetails(rule)}
33+
inline
34+
>
35+
<Switch
36+
label={`Notify when ${describeSloAlertRule(rule, sloWindow)}`}
37+
checked={target != null}
38+
onChange={e =>
39+
setValue(
40+
`alerts.${rule}`,
41+
e.currentTarget.checked ? newSloAlertTarget() : null,
42+
{ shouldDirty: true },
43+
)
44+
}
45+
data-testid={`slo-alert-${rule}`}
46+
/>
47+
</SloFormRow>
48+
{target != null && (
49+
<SloFormRow label="Send to">
50+
<AlertChannelForm
51+
control={control}
52+
channelsName={`alerts.${rule}.channels`}
53+
/>
54+
</SloFormRow>
55+
)}
56+
</>
57+
);
58+
}
59+
60+
/** The fast and slow burn alerts, each on its own with its own channels. */
61+
export function SloAlertFields({
62+
control,
63+
setValue,
64+
}: {
65+
control: Control<SloFormValues>;
66+
setValue: UseFormSetValue<SloFormValues>;
67+
}) {
68+
return (
69+
<>
70+
{SLO_BURN_CONDITIONS.map(({ rule }) => (
71+
<SloAlertRuleFields
72+
key={rule}
73+
rule={rule}
74+
control={control}
75+
setValue={setValue}
76+
/>
77+
))}
78+
</>
79+
);
80+
}

‎packages/app/src/components/Slos/SloDetailPage.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { useSlo, useSlosEnabledGuard } from '@/slo';
2626
import { useSource } from '@/source';
2727

2828
import { SloCharts } from './SloCharts';
29-
import { SloDefinitionPanel } from './SloDetailPanels';
29+
import { SloAlertPanel, SloDefinitionPanel } from './SloDetailPanels';
3030
import {
3131
SloEventsDrawer,
3232
type SloEventsRange,
@@ -142,6 +142,7 @@ function SloDetailView({
142142
onBucketSelect={onBucketSelect}
143143
/>
144144
)}
145+
<SloAlertPanel slo={slo} />
145146
<SloDefinitionPanel slo={slo} sourceName={source?.name} />
146147
</Stack>
147148
</Container>

‎packages/app/src/components/Slos/SloDetailPanels.tsx‎

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1+
import Link from 'next/link';
12
import { formatDistanceToNowStrict } from 'date-fns';
23
import {
34
getSloBurnRule,
45
getSloGoodSql,
56
} from '@hyperdx/common-utils/dist/core/slo';
67
import { describeFilter } from '@hyperdx/common-utils/dist/filters';
7-
import type {
8-
SloApiResponse,
9-
SloState,
10-
SloStats,
8+
import {
9+
describeSloAlertRule,
10+
getSloAlertRuleLabel,
11+
getSloAlertTarget,
12+
SLO_BURN_CONDITIONS,
13+
type SloApiResponse,
14+
type SloState,
15+
type SloStats,
1116
} from '@hyperdx/common-utils/dist/slo';
12-
import { Code, Group, Paper, SimpleGrid, Stack, Text } from '@mantine/core';
17+
import { WebhookService } from '@hyperdx/common-utils/dist/types';
18+
import {
19+
Anchor,
20+
Code,
21+
Group,
22+
Paper,
23+
SimpleGrid,
24+
Stack,
25+
Text,
26+
} from '@mantine/core';
27+
28+
import api from '@/api';
1329

30+
import { SloRecentFirings } from './SloRecentFirings';
31+
import { SloAlertRuleBadge } from './SloStateBadge';
1432
import {
1533
budgetColorVar,
1634
describeBurnWindow,
@@ -20,6 +38,7 @@ import {
2038
formatSli,
2139
formatSloWindow,
2240
formatTarget,
41+
getSloAlertStatus,
2342
} from './sloUtils';
2443

2544
function Stat({
@@ -110,6 +129,59 @@ export function SloStatsGrid({
110129
);
111130
}
112131

132+
/**
133+
* The fast and slow burn alerts the SLO owns, each with its rule, channels
134+
* and recent firings. They are changed from the SLO's editor.
135+
*/
136+
export function SloAlertPanel({ slo }: { slo: SloApiResponse }) {
137+
const { data: webhooks } = api.useWebhooks([
138+
WebhookService.Slack,
139+
WebhookService.Generic,
140+
WebhookService.IncidentIO,
141+
]);
142+
const anyOn = SLO_BURN_CONDITIONS.some(({ rule }) =>
143+
getSloAlertTarget(slo.alerts, rule),
144+
);
145+
return (
146+
<Paper withBorder p="md" radius="sm" data-testid="slo-alert-panel">
147+
<Group justify="space-between" mb="xs">
148+
<Text fw={500}>Burn-rate alerts</Text>
149+
<Anchor component={Link} href={`/slos/${slo.id}/edit#alerts`} size="sm">
150+
{anyOn ? 'Edit' : 'Add an alert'}
151+
</Anchor>
152+
</Group>
153+
<Stack gap="md">
154+
{SLO_BURN_CONDITIONS.map(({ rule }) => {
155+
const target = getSloAlertTarget(slo.alerts, rule);
156+
const status = getSloAlertStatus(slo, rule);
157+
const sentence = describeSloAlertRule(rule, slo.window);
158+
const channels = (target?.channels ?? []).map(
159+
channel =>
160+
webhooks?.data?.find(w => w._id === channel.webhookId)?.name ??
161+
'a webhook',
162+
);
163+
return (
164+
<Stack key={rule} gap={4} data-testid={`slo-alert-panel-${rule}`}>
165+
<Group gap="xs">
166+
<Text size="sm" fw={500}>
167+
{getSloAlertRuleLabel(rule)}
168+
</Text>
169+
<SloAlertRuleBadge slo={slo} rule={rule} />
170+
</Group>
171+
<Text size="sm" c={target ? undefined : 'dimmed'}>
172+
{target
173+
? `Notifies ${channels.join(', ')} when ${sentence}.`
174+
: `Turn it on to be notified when ${sentence}.`}
175+
</Text>
176+
{target && status && <SloRecentFirings alertId={status.id} />}
177+
</Stack>
178+
);
179+
})}
180+
</Stack>
181+
</Paper>
182+
);
183+
}
184+
113185
export function SloDefinitionPanel({
114186
slo,
115187
sourceName,

‎packages/app/src/components/Slos/SloEditorPage.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { useSource } from '@/source';
3434
import { getApiErrorMessage } from '@/utils/apiErrors';
3535
import { parseAsJsonEncoded, parseAsStringEncoded } from '@/utils/queryParsers';
3636

37+
import { SloAlertFields } from './SloAlertFields';
3738
import {
3839
SloDetailsFields,
3940
SloIndicatorFields,
@@ -112,6 +113,10 @@ function SloEditorPage() {
112113
reset(sloFormValuesFromSearch({ ...prefill, source: prefill.source }));
113114
}
114115
initializedRef.current = true;
116+
// "Edit" on an SLO's alerts links here with #alerts.
117+
if (window.location.hash === '#alerts') {
118+
document.getElementById('alerts')?.scrollIntoView();
119+
}
115120
}, [router.isReady, editId, existing, duplicateId, original, prefill, reset]);
116121

117122
const [preview, setPreview] = useState<SloPreviewRequest | null>(null);
@@ -242,6 +247,13 @@ function SloEditorPage() {
242247
<SloFormSection title="Objective">
243248
<SloObjectiveFields control={control} setValue={setValue} />
244249
</SloFormSection>
250+
<SloFormSection
251+
id="alerts"
252+
title="Burn-rate alerts"
253+
description="Optional. Each alert notifies its own channels once when a burn starts and once when it ends."
254+
>
255+
<SloAlertFields control={control} setValue={setValue} />
256+
</SloFormSection>
245257
<SloFormSection title="Details">
246258
<SloDetailsFields control={control} />
247259
</SloFormSection>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { useMemo, useState } from 'react';
2+
import { formatDistanceStrict } from 'date-fns';
3+
import {
4+
AlertState,
5+
type AlertTransition,
6+
} from '@hyperdx/common-utils/dist/types';
7+
import { Skeleton, Stack, Table, Text } from '@mantine/core';
8+
9+
import api from '@/api';
10+
import { FormatTime } from '@/useFormatTime';
11+
12+
const RECENT_FIRINGS_MS = 7 * 24 * 60 * 60 * 1000;
13+
const MAX_FIRINGS_SHOWN = 10;
14+
15+
export type AlertEpisode = { start: string; end?: string };
16+
17+
/**
18+
* The alert's firing episodes, newest first, from its state changes: each
19+
* firing to the next recovery, or still open. Times are evaluation times,
20+
* when the notifications went out.
21+
*/
22+
export function getAlertEpisodes(
23+
transitions: AlertTransition[],
24+
): AlertEpisode[] {
25+
const episodes: AlertEpisode[] = [];
26+
let start: string | undefined;
27+
for (const { state, createdAt } of transitions) {
28+
if (state === AlertState.ALERT) {
29+
start ??= createdAt;
30+
} else if (start != null) {
31+
episodes.push({ start, end: createdAt });
32+
start = undefined;
33+
}
34+
}
35+
if (start != null) episodes.push({ start });
36+
return episodes.reverse();
37+
}
38+
39+
/**
40+
* When one of the SLO's burn alerts fired in the last 7 days, newest
41+
* first.
42+
*/
43+
export function SloRecentFirings({ alertId }: { alertId: string }) {
44+
const [dateRange] = useState<[Date, Date]>(() => [
45+
new Date(Date.now() - RECENT_FIRINGS_MS),
46+
new Date(),
47+
]);
48+
const { data, isLoading } = api.useAlertHistory(alertId, dateRange);
49+
const episodes = useMemo(() => getAlertEpisodes(data?.data ?? []), [data]);
50+
51+
if (isLoading) return <Skeleton h={40} />;
52+
if (episodes.length === 0) {
53+
return (
54+
<Text size="sm" c="dimmed" data-testid="slo-recent-firings">
55+
Did not fire in the last 7 days.
56+
</Text>
57+
);
58+
}
59+
const hidden = episodes.length - MAX_FIRINGS_SHOWN;
60+
return (
61+
<Stack gap={4} data-testid="slo-recent-firings">
62+
<Text size="sm" fw={500}>
63+
Fired {episodes.length === 1 ? 'once' : `${episodes.length} times`} in
64+
the last 7 days
65+
</Text>
66+
<Table verticalSpacing={4} withRowBorders={false} maw={560}>
67+
<Table.Thead>
68+
<Table.Tr>
69+
<Table.Th>Fired</Table.Th>
70+
<Table.Th>Resolved</Table.Th>
71+
<Table.Th>For</Table.Th>
72+
</Table.Tr>
73+
</Table.Thead>
74+
<Table.Tbody>
75+
{episodes.slice(0, MAX_FIRINGS_SHOWN).map(({ start, end }) => (
76+
<Table.Tr key={start}>
77+
<Table.Td>
78+
<FormatTime value={start} />
79+
</Table.Td>
80+
<Table.Td>
81+
{end ? (
82+
<FormatTime value={end} />
83+
) : (
84+
<Text span size="sm" c="var(--color-text-danger)">
85+
Still firing
86+
</Text>
87+
)}
88+
</Table.Td>
89+
<Table.Td>
90+
{/* An open episode runs until the history was read. */}
91+
{formatDistanceStrict(
92+
new Date(start),
93+
end ? new Date(end) : dateRange[1],
94+
)}
95+
</Table.Td>
96+
</Table.Tr>
97+
))}
98+
</Table.Tbody>
99+
</Table>
100+
{hidden > 0 && (
101+
<Text size="xs" c="dimmed">
102+
{hidden} earlier {hidden === 1 ? 'firing is' : 'firings are'} not
103+
shown.
104+
</Text>
105+
)}
106+
</Stack>
107+
);
108+
}

0 commit comments

Comments
 (0)