Skip to content

Commit

Permalink
copy alert state to alert context
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiqueclarke committed Mar 29, 2022
1 parent a50e87c commit a8269d2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
24 changes: 22 additions & 2 deletions x-pack/plugins/uptime/server/lib/alerts/duration_anomaly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,17 @@ Response times as high as ${slowestResponse} ms have been detected from location
Array [
"xpack.uptime.alerts.actionGroups.durationAnomaly",
Object {
"${ALERT_REASON_MSG}": "${reasonMessages[0]}",
"anomalyStartTimestamp": "date",
"bucketSpan": 900,
"expectedResponseTime": "10 ms",
"monitor": "uptime-monitor",
"monitorUrl": "https://elastic.co",
"observerLocation": "harrisburg",
"${ALERT_REASON_MSG}": "Abnormal (minor level) response time detected on uptime-monitor with url https://elastic.co at date. Anomaly severity score is 25.
Response times as high as 200 ms have been detected from location harrisburg. Expected response time is 10 ms.",
"severity": "minor",
"severityScore": 25,
"slowestAnomalyResponse": "200 ms",
"${VIEW_IN_APP_URL}": "http://localhost:5601/hfe/app/uptime/monitor/eHBhY2sudXB0aW1lLmFsZXJ0cy5hY3Rpb25Hcm91cHMuZHVyYXRpb25Bbm9tYWx5MA==?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z",
},
]
Expand All @@ -227,7 +237,17 @@ Response times as high as ${slowestResponse} ms have been detected from location
Array [
"xpack.uptime.alerts.actionGroups.durationAnomaly",
Object {
"${ALERT_REASON_MSG}": "${reasonMessages[1]}",
"anomalyStartTimestamp": "date",
"bucketSpan": 900,
"expectedResponseTime": "20 ms",
"monitor": "uptime-monitor",
"monitorUrl": "https://elastic.co",
"observerLocation": "fairbanks",
"${ALERT_REASON_MSG}": "Abnormal (warning level) response time detected on uptime-monitor with url https://elastic.co at date. Anomaly severity score is 10.
Response times as high as 300 ms have been detected from location fairbanks. Expected response time is 20 ms.",
"severity": "warning",
"severityScore": 10,
"slowestAnomalyResponse": "300 ms",
"${VIEW_IN_APP_URL}": "http://localhost:5601/hfe/app/uptime/monitor/eHBhY2sudXB0aW1lLmFsZXJ0cy5hY3Rpb25Hcm91cHMuZHVyYXRpb25Bbm9tYWx5MQ==?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z",
},
]
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/uptime/server/lib/alerts/duration_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export const durationAnomalyAlertFactory: UptimeAlertTypeFactory<ActionGroupIds>
},
],
actionVariables: {
context: [ACTION_VARIABLES[ALERT_REASON_MSG], ACTION_VARIABLES[VIEW_IN_APP_URL]],
context: [
ACTION_VARIABLES[ALERT_REASON_MSG],
ACTION_VARIABLES[VIEW_IN_APP_URL],
...durationAnomalyTranslations.actionVariables,
...commonStateTranslations,
],
state: [...durationAnomalyTranslations.actionVariables, ...commonStateTranslations],
},
isExportable: true,
Expand Down Expand Up @@ -160,6 +165,7 @@ export const durationAnomalyAlertFactory: UptimeAlertTypeFactory<ActionGroupIds>
alertInstance.scheduleActions(DURATION_ANOMALY.id, {
[ALERT_REASON_MSG]: alertReasonMessage,
[VIEW_IN_APP_URL]: getViewInAppUrl(relativeViewInAppUrl, basePath),
...summary,
});
});
}
Expand Down
42 changes: 42 additions & 0 deletions x-pack/plugins/uptime/server/lib/alerts/status_check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ describe('status check alert', () => {
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": "error message 1",
"monitorId": "first",
"monitorName": "First",
"monitorType": "myType",
"monitorUrl": "localhost:8080",
"observerHostname": undefined,
"observerLocation": "harrisburg",
"reason": "First from harrisburg failed 234 times in the last 15 mins. Alert when > 5.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/Zmlyc3Q=?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22harrisburg%22%5D%5D%5D",
},
Expand Down Expand Up @@ -313,6 +320,13 @@ describe('status check alert', () => {
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": "error message 1",
"monitorId": "first",
"monitorName": "First",
"monitorType": "myType",
"monitorUrl": "localhost:8080",
"observerHostname": undefined,
"observerLocation": "harrisburg",
"reason": "First from harrisburg failed 234 times in the last 15m. Alert when > 5.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/Zmlyc3Q=?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22harrisburg%22%5D%5D%5D",
},
Expand Down Expand Up @@ -785,27 +799,55 @@ describe('status check alert', () => {
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": undefined,
"monitorId": "foo",
"monitorName": "Foo",
"monitorType": "myType",
"monitorUrl": "https://foo.com",
"observerHostname": undefined,
"observerLocation": "harrisburg",
"reason": "Foo from harrisburg 35 days availability is 99.28%. Alert when < 99.34%.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/Zm9v?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22harrisburg%22%5D%5D%5D",
},
],
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": undefined,
"monitorId": "foo",
"monitorName": "Foo",
"monitorType": "myType",
"monitorUrl": "https://foo.com",
"observerHostname": undefined,
"observerLocation": "fairbanks",
"reason": "Foo from fairbanks 35 days availability is 98.03%. Alert when < 99.34%.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/Zm9v?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22fairbanks%22%5D%5D%5D",
},
],
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": undefined,
"monitorId": "unreliable",
"monitorName": "Unreliable",
"monitorType": "myType",
"monitorUrl": "https://unreliable.co",
"observerHostname": undefined,
"observerLocation": "fairbanks",
"reason": "Unreliable from fairbanks 35 days availability is 90.92%. Alert when < 99.34%.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/dW5yZWxpYWJsZQ==?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22fairbanks%22%5D%5D%5D",
},
],
Array [
"xpack.uptime.alerts.actionGroups.monitorStatus",
Object {
"latestErrorMessage": undefined,
"monitorId": "no-name",
"monitorName": "no-name",
"monitorType": "myType",
"monitorUrl": "https://no-name.co",
"observerHostname": undefined,
"observerLocation": "fairbanks",
"reason": "no-name from fairbanks 35 days availability is 90.92%. Alert when < 99.34%.",
"viewInAppUrl": "http://localhost:5601/hfe/app/uptime/monitor/bm8tbmFtZQ==?dateRangeEnd=now&dateRangeStart=2022-03-17T13%3A13%3A33.755Z&filters=%5B%5B%22observer.geo.name%22%2C%5B%22fairbanks%22%5D%5D%5D",
},
Expand Down
16 changes: 12 additions & 4 deletions x-pack/plugins/uptime/server/lib/alerts/status_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const getMonitorSummary = (monitorInfo: Ping, statusMessage: string) => {

return {
...summary,
reason: `${monitorName} from ${observerLocation} ${statusMessage}`,
[ALERT_REASON_MSG]: `${monitorName} from ${observerLocation} ${statusMessage}`,
};
};

Expand Down Expand Up @@ -281,6 +281,8 @@ export const statusCheckAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (
ACTION_VARIABLES[MONITOR_WITH_GEO],
ACTION_VARIABLES[ALERT_REASON_MSG],
ACTION_VARIABLES[VIEW_IN_APP_URL],
...commonMonitorStateI18,
...commonStateTranslations,
],
state: [...commonMonitorStateI18, ...commonStateTranslations],
},
Expand All @@ -289,7 +291,13 @@ export const statusCheckAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (
async executor({
params: rawParams,
state,
services: { savedObjectsClient, scopedClusterClient, alertWithLifecycle, getAlertStartedDate },
services: {
savedObjectsClient,
scopedClusterClient,
alertWithLifecycle,
getAlertStartedDate,
alertFactory,
},
rule: {
schedule: { interval },
},
Expand Down Expand Up @@ -381,8 +389,8 @@ export const statusCheckAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (
});

alert.scheduleActions(MONITOR_STATUS.id, {
[ALERT_REASON_MSG]: monitorSummary.reason,
[VIEW_IN_APP_URL]: getViewInAppUrl(relativeViewInAppUrl, basePath),
...monitorSummary,
});
}
return updateState(state, downMonitorsByLocation.length > 0);
Expand Down Expand Up @@ -451,8 +459,8 @@ export const statusCheckAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (
});

alert.scheduleActions(MONITOR_STATUS.id, {
[ALERT_REASON_MSG]: monitorSummary.reason,
[VIEW_IN_APP_URL]: getViewInAppUrl(relativeViewInAppUrl, basePath),
...monitorSummary,
});
});
return updateState(state, downMonitorsByLocation.length > 0);
Expand Down
16 changes: 9 additions & 7 deletions x-pack/plugins/uptime/server/lib/alerts/tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,18 @@ describe('tls alert', () => {
const [{ value: alertInstanceMock }] = alertWithLifecycle.mock.results;
expect(alertInstanceMock.replaceState).toHaveBeenCalledTimes(4);
mockCertResult.certs.forEach((cert) => {
expect(alertInstanceMock.replaceState).toBeCalledWith(
expect.objectContaining({
commonName: cert.common_name,
issuer: cert.issuer,
status: 'expired',
})
const context = {
commonName: cert.common_name,
issuer: cert.issuer,
status: 'expired',
};
expect(alertInstanceMock.replaceState).toBeCalledWith(expect.objectContaining(context));
expect(alertInstanceMock.scheduleActions).toBeCalledWith(
TLS.id,
expect.objectContaining(context)
);
});
expect(alertInstanceMock.scheduleActions).toHaveBeenCalledTimes(4);
expect(alertInstanceMock.scheduleActions).toBeCalledWith(TLS.id);
});

it('handles dynamic settings for aging or expiration threshold', async () => {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/uptime/server/lib/alerts/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const tlsAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (_server,
},
],
actionVariables: {
context: [],
context: [...tlsTranslations.actionVariables, ...commonStateTranslations],
state: [...tlsTranslations.actionVariables, ...commonStateTranslations],
},
isExportable: true,
Expand Down Expand Up @@ -174,7 +174,7 @@ export const tlsAlertFactory: UptimeAlertTypeFactory<ActionGroupIds> = (_server,
...updateState(state, foundCerts),
...summary,
});
alertInstance.scheduleActions(TLS.id);
alertInstance.scheduleActions(TLS.id, { ...summary });
});
}

Expand Down

0 comments on commit a8269d2

Please sign in to comment.