Skip to content

Commit b562c8e

Browse files
[Synthetics] fix recovery message (#188461)
## Summary Fixes #188387 We previously relied on state to populate values in alert recovery messages. However, values were removed from state when onboarding the rule with FAAD. Fortunately, onboarding with FAAD means we now have access to the alert document within recovery. This PR uses values from that document, instead of state, to populate the alert recovery message. ### Testing 1. Create an oblt cluster with `/create-ccs-cluster` on slack. Choose `dev-oblt`. 2. Add the configuration values from the oblt command to your kibana.yml 3. Navigate to `app/synthetics/settings/alerting` and add a default connector. The easiest connector to add would be a Server log 4. Create an intentionally down HTTP monitor 5. Wait to receive an alert to your alert connector 6. Update the monitor so that the monitor comes up 7. Wait to receive an alert recovery message to your alert connector. The recovery message data should be completely populated
1 parent ac5beb6 commit b562c8e

2 files changed

Lines changed: 48 additions & 39 deletions

File tree

x-pack/plugins/observability_solution/synthetics/server/alert_rules/common.test.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('updateState', () => {
186186

187187
describe('setRecoveredAlertsContext', () => {
188188
const alertUuid = 'alert-id';
189-
const location = 'US Central';
189+
const location = 'us_west';
190190
const configId = '12345';
191191
const idWithLocation = `${configId}-${location}`;
192192
const basePath = {
@@ -222,13 +222,15 @@ describe('setRecoveredAlertsContext', () => {
222222
getRecoveredAlerts: jest.fn().mockReturnValue([
223223
{
224224
alert: {
225-
getId: () => alertUuid,
226-
getState: () => ({
227-
idWithLocation,
228-
monitorName: 'test-monitor',
229-
}),
230-
setContext: jest.fn(),
231225
getUuid: () => alertUuid,
226+
getId: () => idWithLocation,
227+
getState: () => ({}),
228+
setContext: jest.fn(),
229+
},
230+
hit: {
231+
'kibana.alert.instance.id': idWithLocation,
232+
'location.id': location,
233+
configId,
232234
},
233235
},
234236
]),
@@ -264,11 +266,10 @@ describe('setRecoveredAlertsContext', () => {
264266
tz: 'UTC',
265267
});
266268
expect(alertsClientMock.setAlertData).toBeCalledWith({
267-
id: 'alert-id',
269+
id: idWithLocation,
268270
context: {
269271
checkedAt: 'Feb 26, 2023 @ 00:00:00.000',
270272
configId: '12345',
271-
idWithLocation,
272273
linkMessage: '',
273274
alertDetailsUrl: 'https://localhost:5601/app/observability/alerts/alert-id',
274275
monitorName: 'test-monitor',
@@ -280,6 +281,8 @@ describe('setRecoveredAlertsContext', () => {
280281
'Monitor "test-monitor" from Unnamed-location is recovered. Checked at February 25, 2023 7:00 PM.',
281282
stateId: '123456',
282283
status: 'recovered',
284+
locationId: location,
285+
idWithLocation,
283286
},
284287
});
285288
});
@@ -292,13 +295,15 @@ describe('setRecoveredAlertsContext', () => {
292295
getRecoveredAlerts: jest.fn().mockReturnValue([
293296
{
294297
alert: {
295-
getId: () => alertUuid,
296-
getState: () => ({
297-
idWithLocation,
298-
monitorName: 'test-monitor',
299-
}),
300-
setContext: jest.fn(),
301298
getUuid: () => alertUuid,
299+
getId: () => idWithLocation,
300+
getState: () => ({}),
301+
setContext: jest.fn(),
302+
},
303+
hit: {
304+
'kibana.alert.instance.id': idWithLocation,
305+
'location.id': location,
306+
configId,
302307
},
303308
},
304309
]),
@@ -334,14 +339,13 @@ describe('setRecoveredAlertsContext', () => {
334339
tz: 'UTC',
335340
});
336341
expect(alertsClientMock.setAlertData).toBeCalledWith({
337-
id: 'alert-id',
342+
id: idWithLocation,
338343
context: {
339344
configId: '12345',
340345
checkedAt: 'Feb 26, 2023 @ 00:00:00.000',
341346
monitorUrl: '(unavailable)',
342347
reason:
343348
'Monitor "test-monitor" from Unnamed-location is recovered. Checked at February 25, 2023 7:00 PM.',
344-
idWithLocation,
345349
linkMessage: '',
346350
alertDetailsUrl: 'https://localhost:5601/app/observability/alerts/alert-id',
347351
monitorName: 'test-monitor',
@@ -350,6 +354,8 @@ describe('setRecoveredAlertsContext', () => {
350354
stateId: '123456',
351355
status: 'recovered',
352356
monitorUrlLabel: 'URL',
357+
idWithLocation,
358+
locationId: location,
353359
},
354360
});
355361
});
@@ -362,15 +368,15 @@ describe('setRecoveredAlertsContext', () => {
362368
getRecoveredAlerts: jest.fn().mockReturnValue([
363369
{
364370
alert: {
365-
getId: () => alertUuid,
366-
getState: () => ({
367-
idWithLocation,
368-
monitorName: 'test-monitor',
369-
locationId: 'us_west',
370-
configId: '12345-67891',
371-
}),
372-
setContext: jest.fn(),
371+
getId: () => idWithLocation,
373372
getUuid: () => alertUuid,
373+
getState: () => ({}),
374+
setContext: jest.fn(),
375+
},
376+
hit: {
377+
'kibana.alert.instance.id': idWithLocation,
378+
'location.id': location,
379+
configId,
374380
},
375381
},
376382
]),
@@ -382,7 +388,7 @@ describe('setRecoveredAlertsContext', () => {
382388
configId,
383389
monitorQueryId: 'stale-config',
384390
status: 'down',
385-
locationId: 'location',
391+
locationId: location,
386392
ping: {
387393
state: {
388394
id: '123456',
@@ -406,20 +412,20 @@ describe('setRecoveredAlertsContext', () => {
406412
tz: 'UTC',
407413
});
408414
expect(alertsClientMock.setAlertData).toBeCalledWith({
409-
id: 'alert-id',
415+
id: idWithLocation,
410416
context: {
411-
configId: '12345-67891',
417+
configId,
412418
idWithLocation,
413419
alertDetailsUrl: 'https://localhost:5601/app/observability/alerts/alert-id',
414420
monitorName: 'test-monitor',
415421
status: 'up',
416422
recoveryReason:
417423
'the monitor is now up again. It ran successfully at Feb 26, 2023 @ 00:00:00.000',
418424
recoveryStatus: 'is now up',
419-
locationId: 'us_west',
425+
locationId: location,
420426
checkedAt: 'Feb 26, 2023 @ 00:00:00.000',
421427
linkMessage:
422-
'- Link: https://localhost:5601/app/synthetics/monitor/12345-67891/errors/123456?locationId=us_west',
428+
'- Link: https://localhost:5601/app/synthetics/monitor/12345/errors/123456?locationId=us_west',
423429
monitorUrl: '(unavailable)',
424430
monitorUrlLabel: 'URL',
425431
reason:

x-pack/plugins/observability_solution/synthetics/server/alert_rules/common.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ export const setRecoveredAlertsContext = ({
186186
const alertUuid = recoveredAlert.alert.getUuid();
187187

188188
const state = recoveredAlert.alert.getState();
189+
const alertHit = recoveredAlert.hit;
190+
const locationId = alertHit?.['location.id'];
191+
const configId = alertHit?.configId;
189192

190193
let recoveryReason = '';
191194
let recoveryStatus = i18n.translate(
@@ -199,15 +202,14 @@ export const setRecoveredAlertsContext = ({
199202
let monitorSummary: MonitorSummaryStatusRule | null = null;
200203
let lastErrorMessage;
201204

202-
if (state?.idWithLocation && staleDownConfigs[state.idWithLocation]) {
203-
const { idWithLocation, locationId } = state;
204-
const downConfig = staleDownConfigs[idWithLocation];
205-
const { ping, configId } = downConfig;
205+
if (recoveredAlertId && locationId && staleDownConfigs[recoveredAlertId]) {
206+
const downConfig = staleDownConfigs[recoveredAlertId];
207+
const { ping } = downConfig;
206208
monitorSummary = getMonitorSummary(
207209
ping,
208210
RECOVERED_LABEL,
209211
locationId,
210-
configId,
212+
downConfig.configId,
211213
dateFormat,
212214
tz
213215
);
@@ -242,12 +244,11 @@ export const setRecoveredAlertsContext = ({
242244
}
243245
}
244246

245-
if (state?.idWithLocation && upConfigs[state.idWithLocation]) {
246-
const { idWithLocation, configId, locationId } = state;
247+
if (configId && recoveredAlertId && locationId && upConfigs[recoveredAlertId]) {
247248
// pull the last error from state, since it is not available on the up ping
248-
lastErrorMessage = state.lastErrorMessage;
249+
lastErrorMessage = alertHit?.['error.message'];
249250

250-
const upConfig = upConfigs[idWithLocation];
251+
const upConfig = upConfigs[recoveredAlertId];
251252
isUp = Boolean(upConfig) || false;
252253
const ping = upConfig.ping;
253254

@@ -290,6 +291,8 @@ export const setRecoveredAlertsContext = ({
290291
const context = {
291292
...state,
292293
...(monitorSummary ? monitorSummary : {}),
294+
locationId,
295+
idWithLocation: recoveredAlertId,
293296
lastErrorMessage,
294297
recoveryStatus,
295298
linkMessage,

0 commit comments

Comments
 (0)