Skip to content

Commit c1a375a

Browse files
committed
do not overwrite rule configuration for recovered alerts
1 parent 52a5434 commit c1a375a

5 files changed

Lines changed: 61 additions & 26 deletions

File tree

x-pack/plugins/alerting/server/alerts_client/alerts_client.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
LogAlertsOpts,
6161
} from './types';
6262
import { legacyAlertsClientMock } from './legacy_alerts_client.mock';
63-
import { keys, range } from 'lodash';
63+
import { keys, omit, range } from 'lodash';
6464
import { alertingEventLoggerMock } from '../lib/alerting_event_logger/alerting_event_logger.mock';
6565
import { ruleRunMetricsStoreMock } from '../lib/rule_run_metrics_store.mock';
6666
import { expandFlattenedAlert } from './lib';
@@ -79,6 +79,7 @@ import { MaintenanceWindow } from '../application/maintenance_window/types';
7979
import { maintenanceWindowsServiceMock } from '../task_runner/maintenance_windows/maintenance_windows_service.mock';
8080
import { getMockMaintenanceWindow } from '../data/maintenance_window/test_helpers';
8181
import { KibanaRequest } from '@kbn/core/server';
82+
import { rule } from './lib/test_fixtures';
8283

8384
const date = '2023-03-28T22:27:28.159Z';
8485
const startedAtDate = '2023-03-28T13:00:00.000Z';
@@ -1107,11 +1108,13 @@ describe('Alerts Client', () => {
11071108
kibana: {
11081109
alert: {
11091110
instance: { id: '1' },
1111+
rule: omit(rule, 'execution'),
11101112
uuid: 'abc',
11111113
},
11121114
},
11131115
[TIMESTAMP]: date,
11141116
[ALERT_RULE_EXECUTION_TIMESTAMP]: date,
1117+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
11151118
[EVENT_ACTION]: 'close',
11161119
[ALERT_ACTION_GROUP]: 'recovered',
11171120
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -1120,16 +1123,6 @@ describe('Alerts Client', () => {
11201123
[ALERT_FLAPPING_HISTORY]: [true, true],
11211124
[ALERT_MAINTENANCE_WINDOW_IDS]: [],
11221125
[ALERT_PREVIOUS_ACTION_GROUP]: 'default',
1123-
[ALERT_RULE_CATEGORY]: 'My test rule',
1124-
[ALERT_RULE_CONSUMER]: 'bar',
1125-
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
1126-
[ALERT_RULE_NAME]: 'rule-name',
1127-
[ALERT_RULE_PARAMETERS]: { bar: true },
1128-
[ALERT_RULE_PRODUCER]: 'alerts',
1129-
[ALERT_RULE_REVISION]: 0,
1130-
[ALERT_RULE_TYPE_ID]: 'test.rule-type',
1131-
[ALERT_RULE_TAGS]: ['rule-', '-tags'],
1132-
[ALERT_RULE_UUID]: '1',
11331126
[ALERT_SEVERITY_IMPROVING]: true,
11341127
[ALERT_START]: '2023-03-28T12:27:28.159Z',
11351128
[ALERT_END]: date,

x-pack/plugins/alerting/server/alerts_client/lib/build_recovered_alert.test.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ import {
3131
ALERT_RULE_EXECUTION_TIMESTAMP,
3232
ALERT_PREVIOUS_ACTION_GROUP,
3333
ALERT_SEVERITY_IMPROVING,
34+
ALERT_RULE_EXECUTION_UUID,
3435
} from '@kbn/rule-data-utils';
3536
import {
3637
alertRule,
3738
existingFlattenedActiveAlert,
3839
existingExpandedActiveAlert,
40+
rule,
3941
} from './test_fixtures';
42+
import { omit } from 'lodash';
4043

4144
for (const flattened of [true, false]) {
4245
const existingAlert = flattened ? existingFlattenedActiveAlert : existingExpandedActiveAlert;
@@ -63,9 +66,10 @@ for (const flattened of [true, false]) {
6366
kibanaVersion: '8.9.0',
6467
})
6568
).toEqual({
66-
...alertRule,
6769
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
6870
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
71+
// @ts-ignore
72+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
6973
[EVENT_ACTION]: 'close',
7074
[ALERT_ACTION_GROUP]: 'recovered',
7175
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -80,11 +84,13 @@ for (const flattened of [true, false]) {
8084
[ALERT_START]: '2023-03-28T12:27:28.159Z',
8185
[ALERT_END]: '2023-03-30T12:27:28.159Z',
8286
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
87+
// @ts-ignore
8388
[SPACE_IDS]: ['default'],
8489
[VERSION]: '8.9.0',
8590
[TAGS]: ['rule-', '-tags'],
8691
...(flattened
8792
? {
93+
...alertRule,
8894
[EVENT_KIND]: 'signal',
8995
[ALERT_INSTANCE_ID]: 'alert-A',
9096
[ALERT_UUID]: 'abcdefg',
@@ -96,14 +102,15 @@ for (const flattened of [true, false]) {
96102
kibana: {
97103
alert: {
98104
instance: { id: 'alert-A' },
105+
rule: omit(rule, 'execution'),
99106
uuid: 'abcdefg',
100107
},
101108
},
102109
}),
103110
});
104111
});
105112

106-
test('should return alert document with recovery status and updated rule data if rule definition has changed', () => {
113+
test('should return alert document with recovery status and but not update rule data if rule definition has changed', () => {
107114
const legacyAlert = new LegacyAlert<{}, {}, 'default'>('alert-A', {
108115
meta: { uuid: 'abcdefg' },
109116
});
@@ -131,9 +138,10 @@ for (const flattened of [true, false]) {
131138
kibanaVersion: '8.9.0',
132139
})
133140
).toEqual({
134-
...updatedRule,
135141
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
136142
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
143+
// @ts-ignore
144+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
137145
[EVENT_ACTION]: 'close',
138146
[ALERT_ACTION_GROUP]: 'NoLongerActive',
139147
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -148,11 +156,13 @@ for (const flattened of [true, false]) {
148156
[ALERT_START]: '2023-03-28T12:27:28.159Z',
149157
[ALERT_END]: '2023-03-30T12:27:28.159Z',
150158
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
159+
// @ts-ignore
151160
[SPACE_IDS]: ['default'],
152161
[VERSION]: '8.9.0',
153162
[TAGS]: ['rule-', '-tags'],
154163
...(flattened
155164
? {
165+
...alertRule,
156166
[EVENT_KIND]: 'signal',
157167
[ALERT_INSTANCE_ID]: 'alert-A',
158168
[ALERT_UUID]: 'abcdefg',
@@ -164,6 +174,7 @@ for (const flattened of [true, false]) {
164174
kibana: {
165175
alert: {
166176
instance: { id: 'alert-A' },
177+
rule: omit(rule, 'execution'),
167178
uuid: 'abcdefg',
168179
},
169180
},
@@ -226,12 +237,13 @@ for (const flattened of [true, false]) {
226237
kibanaVersion: '8.9.0',
227238
})
228239
).toEqual({
229-
...alertRule,
230240
count: 2,
231241
url: `https://url2`,
232242
'kibana.alert.nested_field': 2,
233243
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
234244
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
245+
// @ts-ignore
246+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
235247
[EVENT_ACTION]: 'close',
236248
[ALERT_ACTION_GROUP]: 'NoLongerActive',
237249
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -246,11 +258,13 @@ for (const flattened of [true, false]) {
246258
[ALERT_START]: '2023-03-28T12:27:28.159Z',
247259
[ALERT_END]: '2023-03-30T12:27:28.159Z',
248260
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
261+
// @ts-ignore
249262
[SPACE_IDS]: ['default'],
250263
[VERSION]: '8.9.0',
251264
[TAGS]: ['rule-', '-tags'],
252265
...(flattened
253266
? {
267+
...alertRule,
254268
[EVENT_KIND]: 'signal',
255269
[ALERT_INSTANCE_ID]: 'alert-A',
256270
[ALERT_UUID]: 'abcdefg',
@@ -262,6 +276,7 @@ for (const flattened of [true, false]) {
262276
kibana: {
263277
alert: {
264278
instance: { id: 'alert-A' },
279+
rule: omit(rule, 'execution'),
265280
uuid: 'abcdefg',
266281
},
267282
},
@@ -291,9 +306,10 @@ for (const flattened of [true, false]) {
291306
kibanaVersion: '8.9.0',
292307
})
293308
).toEqual({
294-
...alertRule,
295309
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
296310
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2030-12-15T02:44:13.124Z',
311+
// @ts-ignore
312+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
297313
[EVENT_ACTION]: 'close',
298314
[ALERT_ACTION_GROUP]: 'recovered',
299315
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -308,11 +324,13 @@ for (const flattened of [true, false]) {
308324
[ALERT_START]: '2023-03-28T12:27:28.159Z',
309325
[ALERT_END]: '2023-03-30T12:27:28.159Z',
310326
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
327+
// @ts-ignore
311328
[SPACE_IDS]: ['default'],
312329
[VERSION]: '8.9.0',
313330
[TAGS]: ['rule-', '-tags'],
314331
...(flattened
315332
? {
333+
...alertRule,
316334
[EVENT_KIND]: 'signal',
317335
[ALERT_INSTANCE_ID]: 'alert-A',
318336
[ALERT_UUID]: 'abcdefg',
@@ -324,6 +342,7 @@ for (const flattened of [true, false]) {
324342
kibana: {
325343
alert: {
326344
instance: { id: 'alert-A' },
345+
rule: omit(rule, 'execution'),
327346
uuid: 'abcdefg',
328347
},
329348
},
@@ -394,12 +413,13 @@ for (const flattened of [true, false]) {
394413
kibanaVersion: '8.9.0',
395414
})
396415
).toEqual({
397-
...alertRule,
398416
count: 2,
399417
url: `https://url2`,
400418
'kibana.alert.nested_field': 2,
401419
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
402420
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
421+
// @ts-ignore
422+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
403423
[EVENT_ACTION]: 'close',
404424
[ALERT_ACTION_GROUP]: 'NoLongerActive',
405425
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -414,11 +434,13 @@ for (const flattened of [true, false]) {
414434
[ALERT_START]: '2023-03-28T12:27:28.159Z',
415435
[ALERT_END]: '2023-03-30T12:27:28.159Z',
416436
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
437+
// @ts-ignore
417438
[SPACE_IDS]: ['default'],
418439
[VERSION]: '8.9.0',
419440
[TAGS]: ['-tags', 'reported-recovery-tag', 'active-alert-tag', 'rule-'],
420441
...(flattened
421442
? {
443+
...alertRule,
422444
[EVENT_KIND]: 'signal',
423445
[ALERT_INSTANCE_ID]: 'alert-A',
424446
[ALERT_UUID]: 'abcdefg',
@@ -430,6 +452,7 @@ for (const flattened of [true, false]) {
430452
kibana: {
431453
alert: {
432454
instance: { id: 'alert-A' },
455+
rule: omit(rule, 'execution'),
433456
uuid: 'abcdefg',
434457
},
435458
},
@@ -498,12 +521,13 @@ for (const flattened of [true, false]) {
498521
kibanaVersion: '8.9.0',
499522
})
500523
).toEqual({
501-
...alertRule,
502524
count: 2,
503525
url: `https://url2`,
504526
'kibana.alert.nested_field': 2,
505527
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
506528
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
529+
// @ts-ignore
530+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
507531
[EVENT_ACTION]: 'close',
508532
[ALERT_ACTION_GROUP]: 'NoLongerActive',
509533
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -518,11 +542,13 @@ for (const flattened of [true, false]) {
518542
[ALERT_START]: '2023-03-28T12:27:28.159Z',
519543
[ALERT_END]: '2023-03-30T12:27:28.159Z',
520544
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
545+
// @ts-ignore
521546
[SPACE_IDS]: ['default'],
522547
[VERSION]: '8.9.0',
523548
[TAGS]: ['rule-', '-tags'],
524549
...(flattened
525550
? {
551+
...alertRule,
526552
[EVENT_KIND]: 'signal',
527553
[ALERT_INSTANCE_ID]: 'alert-A',
528554
[ALERT_UUID]: 'abcdefg',
@@ -534,6 +560,7 @@ for (const flattened of [true, false]) {
534560
kibana: {
535561
alert: {
536562
instance: { id: 'alert-A' },
563+
rule: omit(rule, 'execution'),
537564
uuid: 'abcdefg',
538565
},
539566
},
@@ -601,12 +628,13 @@ for (const flattened of [true, false]) {
601628
},
602629
})
603630
).toEqual({
604-
...alertRule,
605631
count: 2,
606632
url: `https://url2`,
607633
'kibana.alert.deeply.nested_field': 2,
608634
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
609635
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
636+
// @ts-ignore
637+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
610638
[EVENT_ACTION]: 'close',
611639
[ALERT_ACTION_GROUP]: 'NoLongerActive',
612640
[ALERT_CONSECUTIVE_MATCHES]: 0,
@@ -621,11 +649,13 @@ for (const flattened of [true, false]) {
621649
[ALERT_START]: '2023-03-28T12:27:28.159Z',
622650
[ALERT_END]: '2023-03-30T12:27:28.159Z',
623651
[ALERT_TIME_RANGE]: { gte: '2023-03-28T12:27:28.159Z', lte: '2023-03-30T12:27:28.159Z' },
652+
// @ts-ignore
624653
[SPACE_IDS]: ['default'],
625654
[VERSION]: '8.9.0',
626655
[TAGS]: ['rule-', '-tags'],
627656
...(flattened
628657
? {
658+
...alertRule,
629659
[EVENT_KIND]: 'signal',
630660
[ALERT_INSTANCE_ID]: 'alert-A',
631661
[ALERT_UUID]: 'abcdefg',
@@ -637,6 +667,7 @@ for (const flattened of [true, false]) {
637667
kibana: {
638668
alert: {
639669
instance: { id: 'alert-A' },
670+
rule: omit(rule, 'execution'),
640671
uuid: 'abcdefg',
641672
},
642673
},

x-pack/plugins/alerting/server/alerts_client/lib/build_recovered_alert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ALERT_RULE_EXECUTION_TIMESTAMP,
2727
ALERT_PREVIOUS_ACTION_GROUP,
2828
ALERT_SEVERITY_IMPROVING,
29+
ALERT_RULE_EXECUTION_UUID,
2930
} from '@kbn/rule-data-utils';
3031
import { DeepPartial } from '@kbn/utility-types';
3132
import { get } from 'lodash';
@@ -86,12 +87,11 @@ export const buildRecoveredAlert = <
8687
const refreshableAlertFields = replaceRefreshableAlertFields(alert);
8788

8889
const alertUpdates = {
89-
// Set latest rule configuration
90-
...rule,
9190
// Update the timestamp to reflect latest update time
9291
[TIMESTAMP]: timestamp,
9392
[EVENT_ACTION]: 'close',
9493
[ALERT_RULE_EXECUTION_TIMESTAMP]: runTimestamp ?? timestamp,
94+
[ALERT_RULE_EXECUTION_UUID]: rule[ALERT_RULE_EXECUTION_UUID],
9595
// Set the recovery action group
9696
[ALERT_ACTION_GROUP]: recoveryActionGroup,
9797
// Set latest flapping state

x-pack/plugins/alerting/server/alerts_client/lib/build_updated_recovered_alert.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ALERT_RULE_EXECUTION_TIMESTAMP,
2929
ALERT_CONSECUTIVE_MATCHES,
3030
ALERT_PREVIOUS_ACTION_GROUP,
31+
ALERT_RULE_EXECUTION_UUID,
3132
} from '@kbn/rule-data-utils';
3233
import {
3334
alertRule,
@@ -161,7 +162,6 @@ describe('buildUpdatedRecoveredAlert', () => {
161162
timestamp: '2023-03-29T12:27:28.159Z',
162163
})
163164
).toEqual({
164-
...alertRule,
165165
event: {
166166
action: 'close',
167167
kind: 'signal',
@@ -177,6 +177,19 @@ describe('buildUpdatedRecoveredAlert', () => {
177177
id: 'alert-A',
178178
},
179179
maintenance_window_ids: ['maint-x'],
180+
rule: {
181+
category: 'My test rule',
182+
consumer: 'bar',
183+
name: 'rule-name',
184+
parameters: {
185+
bar: true,
186+
},
187+
producer: 'alerts',
188+
revision: 0,
189+
rule_type_id: 'test.rule-type',
190+
tags: ['rule-', '-tags'],
191+
uuid: '1',
192+
},
180193
start: '2023-03-28T12:27:28.159Z',
181194
time_range: {
182195
gte: '2023-03-27T12:27:28.159Z',
@@ -185,16 +198,17 @@ describe('buildUpdatedRecoveredAlert', () => {
185198
uuid: 'abcdefg',
186199
consecutive_matches: 0,
187200
},
201+
space_ids: ['default'],
188202
version: '8.8.1',
189203
},
190204
[TIMESTAMP]: '2023-03-29T12:27:28.159Z',
191205
[ALERT_RULE_EXECUTION_TIMESTAMP]: '2023-03-29T12:27:28.159Z',
206+
[ALERT_RULE_EXECUTION_UUID]: '5f6aa57d-3e22-484e-bae8-cbed868f4d28',
192207
[ALERT_FLAPPING]: true,
193208
[ALERT_FLAPPING_HISTORY]: [false, false, true, true],
194209
[ALERT_PREVIOUS_ACTION_GROUP]: 'recovered',
195210
[ALERT_STATUS]: 'recovered',
196211
[ALERT_WORKFLOW_STATUS]: 'open',
197-
[SPACE_IDS]: ['default'],
198212
[TAGS]: ['rule-', '-tags'],
199213
});
200214
});

0 commit comments

Comments
 (0)