Skip to content

Commit

Permalink
[8.15] [Synthetics] default rules - honor cluster minimum rule interv…
Browse files Browse the repository at this point in the history
…al (#191863) (#192580)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[Synthetics] default rules - honor cluster minimum rule interval
(#191863)](#191863)

<!--- Backport version: 8.9.8 -->

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

<!--BACKPORT [{"author":{"name":"Dominique
Clarke","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-10T18:26:36Z","message":"[Synthetics]
default rules - honor cluster minimum rule interval (#191863)\n\n##
Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/191854\r\n\r\nUtilizes the
cluster's minimum rule interval as the default rule\r\ninterval for
Synthetics default rules.\r\n\r\n### Testing\r\n1. Start Kibana on main,
navigate to Synthetics, and create a monitor.\r\nThis should auto create
a Synthetics default alert.\r\n2. Check out this PR.\r\n3. Add the
following to your Kibana
yml\r\n```\r\nxpack.alerting.rules.minimumScheduleInterval.value:
3m\r\nxpack.alerting.rules.minimumScheduleInterval.enforce:
true\r\n```\r\n3. Navigate to Synthetics settings and change one of the
settings. For\r\nexample, you can add a default alert connector\r\n4.
Navigate to the Manage Rules and confirm the new rule interval is
3m.","sha":"0fda881b76316852c3ad5daac530dc6d65539099","branchLabelMapping":{"^v8.16.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","ci:project-deploy-observability","Team:obs-ux-management","v8.16.0","v8.15.2"],"number":191863,"url":"https://github.com/elastic/kibana/pull/191863","mergeCommit":{"message":"[Synthetics]
default rules - honor cluster minimum rule interval (#191863)\n\n##
Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/191854\r\n\r\nUtilizes the
cluster's minimum rule interval as the default rule\r\ninterval for
Synthetics default rules.\r\n\r\n### Testing\r\n1. Start Kibana on main,
navigate to Synthetics, and create a monitor.\r\nThis should auto create
a Synthetics default alert.\r\n2. Check out this PR.\r\n3. Add the
following to your Kibana
yml\r\n```\r\nxpack.alerting.rules.minimumScheduleInterval.value:
3m\r\nxpack.alerting.rules.minimumScheduleInterval.enforce:
true\r\n```\r\n3. Navigate to Synthetics settings and change one of the
settings. For\r\nexample, you can add a default alert connector\r\n4.
Navigate to the Manage Rules and confirm the new rule interval is
3m.","sha":"0fda881b76316852c3ad5daac530dc6d65539099"}},"sourceBranch":"main","suggestedTargetBranches":["8.15"],"targetPullRequestStates":[{"branch":"main","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/191863","number":191863,"mergeCommit":{"message":"[Synthetics]
default rules - honor cluster minimum rule interval (#191863)\n\n##
Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/191854\r\n\r\nUtilizes the
cluster's minimum rule interval as the default rule\r\ninterval for
Synthetics default rules.\r\n\r\n### Testing\r\n1. Start Kibana on main,
navigate to Synthetics, and create a monitor.\r\nThis should auto create
a Synthetics default alert.\r\n2. Check out this PR.\r\n3. Add the
following to your Kibana
yml\r\n```\r\nxpack.alerting.rules.minimumScheduleInterval.value:
3m\r\nxpack.alerting.rules.minimumScheduleInterval.enforce:
true\r\n```\r\n3. Navigate to Synthetics settings and change one of the
settings. For\r\nexample, you can add a default alert connector\r\n4.
Navigate to the Manage Rules and confirm the new rule interval is
3m.","sha":"0fda881b76316852c3ad5daac530dc6d65539099"}},{"branch":"8.15","label":"v8.15.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
dominiqueclarke and elasticmachine authored Sep 16, 2024
1 parent 7043ff2 commit 40f37ee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -122,17 +133,29 @@ 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) {
const rulesClient = (await this.context.alerting)?.getRulesClient();

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 = [],
Expand All @@ -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,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 40f37ee

Please sign in to comment.