Skip to content

Commit

Permalink
[Alerting] Passing additional rule fields to rule executor (elastic#9…
Browse files Browse the repository at this point in the history
…9819)

* Passing additional rule fields to rule executor

* Fixing types check

* Api docs

* Adding enabled and actions

* Api docs

* Encapsulating rule information into RuleConfig type

* Functional tests

* Functional tests

* Adding producer, ruleTypeId and ruleTypeName

* Api docs

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ymao1 and kibanamachine committed May 24, 2021
1 parent f1aef4e commit dc867a9
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 52 deletions.
129 changes: 84 additions & 45 deletions api_docs/alerting.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions x-pack/plugins/alerting/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ export interface Alert<Params extends AlertTypeParams = never> {

export type SanitizedAlert<Params extends AlertTypeParams = never> = Omit<Alert<Params>, 'apiKey'>;

export type SanitizedRuleConfig = Pick<
SanitizedAlert,
| 'name'
| 'tags'
| 'consumer'
| 'enabled'
| 'schedule'
| 'actions'
| 'createdBy'
| 'updatedBy'
| 'createdAt'
| 'updatedAt'
| 'throttle'
| 'notifyWhen'
> & {
producer: string;
ruleTypeId: string;
ruleTypeName: string;
};

export enum HealthStatus {
OK = 'ok',
Warning = 'warn',
Expand Down
46 changes: 44 additions & 2 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ describe('Task Runner', () => {
kibanaBaseUrl: 'https://localhost:5601',
};

const mockDate = new Date('2019-02-12T21:01:22.479Z');

const mockedAlertTypeSavedObject: Alert<AlertTypeParams> = {
id: '1',
consumer: 'bar',
createdAt: new Date('2019-02-12T21:01:22.479Z'),
updatedAt: new Date('2019-02-12T21:01:22.479Z'),
createdAt: mockDate,
updatedAt: mockDate,
throttle: null,
muteAll: false,
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -155,6 +157,7 @@ describe('Task Runner', () => {
taskRunnerFactoryInitializerParams.actionsPlugin.renderActionParameterTemplates.mockImplementation(
(actionTypeId, actionId, params) => params
);
alertTypeRegistry.get.mockReturnValue(alertType);
});

test('successfully executes the task', async () => {
Expand Down Expand Up @@ -205,6 +208,45 @@ describe('Task Runner', () => {
expect(call.tags).toEqual(['alert-', '-tags']);
expect(call.createdBy).toBe('alert-creator');
expect(call.updatedBy).toBe('alert-updater');
expect(call.rule).not.toBe(null);
expect(call.rule.name).toBe('alert-name');
expect(call.rule.tags).toEqual(['alert-', '-tags']);
expect(call.rule.consumer).toBe('bar');
expect(call.rule.enabled).toBe(true);
expect(call.rule.schedule).toMatchInlineSnapshot(`
Object {
"interval": "10s",
}
`);
expect(call.rule.createdBy).toBe('alert-creator');
expect(call.rule.updatedBy).toBe('alert-updater');
expect(call.rule.createdAt).toBe(mockDate);
expect(call.rule.updatedAt).toBe(mockDate);
expect(call.rule.notifyWhen).toBe('onActiveAlert');
expect(call.rule.throttle).toBe(null);
expect(call.rule.producer).toBe('alerts');
expect(call.rule.ruleTypeId).toBe('test');
expect(call.rule.ruleTypeName).toBe('My test alert');
expect(call.rule.actions).toMatchInlineSnapshot(`
Array [
Object {
"actionTypeId": "action",
"group": "default",
"id": "1",
"params": Object {
"foo": true,
},
},
Object {
"actionTypeId": "action",
"group": "recovered",
"id": "2",
"params": Object {
"isResolved": true,
},
},
]
`);
expect(call.services.alertInstanceFactory).toBeTruthy();
expect(call.services.scopedClusterClient).toBeTruthy();
expect(call.services).toBeTruthy();
Expand Down
25 changes: 25 additions & 0 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ export class TaskRunner<
event: Event
): Promise<AlertTaskState> {
const {
alertTypeId,
consumer,
schedule,
throttle,
notifyWhen,
muteAll,
Expand All @@ -223,12 +226,17 @@ export class TaskRunner<
tags,
createdBy,
updatedBy,
createdAt,
updatedAt,
enabled,
actions,
} = alert;
const {
params: { alertId },
state: { alertInstances: alertRawInstances = {}, alertTypeState = {}, previousStartedAt },
} = this.taskInstance;
const namespace = this.context.spaceIdToNamespace(spaceId);
const alertType = this.alertTypeRegistry.get(alertTypeId);

const alertInstances = mapValues<
Record<string, RawAlertInstance>,
Expand Down Expand Up @@ -265,6 +273,23 @@ export class TaskRunner<
tags,
createdBy,
updatedBy,
rule: {
name,
tags,
consumer,
producer: alertType.producer,
ruleTypeId: alert.alertTypeId,
ruleTypeName: alertType.name,
enabled,
schedule,
actions,
createdBy,
updatedBy,
createdAt,
updatedAt,
throttle,
notifyWhen,
},
});
} catch (err) {
event.message = `alert execution failure: ${alertLabel}`;
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
AlertNotifyWhenType,
WithoutReservedActionGroups,
ActionVariable,
SanitizedRuleConfig,
} from '../common';
import { LicenseType } from '../../licensing/server';

Expand Down Expand Up @@ -89,6 +90,7 @@ export interface AlertExecutorOptions<
services: AlertServices<InstanceState, InstanceContext, ActionGroupIds>;
params: Params;
state: State;
rule: SanitizedRuleConfig;
spaceId: string;
namespace?: string;
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ const mockOptions = {
tags: [],
createdBy: null,
updatedBy: null,
rule: {
name: '',
tags: [],
consumer: '',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: null,
updatedBy: null,
createdAt: new Date(),
updatedAt: new Date(),
throttle: null,
notifyWhen: null,
producer: '',
ruleTypeId: '',
ruleTypeName: '',
},
};

describe('The metric threshold alert type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ describe('rules_notification_alert_type', () => {
previousStartedAt: new Date('2019-12-13T16:40:33.400Z'),
createdBy: 'elastic',
updatedBy: 'elastic',
rule: {
name: 'name',
tags: [],
consumer: 'foo',
producer: 'foo',
ruleTypeId: 'ruleType',
ruleTypeName: 'Name of rule',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-14T16:40:33.400Z'),
updatedAt: new Date('2019-12-14T16:40:33.400Z'),
throttle: null,
notifyWhen: null,
},
};

alert = rulesNotificationAlertType({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ const getPayload = (
previousStartedAt: new Date('2019-12-13T16:40:33.400Z'),
createdBy: 'elastic',
updatedBy: 'elastic',
rule: {
name: ruleAlert.name,
tags: ruleAlert.tags,
consumer: 'foo',
producer: 'foo',
ruleTypeId: 'ruleType',
ruleTypeName: 'Name of rule',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: 'elastic',
updatedBy: 'elastic',
createdAt: new Date('2019-12-13T16:50:33.400Z'),
updatedAt: new Date('2019-12-13T16:50:33.400Z'),
throttle: null,
notifyWhen: null,
},
});

describe('signal_rule_alert_type', () => {
Expand Down
Loading

0 comments on commit dc867a9

Please sign in to comment.