Skip to content

Commit 1dd1ec5

Browse files
authored
[ResponseOps] Adding back recovered alert optimization for the task state (#215344)
## Summary I refactored the flapping code on ON week in PR #213825 , and I removed an optimization for removing recovered alerts from the task state that we don't need to track for flapping. This PR adds it back. ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### To verify 1. Create an rule let it be active for a little and then let it recover. 2. Verify that once the flapping array doesn't have any `true` values changes
1 parent f5424e0 commit 1dd1ec5

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

x-pack/platform/plugins/shared/alerting/server/alerts_client/alerts_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ export class AlertsClient<
345345
return await this.updatePersistedAlertsWithMaintenanceWindowIds();
346346
}
347347

348-
public getRawAlertInstancesForState() {
349-
return this.legacyAlertsClient.getRawAlertInstancesForState();
348+
public getRawAlertInstancesForState(shouldOptimizeTaskState?: boolean) {
349+
return this.legacyAlertsClient.getRawAlertInstancesForState(shouldOptimizeTaskState);
350350
}
351351

352352
public factory() {

x-pack/platform/plugins/shared/alerting/server/alerts_client/legacy_alerts_client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ export class LegacyAlertsClient<
211211
return {};
212212
}
213213

214-
public getRawAlertInstancesForState() {
214+
public getRawAlertInstancesForState(shouldOptimizeTaskState?: boolean) {
215215
return toRawAlertInstances<State, Context, ActionGroupIds, RecoveryActionGroupId>(
216216
this.processedAlerts.trackedActiveAlerts,
217-
this.processedAlerts.trackedRecoveredAlerts
217+
this.processedAlerts.trackedRecoveredAlerts,
218+
shouldOptimizeTaskState
218219
);
219220
}
220221

x-pack/platform/plugins/shared/alerting/server/alerts_client/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface IAlertsClient<
8585
persistAlerts(): Promise<{ alertIds: string[]; maintenanceWindowIds: string[] } | null>;
8686
isTrackedAlert(id: string): boolean;
8787
getSummarizedAlerts?(params: GetSummarizedAlertsParams): Promise<SummarizedAlerts>;
88-
getRawAlertInstancesForState(): {
88+
getRawAlertInstancesForState(shouldOptimizeTaskState?: boolean): {
8989
rawActiveAlerts: Record<string, RawAlertInstance>;
9090
rawRecoveredAlerts: Record<string, RawAlertInstance>;
9191
};

x-pack/platform/plugins/shared/alerting/server/lib/to_raw_alert_instances.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ describe('toRawAlertInstances', () => {
3131
const { rawRecoveredAlerts } = toRawAlertInstances({}, recoveredAlerts);
3232
expect(keys(rawRecoveredAlerts)).toEqual(['1', '2']);
3333
});
34+
35+
test('should return all flapping recovered alerts', () => {
36+
const recoveredAlerts = {
37+
'1': new Alert('1', { meta: { flappingHistory: flapping } }),
38+
'2': new Alert('2', { meta: { flappingHistory: notFlapping } }),
39+
};
40+
const { rawRecoveredAlerts } = toRawAlertInstances({}, recoveredAlerts, true);
41+
expect(keys(rawRecoveredAlerts)).toEqual(['1']);
42+
});
3443
});
3544
});

x-pack/platform/plugins/shared/alerting/server/lib/to_raw_alert_instances.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function toRawAlertInstances<
1616
RecoveryActionGroupId extends string
1717
>(
1818
activeAlerts: Record<string, Alert<State, Context, ActionGroupIds>> = {},
19-
recoveredAlerts: Record<string, Alert<State, Context, RecoveryActionGroupId>> = {}
19+
recoveredAlerts: Record<string, Alert<State, Context, RecoveryActionGroupId>> = {},
20+
shouldOptimizeTaskState: boolean = false
2021
): {
2122
rawActiveAlerts: Record<string, RawAlertInstance>;
2223
rawRecoveredAlerts: Record<string, RawAlertInstance>;
@@ -30,7 +31,20 @@ export function toRawAlertInstances<
3031

3132
for (const id of keys(recoveredAlerts)) {
3233
const alert = recoveredAlerts[id];
33-
rawRecoveredAlerts[id] = alert.toRaw(true);
34+
if (shouldOptimizeTaskState) {
35+
// this is a space saving effort that will only return recovered alerts if they are flapping
36+
// or if the flapping array contains any state changes
37+
const flapping = alert.getFlapping();
38+
const flappingHistory: boolean[] = alert.getFlappingHistory() || [];
39+
const numStateChanges = flappingHistory.filter((f) => f).length;
40+
if (flapping) {
41+
rawRecoveredAlerts[id] = alert.toRaw(true);
42+
} else if (numStateChanges > 0) {
43+
rawRecoveredAlerts[id] = alert.toRaw(true);
44+
}
45+
} else {
46+
rawRecoveredAlerts[id] = alert.toRaw(true);
47+
}
3448
}
3549
return { rawActiveAlerts, rawRecoveredAlerts };
3650
}

x-pack/platform/plugins/shared/alerting/server/task_runner/task_runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export class TaskRunner<
432432
// Only serialize alerts into task state if we're auto-recovering, otherwise
433433
// we don't need to keep this information around.
434434
if (this.ruleType.autoRecoverAlerts) {
435-
const alerts = alertsClient.getRawAlertInstancesForState();
435+
const alerts = alertsClient.getRawAlertInstancesForState(true);
436436
alertsToReturn = alerts.rawActiveAlerts;
437437
recoveredAlertsToReturn = alerts.rawRecoveredAlerts;
438438
}

0 commit comments

Comments
 (0)