Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.9] [Synthetics] Fixed action connectors and added api test for default alerting (#161218) #161294

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import { FindActionResult } from '@kbn/actions-plugin/server';
import { savedObjectsAdapter } from '../../legacy_uptime/lib/saved_objects';
import { UptimeServerSetup } from '../../legacy_uptime/lib/adapters';
import { populateAlertActions } from '../../../common/rules/alert_actions';
import { SyntheticsMonitorStatusTranslations } from '../../../common/rules/synthetics/translations';
import { UptimeRequestHandlerContext } from '../../types';
import {
SyntheticsMonitorStatusTranslations,
TlsTranslations,
} from '../../../common/rules/synthetics/translations';
import {
ACTION_GROUP_DEFINITIONS,
SYNTHETICS_STATUS_RULE,
Expand Down Expand Up @@ -65,7 +68,7 @@ export class DefaultAlertService {
return this.createDefaultAlertIfNotExist(
SYNTHETICS_TLS_RULE,
`Synthetics internal TLS alert`,
'10m'
'1m'
);
}

Expand Down Expand Up @@ -93,7 +96,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 @@ -120,15 +123,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 @@ -137,7 +140,6 @@ export class DefaultAlertService {
tags: alert.tags,
schedule: alert.schedule,
params: alert.params,
notifyWhen: alert.notifyWhen,
},
});
return { ...updatedAlert, ruleTypeId: updatedAlert.alertTypeId };
Expand All @@ -146,25 +148,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