Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/alert-renotify-interval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/common-utils': minor
'@hyperdx/app': minor
'@hyperdx/api': minor
---

feat: alerts get an opt-in "Re-notify every N minutes" setting (`renotifyIntervalMinutes`). While an alert keeps firing it notifies at most once per interval; unset keeps today's behaviour of notifying on every evaluation. Resolve notifications are unchanged.
7 changes: 7 additions & 0 deletions packages/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@
"nullable": true,
"description": "Fire the alert only after its condition has been met for this many consecutive evaluation windows. While the condition is met but fewer than this many consecutive windows have violated, the alert is in the PENDING state.",
"example": 3
},
"renotifyIntervalMinutes": {
"type": "integer",
"minimum": 1,
"nullable": true,
"description": "While the alert keeps firing, repeat its notification at most once every this many minutes. Omit or set to null to notify on every evaluation. A resolve notification is always sent when the alert returns to OK.",
"example": 60
}
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/controllers/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export const makeAlert = (

// Multi-window alerting
numConsecutiveWindows: alert.numConsecutiveWindows ?? null,
renotifyIntervalMinutes: alert.renotifyIntervalMinutes ?? null,
};
};

Expand Down
21 changes: 18 additions & 3 deletions packages/api/src/mcp/__tests__/alerts.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ describe('MCP Alert Tools', () => {
expect(updatedAlert?.tileId).toBeNull();
});

describe('note and numConsecutiveWindows', () => {
describe('note, numConsecutiveWindows and renotifyIntervalMinutes', () => {
async function saveExisting(
alertId: string,
savedSearchId: string,
Expand All @@ -1412,6 +1412,7 @@ describe('MCP Alert Tools', () => {
const alert = await createTestAlert({
note: 'Set in the UI',
numConsecutiveWindows: 4,
renotifyIntervalMinutes: 30,
});

const result = await saveExisting(
Expand All @@ -1425,46 +1426,60 @@ describe('MCP Alert Tools', () => {
expect(output.threshold).toBe(200);
expect(output.note).toBe('Set in the UI');
expect(output.numConsecutiveWindows).toBe(4);
const persisted = await Alert.findById(alert._id);
expect(persisted?.renotifyIntervalMinutes).toBe(30);
});

it('replaces the stored values when provided', async () => {
const webhook = await createTestWebhook();
const alert = await createTestAlert({
note: 'Set in the UI',
numConsecutiveWindows: 4,
renotifyIntervalMinutes: 30,
});

const result = await saveExisting(
alert._id.toString(),
alert.savedSearch!.toString(),
webhook._id.toString(),
{ note: 'Updated note', numConsecutiveWindows: 2 },
{
note: 'Updated note',
numConsecutiveWindows: 2,
renotifyIntervalMinutes: 60,
},
);

expect(result.isError).toBeFalsy();
const persisted = await Alert.findById(alert._id);
expect(persisted?.note).toBe('Updated note');
expect(persisted?.numConsecutiveWindows).toBe(2);
expect(persisted?.renotifyIntervalMinutes).toBe(60);
});

it('clears the stored values when sent as null', async () => {
const webhook = await createTestWebhook();
const alert = await createTestAlert({
note: 'Set in the UI',
numConsecutiveWindows: 4,
renotifyIntervalMinutes: 30,
});

const result = await saveExisting(
alert._id.toString(),
alert.savedSearch!.toString(),
webhook._id.toString(),
{ note: null, numConsecutiveWindows: null },
{
note: null,
numConsecutiveWindows: null,
renotifyIntervalMinutes: null,
},
);

expect(result.isError).toBeFalsy();
const persisted = await Alert.findById(alert._id);
expect(persisted?.note).toBeNull();
expect(persisted?.numConsecutiveWindows).toBeNull();
expect(persisted?.renotifyIntervalMinutes).toBeNull();
});
});

Expand Down
12 changes: 9 additions & 3 deletions packages/api/src/mcp/tools/alerts/saveAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export function registerSaveAlert({
'(webhook IDs come from clickstack_get_webhook). Updates replace the ' +
'alert configuration rather than merging it: channels you do not resend are ' +
'removed, omitted name and message are cleared, and omitted displayName ' +
'and tags are re-derived from the source. Omitted note and ' +
'numConsecutiveWindows keep their stored values. Omitting both ' +
'and tags are re-derived from the source. Omitted note, ' +
'numConsecutiveWindows and renotifyIntervalMinutes keep their stored ' +
'values. Omitting both ' +
'scheduleStartAt and scheduleOffsetMinutes keeps the stored schedule; ' +
'clearing scheduleStartAt without an offset resets the offset to 0. Read the alert with ' +
'clickstack_get_alert first and resend every field you want to keep.',
Expand Down Expand Up @@ -123,14 +124,19 @@ export function registerSaveAlert({
scheduleStartAt: input.scheduleStartAt,
name: input.name,
message: input.message,
// Unlike name and message, an omitted note or window count keeps the
// Unlike name and message, an omitted note, window count or re-notify
// interval keeps the
// stored value: callers written before this tool accepted them would
// otherwise wipe what was set in the UI. null still clears.
note: input.note === undefined ? existing?.note : input.note,
numConsecutiveWindows:
input.numConsecutiveWindows === undefined
? existing?.numConsecutiveWindows
: input.numConsecutiveWindows,
renotifyIntervalMinutes:
input.renotifyIntervalMinutes === undefined
? existing?.renotifyIntervalMinutes
: input.renotifyIntervalMinutes,
displayName: input.displayName,
tags: input.tags,
groupBy: input.groupBy,
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/mcp/tools/alerts/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type AlertInterval,
alertNoteSchema,
alertNumConsecutiveWindowsSchema,
alertRenotifyIntervalMinutesSchema,
checkAlertChannelSelection,
isRangeThresholdType,
MAX_ALERT_CHANNELS,
Expand Down Expand Up @@ -234,6 +235,11 @@ export const mcpSaveAlertSchema = z.object({
'boundary. Must be less than the interval. Ignored when ' +
'scheduleStartAt is set.',
),
renotifyIntervalMinutes: alertRenotifyIntervalMinutesSchema.describe(
'While the alert keeps firing, repeat its notification at most once ' +
'every N minutes. null notifies on every evaluation; omitting it ' +
'keeps the stored value. A resolve notification is always sent.',
),
scheduleStartAt: z
.string()
.datetime()
Expand Down
8 changes: 8 additions & 0 deletions packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export interface IAlert {
// Multi-window alerting: fire only after N violations in M consecutive windows
numConsecutiveWindows?: number | null;

// While firing, notify at most every N minutes; unset notifies on every evaluation.
renotifyIntervalMinutes?: number | null;

// Errors recorded during the most recent execution
executionErrors?: IAlertError[];
createdAt: Date;
Expand Down Expand Up @@ -256,6 +259,11 @@ const AlertSchema = new Schema<IAlert>(
required: false,
min: 1,
},
renotifyIntervalMinutes: {
type: Number,
required: false,
min: 1,
},
silenced: {
required: false,
type: {
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/models/alertHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface IAlertHistory {
errors?: IAlertError[];
/** Diagnostics for the evaluation that wrote this record. */
analytics?: IAlertHistoryAnalytics;
lastNotifiedAt?: Date;
}

const AlertHistorySchema = new Schema<IAlertHistory>({
Expand Down Expand Up @@ -162,6 +163,10 @@ const AlertHistorySchema = new Schema<IAlertHistory>({
required: false,
default: undefined,
},
lastNotifiedAt: {
type: Date,
required: false,
},
});

AlertHistorySchema.index(
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/routers/api/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const formatAlertResponse = (
'updatedAt',
'executionErrors',
'numConsecutiveWindows',
'renotifyIntervalMinutes',
]),
tileId: alert.tileId ?? undefined,
groupBy: alert.groupBy ?? undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ describe('External API Alerts', () => {
message: 'This is a test alert for format verification',
note: null,
numConsecutiveWindows: null,
renotifyIntervalMinutes: null,
threshold: 123,
interval: '15m',
source: AlertSource.TILE,
Expand Down Expand Up @@ -3173,6 +3174,42 @@ describe('External API Alerts', () => {
expect(getResponse.body.data.numConsecutiveWindows).toBeNull();
});
});
describe('Re-notification interval (renotifyIntervalMinutes)', () => {
it('should create an alert with renotifyIntervalMinutes and return it on GET', async () => {
const { alert } = await createTestAlert({ renotifyIntervalMinutes: 60 });
expect(alert.renotifyIntervalMinutes).toBe(60);

const getResponse = await authRequest(
'get',
`${ALERTS_BASE_URL}/${alert.id}`,
).expect(200);
expect(getResponse.body.data.renotifyIntervalMinutes).toBe(60);
});

it('should update an alert to set and then clear renotifyIntervalMinutes', async () => {
const { alert, alertInput } = await createTestAlert();
expect(alert.renotifyIntervalMinutes).toBeNull();

const getRenotify = async () =>
(await authRequest('get', `${ALERTS_BASE_URL}/${alert.id}`).expect(200))
.body.data.renotifyIntervalMinutes;

await authRequest('put', `${ALERTS_BASE_URL}/${alert.id}`)
.send({ ...alertInput, renotifyIntervalMinutes: 30 })
.expect(200);
expect(await getRenotify()).toBe(30);

// Unset already means every evaluation, so 0 has no meaning.
await authRequest('put', `${ALERTS_BASE_URL}/${alert.id}`)
.send({ ...alertInput, renotifyIntervalMinutes: 0 })
.expect(400);

await authRequest('put', `${ALERTS_BASE_URL}/${alert.id}`)
.send({ ...alertInput, renotifyIntervalMinutes: null })
.expect(200);
expect(await getRenotify()).toBeNull();
});
});
describe('Multiple notification channels', () => {
const makeWebhookNamed = async (name: string) =>
await Webhook.findOneAndUpdate(
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/routers/external-api/v2/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ function toInternalAlertInput(body: ExternalAlertInput): InternalAlertInput {
* nullable: true
* description: Fire the alert only after its condition has been met for this many consecutive evaluation windows. While the condition is met but fewer than this many consecutive windows have violated, the alert is in the PENDING state.
* example: 3
* renotifyIntervalMinutes:
* type: integer
* minimum: 1
* nullable: true
* description: While the alert keeps firing, repeat its notification at most once every this many minutes. Omit or set to null to notify on every evaluation. A resolve notification is always sent when the alert returns to OK.
* example: 60
*
* AlertResponse:
* allOf:
Expand Down
Loading
Loading