Skip to content

Commit

Permalink
[Synthetics] add default email recovery message (#154862)
Browse files Browse the repository at this point in the history
## Summary

Resolves #153891
<img width="1334" alt="Screen Shot 2023-04-18 at 7 50 20 PM"
src="https://user-images.githubusercontent.com/11356435/232928889-09de824f-14ef-426d-8732-6da7fc12eb37.png">

Down email

Recovered email
<img width="1339" alt="Screen Shot 2023-04-18 at 7 50 30 PM"
src="https://user-images.githubusercontent.com/11356435/232928874-4e7c0cbb-b7ec-4684-b0d2-88ec993697df.png">

Please note the `recoverd` typo in the screenshot has been addressed in
commits
[7c4040f](7c4040f)
and
[56f637b](56f637b)

### Testing
This PR is extremely tricky to test. I have deployed this branch to my
own cloud account. This ensures that I can control the allowlist setting
for emails on cloud. If you want, I can give you the cloud cluster where
this branch is hosted and if you give me your email I can add you to my
allowlist settings for you to test.

The cloud cluster is current on commit
[f889500](f889500)
  • Loading branch information
dominiqueclarke authored Apr 20, 2023
1 parent c68dfd7 commit 0b71c74
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 156 deletions.
140 changes: 139 additions & 1 deletion x-pack/plugins/synthetics/common/rules/alert_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { populateAlertActions } from './alert_actions';
import { ActionConnector } from './types';
import { MONITOR_STATUS } from '../constants/uptime_alerts';
import { MONITOR_STATUS as SYNTHETICS_MONITOR_STATUS } from '../constants/synthetics_alerts';
import { MonitorStatusTranslations } from '../translations';
import { MonitorStatusTranslations } from './legacy_uptime/translations';
import { SyntheticsMonitorStatusTranslations } from './synthetics/translations';

describe('Legacy Alert Actions factory', () => {
Expand All @@ -33,6 +33,7 @@ describe('Legacy Alert Actions factory', () => {
defaultActionMessage: MonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: MonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: MonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage: MonitorStatusTranslations.defaultRecoverySubjectMessage,
},
isLegacy: true,
});
Expand Down Expand Up @@ -60,6 +61,70 @@ describe('Legacy Alert Actions factory', () => {
]);
});

it('generate expected action for email', async () => {
const resp = populateAlertActions({
groupId: MONITOR_STATUS.id,
defaultActions: [
{
actionTypeId: '.email',
group: 'xpack.uptime.alerts.actionGroups.monitorStatus',
params: {
dedupKey: 'always-downxpack.uptime.alerts.actionGroups.monitorStatus',
eventAction: 'trigger',
severity: 'error',
summary: MonitorStatusTranslations.defaultActionMessage,
},
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
},
] as unknown as ActionConnector[],
translations: {
defaultActionMessage: MonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: MonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: MonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage: MonitorStatusTranslations.defaultRecoverySubjectMessage,
},
isLegacy: true,
defaultEmail: {
to: ['[email protected]'],
},
});
expect(resp).toEqual([
{
group: 'recovered',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
bcc: [],
cc: [],
kibanaFooterLink: {
path: '',
text: '',
},
message:
'Alert for monitor {{context.monitorName}} with url {{{context.monitorUrl}}} from {{context.observerLocation}} has recovered',
subject:
'Monitor {{context.monitorName}} with url {{{context.monitorUrl}}} has recovered',
to: ['[email protected]'],
},
},
{
group: 'xpack.uptime.alerts.actionGroups.monitorStatus',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
bcc: [],
cc: [],
kibanaFooterLink: {
path: '',
text: '',
},
message:
'Monitor {{context.monitorName}} with url {{{context.monitorUrl}}} from {{context.observerLocation}} {{{context.statusMessage}}} The latest error message is {{{context.latestErrorMessage}}}, checked at {{context.checkedAt}}',
subject: 'Monitor {{context.monitorName}} with url {{{context.monitorUrl}}} is down',
to: ['[email protected]'],
},
},
]);
});

it('generate expected action for index', async () => {
const resp = populateAlertActions({
groupId: MONITOR_STATUS.id,
Expand All @@ -80,6 +145,7 @@ describe('Legacy Alert Actions factory', () => {
defaultActionMessage: MonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: MonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: MonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage: MonitorStatusTranslations.defaultRecoverySubjectMessage,
},
isLegacy: true,
});
Expand Down Expand Up @@ -141,6 +207,7 @@ describe('Legacy Alert Actions factory', () => {
defaultActionMessage: MonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: MonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: MonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage: MonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
expect(resp).toEqual([
Expand Down Expand Up @@ -189,6 +256,8 @@ describe('Alert Actions factory', () => {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
expect(resp).toEqual([
Expand Down Expand Up @@ -235,6 +304,8 @@ describe('Alert Actions factory', () => {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
expect(resp).toEqual([
Expand Down Expand Up @@ -295,6 +366,8 @@ describe('Alert Actions factory', () => {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
expect(resp).toEqual([
Expand All @@ -320,4 +393,69 @@ describe('Alert Actions factory', () => {
},
]);
});

it('generate expected action for email action connector', async () => {
const resp = populateAlertActions({
groupId: SYNTHETICS_MONITOR_STATUS.id,
defaultActions: [
{
actionTypeId: '.email',
group: 'xpack.synthetics.alerts.actionGroups.monitorStatus',
params: {
dedupKey: 'always-downxpack.uptime.alerts.actionGroups.monitorStatus',
eventAction: 'trigger',
severity: 'error',
summary:
'Monitor {{context.monitorName}} with url {{{context.monitorUrl}}} from {{context.observerLocation}} {{{context.statusMessage}}} The latest error message is {{{context.latestErrorMessage}}}',
},
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
},
] as unknown as ActionConnector[],
defaultEmail: {
to: ['[email protected]'],
},
translations: {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
expect(resp).toEqual([
{
group: 'recovered',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
bcc: [],
cc: [],
kibanaFooterLink: {
path: '',
text: '',
},
message:
'The alert for the monitor {{context.monitorName}} checking {{{context.monitorUrl}}} from {{context.locationName}} is no longer active: {{context.recoveryReason}}.',
subject:
'The monitor {{context.monitorName}} checking {{{context.monitorUrl}}} has recovered.',
to: ['[email protected]'],
},
},
{
group: 'xpack.synthetics.alerts.actionGroups.monitorStatus',
id: 'f2a3b195-ed76-499a-805d-82d24d4eeba9',
params: {
bcc: [],
cc: [],
kibanaFooterLink: {
path: '',
text: '',
},
message:
'The monitor {{context.monitorName}} checking {{{context.monitorUrl}}} from {{context.locationName}} last ran at {{context.checkedAt}} and is {{{context.status}}}. The last error received is: {{{context.lastErrorMessage}}}.',
subject: 'The monitor {{context.monitorName}} checking {{{context.monitorUrl}}} is down.',
to: ['[email protected]'],
},
},
]);
});
});
17 changes: 13 additions & 4 deletions x-pack/plugins/synthetics/common/rules/alert_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface Translations {
defaultActionMessage: string;
defaultRecoveryMessage: string;
defaultSubjectMessage: string;
defaultRecoverySubjectMessage: string;
}

export function populateAlertActions({
Expand Down Expand Up @@ -107,6 +108,8 @@ export function populateAlertActions({
case EMAIL_ACTION_ID:
if (defaultEmail) {
action.params = getEmailActionParams(translations, defaultEmail);
recoveredAction.params = getEmailActionParams(translations, defaultEmail, true);
actions.push(recoveredAction);
}
break;
default:
Expand Down Expand Up @@ -270,13 +273,19 @@ function getJiraActionParams({ defaultActionMessage }: Translations): JiraAction
}

function getEmailActionParams(
{ defaultActionMessage, defaultSubjectMessage }: Translations,
defaultEmail: DefaultEmail
{
defaultActionMessage,
defaultSubjectMessage,
defaultRecoverySubjectMessage,
defaultRecoveryMessage,
}: Translations,
defaultEmail: DefaultEmail,
isRecovery?: boolean
): EmailActionParams {
return {
to: defaultEmail.to,
subject: defaultSubjectMessage,
message: defaultActionMessage,
subject: isRecovery ? defaultRecoverySubjectMessage : defaultSubjectMessage,
message: isRecovery ? defaultRecoveryMessage : defaultActionMessage,
cc: defaultEmail.cc ?? [],
bcc: defaultEmail.bcc ?? [],
kibanaFooterLink: {
Expand Down
Loading

0 comments on commit 0b71c74

Please sign in to comment.