Skip to content

Commit e55880c

Browse files
committed
feedback: enrich eventId (dedupe key) + fetch state from alert history
1 parent 260254c commit e55880c

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ describe('checkAlerts', () => {
316316
alertProvider,
317317
clickhouseClient: {} as any,
318318
metadata: {} as any,
319+
state: 'ALERT',
319320
template: 'Custom body @webhook-My_Web', // partial name should work
320321
view: {
321322
...defaultSearchView,
@@ -354,6 +355,7 @@ describe('checkAlerts', () => {
354355
alertProvider,
355356
clickhouseClient: {} as any,
356357
metadata: {} as any,
358+
state: 'ALERT',
357359
template: 'Custom body @webhook-My_Web', // partial name should work
358360
view: {
359361
...defaultSearchView,
@@ -414,6 +416,7 @@ describe('checkAlerts', () => {
414416
alertProvider,
415417
clickhouseClient: {} as any,
416418
metadata: {} as any,
419+
state: 'ALERT',
417420
template: 'Custom body @webhook-{{attributes.webhookName}}', // partial name should work
418421
view: {
419422
...defaultSearchView,
@@ -485,6 +488,7 @@ describe('checkAlerts', () => {
485488
alertProvider,
486489
clickhouseClient: {} as any,
487490
metadata: {} as any,
491+
state: 'ALERT',
488492
template: `
489493
{{#is_match "attributes.k8s.pod.name" "otel-collector-123"}}
490494
Runbook URL: {{attributes.runbook.url}}
@@ -522,6 +526,7 @@ describe('checkAlerts', () => {
522526
alertProvider,
523527
clickhouseClient: {} as any,
524528
metadata: {} as any,
529+
state: 'ALERT',
525530
template:
526531
'{{#is_match "attributes.host" "web"}} @webhook-My_Web {{/is_match}}', // partial name should work
527532
view: {

packages/api/src/tasks/checkAlerts/index.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ export const processAlert = async (
160160
) => {
161161
const { alert, source, previous } = details;
162162
try {
163-
// Fetch full previous alert history to check state
164-
let previousAlertHistory: IAlertHistory | null = null;
165-
if (previous) {
166-
previousAlertHistory = await AlertHistory.findOne({
167-
alert: new mongoose.Types.ObjectId(alert.id),
168-
createdAt: previous.createdAt,
169-
});
170-
}
171163
const windowSizeInMins = ms(alert.interval) / 60000;
172164
const nowInMinsRoundDown = roundDownToXMinutes(windowSizeInMins)(now);
173165
if (
@@ -391,7 +383,7 @@ export const processAlert = async (
391383

392384
// Check if alert transitioned from ALERT to OK (resolved)
393385
if (
394-
previousAlertHistory?.state === AlertState.ALERT &&
386+
previous?.state === AlertState.ALERT &&
395387
history.state === AlertState.OK
396388
) {
397389
logger.info({
@@ -448,6 +440,7 @@ export { handleSendGenericWebhook };
448440
export interface AggregatedAlertHistory {
449441
_id: ObjectId;
450442
createdAt: Date;
443+
state: AlertState;
451444
}
452445

453446
/**
@@ -478,11 +471,16 @@ export const getPreviousAlertHistories = async (
478471
createdAt: { $lte: now },
479472
},
480473
},
481-
// Group by alert ID, taking the latest createdAt value for each group
474+
// Sort by createdAt descending to get the latest first
475+
{
476+
$sort: { createdAt: -1 },
477+
},
478+
// Group by alert ID, taking the first (latest) values for each group
482479
{
483480
$group: {
484481
_id: '$alert',
485-
createdAt: { $max: '$createdAt' },
482+
createdAt: { $first: '$createdAt' },
483+
state: { $first: '$state' },
486484
},
487485
},
488486
]),

packages/api/src/tasks/checkAlerts/template.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ export const renderAlertTemplate = async ({
478478
const endTime = view.endTime.getTime();
479479
const eventId = objectHash({
480480
alertId: alert.id,
481+
channel: {
482+
type: channel.type,
483+
id: channel.channel._id.toString(),
484+
},
481485
});
482486

483487
await notifyChannel({

0 commit comments

Comments
 (0)