Skip to content

Commit

Permalink
[Response Ops][Alerting] Remove rules client from alerting task runner (
Browse files Browse the repository at this point in the history
elastic#194300)

## Summary

Initializing the rules client with every rule execution causes 2
requests to Elasticsearch, a `POST /_security/user/_has_privileges`
request and a `GET/.kibana_<version>/_doc/space:<spaceId>` request. We
only use the rules client to proxy two methods `getAlertFromRaw` and
`clearExpiredSnoozes`. Both of these methods can be converted into
library functions that can be used without the rules client. This PR
makes that conversion.

---------

Co-authored-by: Elastic Machine <[email protected]>
(cherry picked from commit 326f813)
  • Loading branch information
ymao1 committed Oct 3, 2024
1 parent 5998ecd commit 2367fa0
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ export async function findRules<Params extends RuleParams = never>(
throw error;
}

const rule = getAlertFromRaw<Params>(
context,
const rule = getAlertFromRaw<Params>({
excludeFromPublicApi,
id,
attributes.alertTypeId,
(fields ? pick(attributes, fields) : attributes) as RawRule,
includeLegacyId: false,
includeSnoozeData,
isSystemAction: context.isSystemAction,
logger: context.logger,
rawRule: (fields ? pick(attributes, fields) : attributes) as RawRule,
references,
false,
excludeFromPublicApi,
includeSnoozeData
);
ruleTypeId: attributes.alertTypeId,
ruleTypeRegistry: context.ruleTypeRegistry,
});

// collect SIEM rule for further formatting legacy actions
if (attributes.consumer === AlertConsumers.SIEM) {
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ export class AlertingPlugin {
encryptedSavedObjectsClient,
eventLogger: this.eventLogger!,
executionContext: core.executionContext,
getRulesClientWithRequest,
kibanaBaseUrl: this.kibanaBaseUrl,
logger,
maintenanceWindowsService: new MaintenanceWindowsService({
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/alerting/server/rules_client.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ const createRulesClientMock = () => {
bulkDisableRules: jest.fn(),
snooze: jest.fn(),
unsnooze: jest.fn(),
clearExpiredSnoozes: jest.fn(),
runSoon: jest.fn(),
clone: jest.fn(),
getAlertFromRaw: jest.fn(),
getScheduleFrequency: jest.fn(),
bulkUntrackAlerts: jest.fn(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ export async function createRuleSavedObject<Params extends RuleTypeParams = neve
return createdAlert as SavedObject<RuleAttributes>;
}

return getAlertFromRaw<Params>(
context,
createdAlert.id,
createdAlert.attributes.alertTypeId,
createdAlert.attributes,
return getAlertFromRaw<Params>({
excludeFromPublicApi: true,
id: createdAlert.id,
includeLegacyId: false,
isSystemAction: context.isSystemAction,
logger: context.logger,
rawRule: createdAlert.attributes,
references,
false,
true
);
ruleTypeId: createdAlert.attributes.alertTypeId,
ruleTypeRegistry: context.ruleTypeRegistry,
});
}
115 changes: 70 additions & 45 deletions x-pack/plugins/alerting/server/rules_client/lib/get_alert_from_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { omit, isEmpty } from 'lodash';
import { SavedObjectReference } from '@kbn/core/server';
import { Logger, SavedObjectReference } from '@kbn/core/server';
import {
Rule,
PartialRule,
Expand All @@ -15,6 +15,7 @@ import {
RuleTypeParams,
RuleWithLegacyId,
PartialRuleWithLegacyId,
RuleTypeRegistry,
} from '../../types';
import {
ruleExecutionStatusFromRaw,
Expand All @@ -24,11 +25,11 @@ import {
import { UntypedNormalizedRuleType } from '../../rule_type_registry';
import { getActiveScheduledSnoozes } from '../../lib/is_rule_snoozed';
import { injectReferencesIntoParams } from '../common';
import { RulesClientContext } from '../types';
import {
transformRawActionsToDomainActions,
transformRawActionsToDomainSystemActions,
} from '../../application/rule/transforms/transform_raw_actions_to_domain_actions';
import { fieldsToExcludeFromPublicApi } from '../rules_client';

export interface GetAlertFromRawParams {
id: string;
Expand All @@ -41,35 +42,52 @@ export interface GetAlertFromRawParams {
omitGeneratedValues?: boolean;
}

interface GetAlertFromRawOpts {
excludeFromPublicApi?: boolean;
id: string;
includeLegacyId?: boolean;
includeSnoozeData?: boolean;
isSystemAction: (actionId: string) => boolean;
logger: Logger;
omitGeneratedValues?: boolean;
rawRule: RawRule;
references: SavedObjectReference[] | undefined;
ruleTypeId: string;
ruleTypeRegistry: RuleTypeRegistry;
}

type GetPartialRuleFromRawOpts = Omit<GetAlertFromRawOpts, 'ruleTypeRegistry'> & {
ruleType: UntypedNormalizedRuleType;
};
/**
* @deprecated in favor of transformRuleAttributesToRuleDomain
*/
export function getAlertFromRaw<Params extends RuleTypeParams>(
context: RulesClientContext,
id: string,
ruleTypeId: string,
rawRule: RawRule,
references: SavedObjectReference[] | undefined,
includeLegacyId: boolean = false,
excludeFromPublicApi: boolean = false,
includeSnoozeData: boolean = false,
omitGeneratedValues: boolean = true
opts: GetAlertFromRawOpts
): Rule | RuleWithLegacyId {
const ruleType = context.ruleTypeRegistry.get(ruleTypeId);
const {
excludeFromPublicApi = false,
includeLegacyId = false,
includeSnoozeData = false,
omitGeneratedValues = true,
} = opts;
const ruleType = opts.ruleTypeRegistry.get(opts.ruleTypeId);
// In order to support the partial update API of Saved Objects we have to support
// partial updates of an Alert, but when we receive an actual RawRule, it is safe
// to cast the result to an Alert
const res = getPartialRuleFromRaw<Params>(
context,
id,
ruleType,
rawRule,
references,
includeLegacyId,
const res = getPartialRuleFromRaw<Params>({
excludeFromPublicApi,
id: opts.id,
includeLegacyId,
includeSnoozeData,
omitGeneratedValues
);
isSystemAction: opts.isSystemAction,
logger: opts.logger,
omitGeneratedValues,
rawRule: opts.rawRule,
references: opts.references,
ruleTypeId: opts.ruleTypeId,
ruleType,
});
// include to result because it is for internal rules client usage
if (includeLegacyId) {
return res as RuleWithLegacyId;
Expand All @@ -78,11 +96,18 @@ export function getAlertFromRaw<Params extends RuleTypeParams>(
return omit(res, ['legacyId']) as Rule;
}

export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
context: RulesClientContext,
id: string,
ruleType: UntypedNormalizedRuleType,
{
function getPartialRuleFromRaw<Params extends RuleTypeParams>(
opts: GetPartialRuleFromRawOpts
): PartialRule<Params> | PartialRuleWithLegacyId<Params> {
const {
excludeFromPublicApi = false,
includeLegacyId = false,
includeSnoozeData = false,
omitGeneratedValues = true,
rawRule,
} = opts;

const {
createdAt,
updatedAt,
meta,
Expand All @@ -99,13 +124,8 @@ export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
lastRun,
isSnoozedUntil: DoNotUseIsSNoozedUntil,
...partialRawRule
}: Partial<RawRule>,
references: SavedObjectReference[] | undefined,
includeLegacyId: boolean = false,
excludeFromPublicApi: boolean = false,
includeSnoozeData: boolean = false,
omitGeneratedValues: boolean = true
): PartialRule<Params> | PartialRuleWithLegacyId<Params> {
} = rawRule;

const snoozeScheduleDates = snoozeSchedule?.map((s) => ({
...s,
rRule: {
Expand All @@ -125,31 +145,36 @@ export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
const includeMonitoring = monitoring && !excludeFromPublicApi;

const rule: PartialRule<Params> = {
id,
id: opts.id,
notifyWhen,
...omit(partialRawRule, excludeFromPublicApi ? [...context.fieldsToExcludeFromPublicApi] : ''),
...omit(partialRawRule, excludeFromPublicApi ? [...fieldsToExcludeFromPublicApi] : ''),
// we currently only support the Interval Schedule type
// Once we support additional types, this type signature will likely change
schedule: schedule as IntervalSchedule,
actions: actions
? transformRawActionsToDomainActions({
ruleId: id,
ruleId: opts.id,
actions,
references: references || [],
isSystemAction: context.isSystemAction,
references: opts.references || [],
isSystemAction: opts.isSystemAction,
omitGeneratedValues,
})
: [],
systemActions: actions
? transformRawActionsToDomainSystemActions({
ruleId: id,
ruleId: opts.id,
actions,
references: references || [],
isSystemAction: context.isSystemAction,
references: opts.references || [],
isSystemAction: opts.isSystemAction,
omitGeneratedValues,
})
: [],
params: injectReferencesIntoParams(id, ruleType, params, references || []) as Params,
params: injectReferencesIntoParams(
opts.id,
opts.ruleType,
params,
opts.references || []
) as Params,
...(excludeFromPublicApi ? {} : { snoozeSchedule: snoozeScheduleDates ?? [] }),
...(includeSnoozeData && !excludeFromPublicApi
? {
Expand All @@ -164,10 +189,10 @@ export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
...(createdAt ? { createdAt: new Date(createdAt) } : {}),
...(scheduledTaskId ? { scheduledTaskId } : {}),
...(executionStatus
? { executionStatus: ruleExecutionStatusFromRaw(context.logger, id, executionStatus) }
? { executionStatus: ruleExecutionStatusFromRaw(opts.logger, opts.id, executionStatus) }
: {}),
...(includeMonitoring
? { monitoring: convertMonitoringFromRawAndVerify(context.logger, id, monitoring) }
? { monitoring: convertMonitoringFromRawAndVerify(opts.logger, opts.id, monitoring) }
: {}),
...(nextRun ? { nextRun: new Date(nextRun) } : {}),
...(lastRun
Expand All @@ -193,7 +218,7 @@ export function getPartialRuleFromRaw<Params extends RuleTypeParams>(
// Need the `rule` object to build a URL
if (!excludeFromPublicApi) {
const viewInAppRelativeUrl =
ruleType.getViewInAppRelativeUrl && ruleType.getViewInAppRelativeUrl({ rule });
opts.ruleType.getViewInAppRelativeUrl && opts.ruleType.getViewInAppRelativeUrl({ rule });
if (viewInAppRelativeUrl) {
rule.viewInAppRelativeUrl = viewInAppRelativeUrl;
}
Expand Down

This file was deleted.

22 changes: 1 addition & 21 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ import { bulkEnableRules, BulkEnableRulesParams } from '../application/rule/meth
import { enableRule } from '../application/rule/methods/enable_rule/enable_rule';
import { updateRuleApiKey } from '../application/rule/methods/update_api_key/update_rule_api_key';
import { disableRule } from '../application/rule/methods/disable/disable_rule';
import { clearExpiredSnoozes } from './methods/clear_expired_snoozes';
import { muteInstance } from '../application/rule/methods/mute_alert/mute_instance';
import { muteAll } from './methods/mute_all';
import { unmuteAll } from './methods/unmute_all';
import { unmuteInstance } from '../application/rule/methods/unmute_alert/unmute_instance';
import { runSoon } from './methods/run_soon';
import { listRuleTypes } from './methods/list_rule_types';
import { getAlertFromRaw, GetAlertFromRawParams } from './lib/get_alert_from_raw';
import { getScheduleFrequency } from '../application/rule/methods/get_schedule_frequency/get_schedule_frequency';
import {
bulkUntrackAlerts,
Expand All @@ -84,7 +82,7 @@ export type ConstructorOptions = Omit<
'fieldsToExcludeFromPublicApi' | 'minimumScheduleIntervalInMs'
>;

const fieldsToExcludeFromPublicApi: Array<keyof SanitizedRule> = [
export const fieldsToExcludeFromPublicApi: Array<keyof SanitizedRule> = [
'monitoring',
'mapped_params',
'snoozeSchedule',
Expand Down Expand Up @@ -174,11 +172,6 @@ export class RulesClient {
public snooze = (options: SnoozeRuleOptions) => snoozeRule(this.context, options);
public unsnooze = (options: UnsnoozeParams) => unsnoozeRule(this.context, options);

public clearExpiredSnoozes = (options: {
rule: Pick<SanitizedRule<RuleTypeParams>, 'id' | 'snoozeSchedule'>;
version?: string;
}) => clearExpiredSnoozes(this.context, options);

public muteAll = (options: { id: string }) => muteAll(this.context, options);
public unmuteAll = (options: { id: string }) => unmuteAll(this.context, options);
public muteInstance = (options: MuteAlertParams) => muteInstance(this.context, options);
Expand Down Expand Up @@ -214,17 +207,4 @@ export class RulesClient {
public getTags = (params: RuleTagsParams) => getRuleTags(this.context, params);

public getScheduleFrequency = () => getScheduleFrequency(this.context);

public getAlertFromRaw = (params: GetAlertFromRawParams) =>
getAlertFromRaw(
this.context,
params.id,
params.ruleTypeId,
params.rawRule,
params.references,
params.includeLegacyId,
params.excludeFromPublicApi,
params.includeSnoozeData,
params.omitGeneratedValues
);
}
Loading

0 comments on commit 2367fa0

Please sign in to comment.