From 40f37ee06745853de6a1ab1de4fe17731477189a Mon Sep 17 00:00:00 2001 From: Dominique Clarke Date: Mon, 16 Sep 2024 09:31:27 -0400 Subject: [PATCH] [8.15] [Synthetics] default rules - honor cluster minimum rule interval (#191863) (#192580) # Backport This will backport the following commits from `main` to `8.15`: - [[Synthetics] default rules - honor cluster minimum rule interval (#191863)](https://github.com/elastic/kibana/pull/191863) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Elastic Machine --- .../synthetics/server/plugin.ts | 1 + .../default_alerts/default_alert_service.ts | 36 ++++++++++++++++--- .../synthetics/server/types.ts | 8 +++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/server/plugin.ts b/x-pack/plugins/observability_solution/synthetics/server/plugin.ts index 01466504d22b4..45bbf531747ae 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/plugin.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/plugin.ts @@ -73,6 +73,7 @@ export class Plugin implements PluginType { telemetry: this.telemetryEventsSender, isDev: this.initContext.env.mode.dev, share: plugins.share, + alerting: plugins.alerting, } as SyntheticsServerSetup; this.syntheticsService = new SyntheticsService(this.server); diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/default_alerts/default_alert_service.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/default_alerts/default_alert_service.ts index 38606b4d2865f..fe4f2517be2d2 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/default_alerts/default_alert_service.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/default_alerts/default_alert_service.ts @@ -6,6 +6,7 @@ */ import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; +import { parseDuration } from '@kbn/alerting-plugin/server'; import { FindActionResult } from '@kbn/actions-plugin/server'; import { savedObjectsAdapter } from '../../saved_objects'; import { populateAlertActions } from '../../../common/rules/alert_actions'; @@ -55,19 +56,29 @@ export class DefaultAlertService { }; } + getMinimumRuleInterval() { + const minimumInterval = this.server.alerting.getConfig().minimumScheduleInterval; + const minimumIntervalInMs = parseDuration(minimumInterval.value); + const defaultIntervalInMs = parseDuration('1m'); + const interval = minimumIntervalInMs < defaultIntervalInMs ? '1m' : minimumInterval.value; + return interval; + } + setupStatusRule() { + const minimumRuleInterval = this.getMinimumRuleInterval(); return this.createDefaultAlertIfNotExist( SYNTHETICS_STATUS_RULE, `Synthetics status internal rule`, - '1m' + minimumRuleInterval ); } setupTlsRule() { + const minimumRuleInterval = this.getMinimumRuleInterval(); return this.createDefaultAlertIfNotExist( SYNTHETICS_TLS_RULE, `Synthetics internal TLS rule`, - '1m' + minimumRuleInterval ); } @@ -122,10 +133,20 @@ export class DefaultAlertService { } updateStatusRule() { - return this.updateDefaultAlert(SYNTHETICS_STATUS_RULE, `Synthetics status internal rule`, '1m'); + const minimumRuleInterval = this.getMinimumRuleInterval(); + return this.updateDefaultAlert( + SYNTHETICS_STATUS_RULE, + `Synthetics status internal rule`, + minimumRuleInterval + ); } updateTlsRule() { - return this.updateDefaultAlert(SYNTHETICS_TLS_RULE, `Synthetics internal TLS rule`, '1m'); + const minimumRuleInterval = this.getMinimumRuleInterval(); + return this.updateDefaultAlert( + SYNTHETICS_TLS_RULE, + `Synthetics internal TLS rule`, + minimumRuleInterval + ); } async updateDefaultAlert(ruleType: DefaultRuleType, name: string, interval: string) { @@ -133,6 +154,8 @@ export class DefaultAlertService { const alert = await this.getExistingAlert(ruleType); if (alert) { + const currentIntervalInMs = parseDuration(alert.schedule.interval); + const minimumIntervalInMs = parseDuration(interval); const actions = await this.getAlertActions(ruleType); const { actions: actionsFromRules = [], @@ -144,7 +167,10 @@ export class DefaultAlertService { actions, name: alert.name, tags: alert.tags, - schedule: alert.schedule, + schedule: { + interval: + currentIntervalInMs < minimumIntervalInMs ? interval : alert.schedule.interval, + }, params: alert.params, }, }); diff --git a/x-pack/plugins/observability_solution/synthetics/server/types.ts b/x-pack/plugins/observability_solution/synthetics/server/types.ts index 9bfab3c676d7f..43bad3167e0da 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/types.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/types.ts @@ -17,6 +17,7 @@ import { Logger, SavedObjectsClientContract, } from '@kbn/core/server'; +import { PluginSetupContract as AlertingPluginSetup } from '@kbn/alerting-plugin/server'; import { SharePluginSetup } from '@kbn/share-plugin/server'; import { ObservabilityPluginSetup } from '@kbn/observability-plugin/server'; import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; @@ -30,7 +31,7 @@ import { EncryptedSavedObjectsPluginSetup, EncryptedSavedObjectsPluginStart, } from '@kbn/encrypted-saved-objects-plugin/server'; -import { PluginSetupContract } from '@kbn/features-plugin/server'; +import { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-plugin/server'; import { RuleRegistryPluginSetupContract } from '@kbn/rule-registry-plugin/server'; import { TaskManagerSetupContract, @@ -58,13 +59,14 @@ export interface SyntheticsServerSetup { basePath: IBasePath; isDev?: boolean; coreStart: CoreStart; + alerting: AlertingPluginSetup; pluginsStart: SyntheticsPluginsStartDependencies; isElasticsearchServerless: boolean; } export interface SyntheticsPluginsSetupDependencies { - features: PluginSetupContract; - alerting: any; + features: FeaturesPluginSetup; + alerting: AlertingPluginSetup; observability: ObservabilityPluginSetup; usageCollection: UsageCollectionSetup; ml: MlSetup;