Skip to content

Commit

Permalink
[Response Ops][Alerting] Creating global service for fetching and cac…
Browse files Browse the repository at this point in the history
…hing rules settings (#192404)

Resolves #184321 and
#149884

## Summary

Created a `RulesSettingsService` that caches flapping and query delay
settings per space for 1 minute so rules are not accessing the SO index
with every execution.

Also moved all classes related to the rules settings into the
`rules_settings` folder so some of these changes are just renaming
changes.

## To Verify
1. Add some logging to
`x-pack/plugins/alerting/server/rules_settings/rules_settings_service.ts`
to indicate when settings are being fetched and when they're returning
from the cache.
2. Create and run some rules in different spaces to see that flapping
and query delay settings are being returned from the cache when the
cache has not expired.

---------

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
ymao1 and elasticmachine authored Sep 16, 2024
1 parent f458714 commit 7a0fe2e
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 @@ -17,7 +17,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 7a0fe2e

Please sign in to comment.