Skip to content

Commit 3e00bb0

Browse files
committed
feedback: enrich eventId (dedupe key) + fetch state from alert history
1 parent badb769 commit 3e00bb0

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

packages/api/src/tasks/__tests__/checkAlerts.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ describe('checkAlerts', () => {
312312
alertProvider,
313313
clickhouseClient: {} as any,
314314
metadata: {} as any,
315+
state: 'ALERT',
315316
template: 'Custom body @webhook-My_Web', // partial name should work
316317
view: {
317318
...defaultSearchView,
@@ -350,6 +351,7 @@ describe('checkAlerts', () => {
350351
alertProvider,
351352
clickhouseClient: {} as any,
352353
metadata: {} as any,
354+
state: 'ALERT',
353355
template: 'Custom body @webhook-My_Web', // partial name should work
354356
view: {
355357
...defaultSearchView,
@@ -410,6 +412,7 @@ describe('checkAlerts', () => {
410412
alertProvider,
411413
clickhouseClient: {} as any,
412414
metadata: {} as any,
415+
state: 'ALERT',
413416
template: 'Custom body @webhook-{{attributes.webhookName}}', // partial name should work
414417
view: {
415418
...defaultSearchView,
@@ -481,6 +484,7 @@ describe('checkAlerts', () => {
481484
alertProvider,
482485
clickhouseClient: {} as any,
483486
metadata: {} as any,
487+
state: 'ALERT',
484488
template: `
485489
{{#is_match "attributes.k8s.pod.name" "otel-collector-123"}}
486490
Runbook URL: {{attributes.runbook.url}}
@@ -518,6 +522,7 @@ describe('checkAlerts', () => {
518522
alertProvider,
519523
clickhouseClient: {} as any,
520524
metadata: {} as any,
525+
state: 'ALERT',
521526
template:
522527
'{{#is_match "attributes.host" "web"}} @webhook-My_Web {{/is_match}}', // partial name should work
523528
view: {

packages/api/src/tasks/checkAlerts.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ export const processAlert = async (
157157
) => {
158158
const { alert, source, previous } = details;
159159
try {
160-
// Fetch full previous alert history to check state
161-
let previousAlertHistory: IAlertHistory | null = null;
162-
if (previous) {
163-
previousAlertHistory = await AlertHistory.findOne({
164-
alert: new mongoose.Types.ObjectId(alert.id),
165-
createdAt: previous.createdAt,
166-
});
167-
}
168160
const windowSizeInMins = ms(alert.interval) / 60000;
169161
const nowInMinsRoundDown = roundDownToXMinutes(windowSizeInMins)(now);
170162
if (
@@ -383,7 +375,7 @@ export const processAlert = async (
383375

384376
// Check if alert transitioned from ALERT to OK (resolved)
385377
if (
386-
previousAlertHistory?.state === AlertState.ALERT &&
378+
previous?.state === AlertState.ALERT &&
387379
history.state === AlertState.OK
388380
) {
389381
logger.info({
@@ -440,6 +432,7 @@ export { handleSendGenericWebhook };
440432
export interface AggregatedAlertHistory {
441433
_id: ObjectId;
442434
createdAt: Date;
435+
state: AlertState;
443436
}
444437

445438
/**
@@ -470,11 +463,16 @@ export const getPreviousAlertHistories = async (
470463
createdAt: { $lte: now },
471464
},
472465
},
473-
// Group by alert ID, taking the latest createdAt value for each group
466+
// Sort by createdAt descending to get the latest first
467+
{
468+
$sort: { createdAt: -1 },
469+
},
470+
// Group by alert ID, taking the first (latest) values for each group
474471
{
475472
$group: {
476473
_id: '$alert',
477-
createdAt: { $max: '$createdAt' },
474+
createdAt: { $first: '$createdAt' },
475+
state: { $first: '$state' },
478476
},
479477
},
480478
]),

packages/api/src/tasks/template.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ export const renderAlertTemplate = async ({
475475
const endTime = view.endTime.getTime();
476476
const eventId = objectHash({
477477
alertId: alert.id,
478+
channel: {
479+
type: channel.type,
480+
id: channel.channel._id.toString(),
481+
},
478482
});
479483

480484
await notifyChannel({

0 commit comments

Comments
 (0)