Skip to content

Commit

Permalink
[8.9] [Synthetics] Fixed action connectors and added api test for def…
Browse files Browse the repository at this point in the history
…ault alerting (#161218) (#161294)

# Backport

This will backport the following commits from `main` to `8.9`:
- [[Synthetics] Fixed action connectors and added api test for default
alerting (#161218)](#161218)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2023-07-05T19:46:18Z","message":"[Synthetics]
Fixed action connectors and added api test for default alerting
(#161218)","sha":"93fc2a85d69529aa5123fc4dc3921432dd5b60ec","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:uptime","release_note:skip","v8.9.0","v8.10.0"],"number":161218,"url":"https://github.com/elastic/kibana/pull/161218","mergeCommit":{"message":"[Synthetics]
Fixed action connectors and added api test for default alerting
(#161218)","sha":"93fc2a85d69529aa5123fc4dc3921432dd5b60ec"}},"sourceBranch":"main","suggestedTargetBranches":["8.9"],"targetPullRequestStates":[{"branch":"8.9","label":"v8.9.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/161218","number":161218,"mergeCommit":{"message":"[Synthetics]
Fixed action connectors and added api test for default alerting
(#161218)","sha":"93fc2a85d69529aa5123fc4dc3921432dd5b60ec"}}]}]
BACKPORT-->
  • Loading branch information
shahzad31 authored Jul 6, 2023
1 parent 9ee8cd6 commit 449fce9
Show file tree
Hide file tree
Showing 5 changed files with 588 additions and 24 deletions.
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

0 comments on commit 449fce9

Please sign in to comment.