Skip to content

Commit

Permalink
[Synthetics] Fixed action connectors and added api test for default a…
Browse files Browse the repository at this point in the history
…lerting (elastic#161218)
  • Loading branch information
shahzad31 authored Jul 5, 2023
1 parent 40d2f68 commit 93fc2a8
Show file tree
Hide file tree
Showing 6 changed files with 591 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ x-pack/plugins/infra/server/lib/alerting @elastic/actionable-observability
/x-pack/test/functional/services/uptime @elastic/uptime
/x-pack/test/api_integration/apis/uptime @elastic/uptime
/x-pack/test/api_integration/apis/synthetics @elastic/uptime
/x-pack/test/alerting_api_integration/observability/synthetics_rule.ts @elastic/uptime
/x-pack/test/alerting_api_integration/observability/index.ts @elastic/uptime


# Client Side Monitoring / Uptime (lives in APM directories but owned by Uptime)
/x-pack/plugins/apm/public/application/uxApp.tsx @elastic/uptime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { FindActionResult } from '@kbn/actions-plugin/server';
import { savedObjectsAdapter } from '../../saved_objects';
import { populateAlertActions } from '../../../common/rules/alert_actions';
import { SyntheticsMonitorStatusTranslations } from '../../../common/rules/synthetics/translations';
import {
SyntheticsMonitorStatusTranslations,
TlsTranslations,
} from '../../../common/rules/synthetics/translations';
import { SyntheticsServerSetup, UptimeRequestHandlerContext } from '../../types';
import {
ACTION_GROUP_DEFINITIONS,
Expand Down Expand Up @@ -64,7 +67,7 @@ export class DefaultAlertService {
return this.createDefaultAlertIfNotExist(
SYNTHETICS_TLS_RULE,
`Synthetics internal TLS alert`,
'10m'
'1m'
);
}

Expand Down Expand Up @@ -92,7 +95,7 @@ export class DefaultAlertService {
return alert;
}

const actions = await this.getAlertActions();
const actions = await this.getAlertActions(ruleType);

const rulesClient = (await this.context.alerting)?.getRulesClient();
const newAlert = await rulesClient.create<{}>({
Expand All @@ -119,15 +122,15 @@ export class DefaultAlertService {
);
}
updateTlsRule() {
return this.updateDefaultAlert(SYNTHETICS_TLS_RULE, `Synthetics internal TLS alert`, '10m');
return this.updateDefaultAlert(SYNTHETICS_TLS_RULE, `Synthetics internal TLS alert`, '1m');
}

async updateDefaultAlert(ruleType: DefaultRuleType, name: string, interval: string) {
const rulesClient = (await this.context.alerting)?.getRulesClient();

const alert = await this.getExistingAlert(ruleType);
if (alert) {
const actions = await this.getAlertActions();
const actions = await this.getAlertActions(ruleType);
const updatedAlert = await rulesClient.update({
id: alert.id,
data: {
Expand All @@ -136,7 +139,6 @@ export class DefaultAlertService {
tags: alert.tags,
schedule: alert.schedule,
params: alert.params,
notifyWhen: alert.notifyWhen,
},
});
return { ...updatedAlert, ruleTypeId: updatedAlert.alertTypeId };
Expand All @@ -145,25 +147,39 @@ export class DefaultAlertService {
return await this.createDefaultAlertIfNotExist(ruleType, name, interval);
}

async getAlertActions() {
async getAlertActions(ruleType: DefaultRuleType) {
const { actionConnectors, settings } = await this.getActionConnectors();

const defaultActions = (actionConnectors ?? []).filter((act) =>
settings?.defaultConnectors?.includes(act.id)
);

return populateAlertActions({
groupId: ACTION_GROUP_DEFINITIONS.MONITOR_STATUS.id,
defaultActions,
defaultEmail: settings?.defaultEmail!,
translations: {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
if (ruleType === SYNTHETICS_STATUS_RULE) {
return populateAlertActions({
defaultActions,
groupId: ACTION_GROUP_DEFINITIONS.MONITOR_STATUS.id,
defaultEmail: settings?.defaultEmail!,
translations: {
defaultActionMessage: SyntheticsMonitorStatusTranslations.defaultActionMessage,
defaultRecoveryMessage: SyntheticsMonitorStatusTranslations.defaultRecoveryMessage,
defaultSubjectMessage: SyntheticsMonitorStatusTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage:
SyntheticsMonitorStatusTranslations.defaultRecoverySubjectMessage,
},
});
} else {
return populateAlertActions({
defaultActions,
groupId: ACTION_GROUP_DEFINITIONS.TLS_CERTIFICATE.id,
defaultEmail: settings?.defaultEmail!,
translations: {
defaultActionMessage: TlsTranslations.defaultActionMessage,
defaultRecoveryMessage: TlsTranslations.defaultRecoveryMessage,
defaultSubjectMessage: TlsTranslations.defaultSubjectMessage,
defaultRecoverySubjectMessage: TlsTranslations.defaultRecoverySubjectMessage,
},
});
}
}

async getActionConnectors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export const updateDefaultAlertingRoute: SyntheticsRestApiRouteFactory = () => (
handler: async ({ context, server, savedObjectsClient }): Promise<any> => {
const defaultAlertService = new DefaultAlertService(context, server, savedObjectsClient);

return Promise.allSettled([
const [statusRule, tlsRule] = await Promise.all([
defaultAlertService.updateStatusRule(),
defaultAlertService.updateTlsRule(),
]);
return {
statusRule,
tlsRule,
};
},
});
12 changes: 9 additions & 3 deletions x-pack/test/alerting_api_integration/observability/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

// eslint-disable-next-line import/no-default-export
export default function ({ loadTestFile }: any) {
describe('MetricsUI Endpoints', () => {
loadTestFile(require.resolve('./metric_threshold_rule'));
loadTestFile(require.resolve('./threshold_rule'));
describe('Observability Rules', () => {
describe('MetricsUI Endpoints', () => {
loadTestFile(require.resolve('./metric_threshold_rule'));
loadTestFile(require.resolve('./threshold_rule'));
});

describe('Synthetics', () => {
loadTestFile(require.resolve('./synthetics_rule'));
});
});
}
Loading

0 comments on commit 93fc2a8

Please sign in to comment.