Skip to content

Commit

Permalink
[8.x] [Response Ops][Alerting] Creating global service for fetching a…
Browse files Browse the repository at this point in the history
…nd caching rules settings (elastic#192404) (elastic#193011)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Response Ops][Alerting] Creating global service for fetching and
caching rules settings
(elastic#192404)](elastic#192404)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Ying
Mao","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-16T13:22:44Z","message":"[Response
Ops][Alerting] Creating global service for fetching and caching rules
settings (elastic#192404)\n\nResolves
elastic#184321
and\r\nhttps://github.com/elastic/issues/149884\r\n\r\n##
Summary\r\n\r\nCreated a `RulesSettingsService` that caches flapping and
query delay\r\nsettings per space for 1 minute so rules are not
accessing the SO index\r\nwith every execution.\r\n\r\nAlso moved all
classes related to the rules settings into the\r\n`rules_settings`
folder so some of these changes are just renaming\r\nchanges.\r\n\r\n##
To Verify\r\n1. Add some logging
to\r\n`x-pack/plugins/alerting/server/rules_settings/rules_settings_service.ts`\r\nto
indicate when settings are being fetched and when they're
returning\r\nfrom the cache.\r\n2. Create and run some rules in
different spaces to see that flapping\r\nand query delay settings are
being returned from the cache when the\r\ncache has not
expired.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"7a0fe2e83683cf38c537694daa88a3a75bfe3e57","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Alerting","release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Response
Ops][Alerting] Creating global service for fetching and caching rules
settings","number":192404,"url":"https://github.com/elastic/kibana/pull/192404","mergeCommit":{"message":"[Response
Ops][Alerting] Creating global service for fetching and caching rules
settings (elastic#192404)\n\nResolves
elastic#184321
and\r\nhttps://github.com/elastic/issues/149884\r\n\r\n##
Summary\r\n\r\nCreated a `RulesSettingsService` that caches flapping and
query delay\r\nsettings per space for 1 minute so rules are not
accessing the SO index\r\nwith every execution.\r\n\r\nAlso moved all
classes related to the rules settings into the\r\n`rules_settings`
folder so some of these changes are just renaming\r\nchanges.\r\n\r\n##
To Verify\r\n1. Add some logging
to\r\n`x-pack/plugins/alerting/server/rules_settings/rules_settings_service.ts`\r\nto
indicate when settings are being fetched and when they're
returning\r\nfrom the cache.\r\n2. Create and run some rules in
different spaces to see that flapping\r\nand query delay settings are
being returned from the cache when the\r\ncache has not
expired.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"7a0fe2e83683cf38c537694daa88a3a75bfe3e57"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192404","number":192404,"mergeCommit":{"message":"[Response
Ops][Alerting] Creating global service for fetching and caching rules
settings (elastic#192404)\n\nResolves
elastic#184321
and\r\nhttps://github.com/elastic/issues/149884\r\n\r\n##
Summary\r\n\r\nCreated a `RulesSettingsService` that caches flapping and
query delay\r\nsettings per space for 1 minute so rules are not
accessing the SO index\r\nwith every execution.\r\n\r\nAlso moved all
classes related to the rules settings into the\r\n`rules_settings`
folder so some of these changes are just renaming\r\nchanges.\r\n\r\n##
To Verify\r\n1. Add some logging
to\r\n`x-pack/plugins/alerting/server/rules_settings/rules_settings_service.ts`\r\nto
indicate when settings are being fetched and when they're
returning\r\nfrom the cache.\r\n2. Create and run some rules in
different spaces to see that flapping\r\nand query delay settings are
being returned from the cache when the\r\ncache has not
expired.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"7a0fe2e83683cf38c537694daa88a3a75bfe3e57"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ying Mao <[email protected]>
  • Loading branch information
kibanamachine and ymao1 authored Sep 16, 2024
1 parent 02f8d09 commit 7939f22
Show file tree
Hide file tree
Showing 38 changed files with 563 additions and 157 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ describe('config validation', () => {
},
},
},
"rulesSettings": Object {
"cacheInterval": 60000,
},
}
`);
});
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/alerting/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { schema, TypeOf } from '@kbn/config-schema';
import { validateDurationSchema, parseDuration } from './lib';
import { DEFAULT_CACHE_INTERVAL_MS } from './rules_settings';

export const DEFAULT_MAX_ALERTS = 1000;
const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -74,6 +75,9 @@ export const configSchema = schema.object({
enableFrameworkAlerts: schema.boolean({ defaultValue: true }),
cancelAlertsOnRuleTimeout: schema.boolean({ defaultValue: true }),
rules: rulesSchema,
rulesSettings: schema.object({
cacheInterval: schema.number({ defaultValue: DEFAULT_CACHE_INTERVAL_MS }),
}),
});

export type AlertingConfig = TypeOf<typeof configSchema>;
Expand Down
48 changes: 28 additions & 20 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ import { ServerlessPluginSetup } from '@kbn/serverless/server';
import { RuleTypeRegistry } from './rule_type_registry';
import { TaskRunnerFactory } from './task_runner';
import { RulesClientFactory } from './rules_client_factory';
import { RulesSettingsClientFactory } from './rules_settings_client_factory';
import {
RulesSettingsClientFactory,
RulesSettingsService,
getRulesSettingsFeature,
} from './rules_settings';
import { MaintenanceWindowClientFactory } from './maintenance_window_client_factory';
import { ILicenseState, LicenseState } from './lib/license_state';
import { AlertingRequestHandlerContext, ALERTING_FEATURE_ID, RuleAlertData } from './types';
Expand Down Expand Up @@ -106,7 +110,6 @@ import {
type InitializationPromise,
errorResult,
} from './alerts_service';
import { getRulesSettingsFeature } from './rules_settings_feature';
import { maintenanceWindowFeature } from './maintenance_window_feature';
import { ConnectorAdapterRegistry } from './connector_adapters/connector_adapter_registry';
import { ConnectorAdapter, ConnectorAdapterParams } from './connector_adapters/types';
Expand Down Expand Up @@ -588,33 +591,38 @@ export class AlertingPlugin {
};

taskRunnerFactory.initialize({
logger,
actionsConfigMap: getActionsConfigMap(this.config.rules.run.actions),
actionsPlugin: plugins.actions,
alertsService: this.alertsService,
backfillClient: this.backfillClient!,
basePathService: core.http.basePath,
cancelAlertsOnRuleTimeout: this.config.cancelAlertsOnRuleTimeout,
connectorAdapterRegistry: this.connectorAdapterRegistry,
data: plugins.data,
share: plugins.share,
dataViews: plugins.dataViews,
savedObjects: core.savedObjects,
uiSettings: core.uiSettings,
elasticsearch: core.elasticsearch,
getRulesClientWithRequest,
spaceIdToNamespace,
actionsPlugin: plugins.actions,
encryptedSavedObjectsClient,
basePathService: core.http.basePath,
eventLogger: this.eventLogger!,
executionContext: core.executionContext,
ruleTypeRegistry: this.ruleTypeRegistry!,
alertsService: this.alertsService,
getMaintenanceWindowClientWithRequest,
getRulesClientWithRequest,
kibanaBaseUrl: this.kibanaBaseUrl,
supportsEphemeralTasks: plugins.taskManager.supportsEphemeralTasks(),
maxEphemeralActionsPerRule: this.config.maxEphemeralActionsPerAlert,
cancelAlertsOnRuleTimeout: this.config.cancelAlertsOnRuleTimeout,
logger,
maxAlerts: this.config.rules.run.alerts.max,
actionsConfigMap: getActionsConfigMap(this.config.rules.run.actions),
maxEphemeralActionsPerRule: this.config.maxEphemeralActionsPerAlert,
ruleTypeRegistry: this.ruleTypeRegistry!,
rulesSettingsService: new RulesSettingsService({
cacheInterval: this.config.rulesSettings.cacheInterval,
getRulesSettingsClientWithRequest,
isServerless: !!plugins.serverless,
logger,
}),
savedObjects: core.savedObjects,
share: plugins.share,
spaceIdToNamespace,
supportsEphemeralTasks: plugins.taskManager.supportsEphemeralTasks(),
uiSettings: core.uiSettings,
usageCounter: this.usageCounter,
getRulesSettingsClientWithRequest,
getMaintenanceWindowClientWithRequest,
backfillClient: this.backfillClient!,
connectorAdapterRegistry: this.connectorAdapterRegistry,
});

this.eventLogService!.registerSavedObjectProvider(RULE_SAVED_OBJECT_TYPE, (request) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { httpServerMock } from '@kbn/core/server/mocks';
import { actionsClientMock } from '@kbn/actions-plugin/server/mocks';
import type { ActionsClientMock } from '@kbn/actions-plugin/server/mocks';
import { rulesClientMock, RulesClientMock } from '../rules_client.mock';
import { rulesSettingsClientMock, RulesSettingsClientMock } from '../rules_settings_client.mock';
import {
rulesSettingsClientMock,
RulesSettingsClientMock,
} from '../rules_settings/rules_settings_client.mock';
import {
maintenanceWindowClientMock,
MaintenanceWindowClientMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { httpServiceMock } from '@kbn/core/server/mocks';
import { licenseStateMock } from '../lib/license_state.mock';
import { mockHandlerArguments } from './_mock_handler_arguments';
import { rulesSettingsClientMock, RulesSettingsClientMock } from '../rules_settings_client.mock';
import {
rulesSettingsClientMock,
RulesSettingsClientMock,
} from '../rules_settings/rules_settings_client.mock';
import { getFlappingSettingsRoute } from './get_flapping_settings';

let rulesSettingsClient: RulesSettingsClientMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { mockHandlerArguments } from '../../../_mock_handler_arguments';
import {
rulesSettingsClientMock,
RulesSettingsClientMock,
} from '../../../../rules_settings_client.mock';
} from '../../../../rules_settings/rules_settings_client.mock';
import { getQueryDelaySettingsRoute } from './get_query_delay_settings';

let rulesSettingsClient: RulesSettingsClientMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { mockHandlerArguments } from '../../../_mock_handler_arguments';
import {
rulesSettingsClientMock,
RulesSettingsClientMock,
} from '../../../../rules_settings_client.mock';
} from '../../../../rules_settings/rules_settings_client.mock';
import { updateQueryDelaySettingsRoute } from './update_query_delay_settings';

let rulesSettingsClient: RulesSettingsClientMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { httpServiceMock } from '@kbn/core/server/mocks';
import { licenseStateMock } from '../lib/license_state.mock';
import { mockHandlerArguments } from './_mock_handler_arguments';
import { rulesSettingsClientMock, RulesSettingsClientMock } from '../rules_settings_client.mock';
import {
rulesSettingsClientMock,
RulesSettingsClientMock,
} from '../rules_settings/rules_settings_client.mock';
import { updateFlappingSettingsRoute } from './update_flapping_settings';

let rulesSettingsClient: RulesSettingsClientMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
export * from './rules_settings_client';
export * from './flapping/rules_settings_flapping_client';
export * from './query_delay/rules_settings_query_delay_client';
export * from './rules_settings_client_factory';
export * from './rules_settings_feature';
export * from './rules_settings_service';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RulesSettingsQueryDelayClientApi,
DEFAULT_FLAPPING_SETTINGS,
DEFAULT_QUERY_DELAY_SETTINGS,
} from './types';
} from '../types';

export type RulesSettingsClientMock = jest.Mocked<RulesSettingsClientApi>;
export type RulesSettingsFlappingClientMock = jest.Mocked<RulesSettingsFlappingClientApi>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@kbn/core/server/mocks';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { SECURITY_EXTENSION_ID } from '@kbn/core-saved-objects-server';
import { RULES_SETTINGS_SAVED_OBJECT_TYPE } from '../common';
import { RULES_SETTINGS_SAVED_OBJECT_TYPE } from '../../common';

jest.mock('./rules_settings_client');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SecurityServiceStart,
} from '@kbn/core/server';
import { RulesSettingsClient } from './rules_settings_client';
import { RULES_SETTINGS_SAVED_OBJECT_TYPE } from '../common';
import { RULES_SETTINGS_SAVED_OBJECT_TYPE } from '../../common';

export interface RulesSettingsClientFactoryOpts {
logger: Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
RULES_SETTINGS_SAVED_OBJECT_TYPE,
ALL_QUERY_DELAY_SETTINGS_SUB_FEATURE_ID,
READ_QUERY_DELAY_SETTINGS_SUB_FEATURE_ID,
} from '../common';
} from '../../common';

export function getRulesSettingsFeature(isServerless: boolean): KibanaFeatureConfig {
const settings = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { DEFAULT_FLAPPING_SETTINGS, DEFAULT_QUERY_DELAY_SETTINGS } from '../types';

const createRulesSettingsServiceMock = () => {
return jest.fn().mockImplementation(() => {
return {
getSettings: jest.fn().mockReturnValue({
queryDelaySettings: DEFAULT_QUERY_DELAY_SETTINGS,
flappingSettings: DEFAULT_FLAPPING_SETTINGS,
}),
};
});
};

export const rulesSettingsServiceMock = {
create: createRulesSettingsServiceMock(),
};
Loading

0 comments on commit 7939f22

Please sign in to comment.