Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ResponseOps] move alert UUID generation from rule registry to the alerting framework #143489

Merged
merged 41 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
08868bd
[ResponseOps] move alert UUID generation from rule registry to alerti…
pmuellr Oct 18, 2022
20577b3
Merge branch 'main' into alerting/alert-uuid
kibanamachine Nov 10, 2022
7fa690b
add retries for a function test
pmuellr Nov 10, 2022
87cfa3b
merge main and fix _some_ stuff
pmuellr Jan 27, 2023
20d8462
Merge branch 'main' into alerting/alert-uuid
pmuellr Feb 13, 2023
df9ecbb
merge main and fix conflicts
pmuellr Feb 13, 2023
6a51e52
add back some doc I removed when fixing merge conflicts
pmuellr Feb 13, 2023
6332280
fix jest tests after merging main
pmuellr Feb 13, 2023
1fb883c
fix some FT, start on migrating UUID out of RR alerts into framework …
pmuellr Feb 14, 2023
d2e95d9
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Feb 14, 2023
98de392
add a zx sctipt to get task docs to write tests agains
pmuellr Feb 15, 2023
df620f4
finished jest tests for migration
pmuellr Feb 22, 2023
b9cf89d
get tests working again
pmuellr Feb 22, 2023
39639f3
fix file name issue
pmuellr Feb 22, 2023
a1a40dd
Merge branch 'main' into alerting/alert-uuid
kibanamachine Feb 23, 2023
16e92b3
Merge branch 'main' into alerting/alert-uuid
kibanamachine Feb 23, 2023
9eeed3c
remove rendundant data from test
pmuellr Feb 28, 2023
65c9b90
move tm-shared types to a new package
pmuellr Feb 28, 2023
4071334
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Feb 28, 2023
3abea57
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Feb 28, 2023
d271351
another attempt at building cross-package type package
pmuellr Mar 9, 2023
c835e90
fix jest config to point to right root, and fix license in package.json
pmuellr Mar 10, 2023
fb0a00f
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Mar 10, 2023
229a06b
merge main, fix conflicts
pmuellr Mar 10, 2023
433f0fe
Merge remote-tracking branch 'pmuellr/alerting/alert-uuid' into alert…
pmuellr Mar 10, 2023
841993a
fix tests failing after main merge
pmuellr Mar 10, 2023
63bb133
rearrange package re-export in hopes of fixing lint error
pmuellr Mar 10, 2023
8974bca
Merge branch 'main' into alerting/alert-uuid
kibanamachine Mar 20, 2023
bf4f2de
various cleanups; function tests next
pmuellr Mar 21, 2023
62cdfe9
add function tests
pmuellr Mar 21, 2023
ff1e99f
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Mar 21, 2023
f9227f9
fix UUID being removed when storing recovered alerts for flapping
pmuellr Mar 21, 2023
8fdecdf
Merge remote-tracking branch 'pmuellr/alerting/alert-uuid' into alert…
pmuellr Mar 21, 2023
bbbeb55
merge main, fix merge conflict in x-pack/test/tsconfig.json
pmuellr Mar 21, 2023
d28e83c
add additional function test
pmuellr Mar 22, 2023
ad4535f
Merge branch 'main' into alerting/alert-uuid
kibanamachine Mar 28, 2023
1ba4666
fix doc
pmuellr Mar 28, 2023
961651c
Merge branch 'main' into alerting/alert-uuid
kibanamachine Mar 28, 2023
68a79b4
merge main and fix simple merge conflict
pmuellr Mar 30, 2023
9c9ca7c
fix test broken after main merge, fix doc per PR comment
pmuellr Mar 30, 2023
b6bd1b3
Merge branch 'main' into alerting/alert-uuid
kibanamachine Apr 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ x-pack/test/alerting_api_integration/common/plugins/alerts @elastic/response-ops
x-pack/examples/alerting_example @elastic/response-ops
x-pack/test/functional_with_es_ssl/plugins/alerts @elastic/response-ops
x-pack/plugins/alerting @elastic/response-ops
x-pack/packages/kbn-alerting-state-types @elastic/response-ops
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new package added in this PR, to hold alerting types used by task manager. More below.

packages/kbn-alerts @elastic/security-solution
packages/kbn-alerts-as-data-utils @elastic/response-ops
x-pack/test/alerting_api_integration/common/plugins/alerts_restricted @elastic/response-ops
Expand Down
1 change: 1 addition & 0 deletions docs/user/alerting/action-variables.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ If the rule's action frequency is not a summary of alerts, it passes the followi
`alert.actionSubgroup`:: The action subgroup of the alert that scheduled the action.
`alert.flapping`:: A flag on the alert that indicates whether the alert status is changing repeatedly.
`alert.id`:: The ID of the alert that scheduled the action.
`alert.uuid`:: A universally unique identifier for the alert. While the alert is active, the UUID value remains unchanged each time the rule runs. preview:[]

[float]
[[defining-rules-actions-variable-context]]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"@kbn/alerting-example-plugin": "link:x-pack/examples/alerting_example",
"@kbn/alerting-fixture-plugin": "link:x-pack/test/functional_with_es_ssl/plugins/alerts",
"@kbn/alerting-plugin": "link:x-pack/plugins/alerting",
"@kbn/alerting-state-types": "link:x-pack/packages/kbn-alerting-state-types",
"@kbn/alerts": "link:packages/kbn-alerts",
"@kbn/alerts-as-data-utils": "link:packages/kbn-alerts-as-data-utils",
"@kbn/alerts-restricted-fixtures-plugin": "link:x-pack/test/alerting_api_integration/common/plugins/alerts_restricted",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"synthetics-param": "9776c9b571d35f0d0397e8915e035ea1dc026db7",
"synthetics-privates-locations": "7d032fc788905e32152029ae7ab3d6038c48ae44",
"tag": "87f21f07df9cc37001b15a26e413c18f50d1fbfe",
"task": "ebcc113df12f14bf627dbd335ba78507187b48a3",
"task": "ff760534a44c4cfabcf4baf8cfe8283f717cab02",
"telemetry": "561b329aaed3c15b91aaf2075645be3097247612",
"ui-metric": "410a8ad28e0f44b161c960ff0ce950c712b17c52",
"upgrade-assistant-ml-upgrade-operation": "d8816e5ce32649e7a3a43e2c406c632319ff84bb",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"@kbn/alerting-fixture-plugin/*": ["x-pack/test/functional_with_es_ssl/plugins/alerts/*"],
"@kbn/alerting-plugin": ["x-pack/plugins/alerting"],
"@kbn/alerting-plugin/*": ["x-pack/plugins/alerting/*"],
"@kbn/alerting-state-types": ["x-pack/packages/kbn-alerting-state-types"],
"@kbn/alerting-state-types/*": ["x-pack/packages/kbn-alerting-state-types/*"],
"@kbn/alerts": ["packages/kbn-alerts"],
"@kbn/alerts/*": ["packages/kbn-alerts/*"],
"@kbn/alerts-as-data-utils": ["packages/kbn-alerts-as-data-utils"],
Expand Down
8 changes: 8 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @kbn/alerting-state-types
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package was added so that we can use the same types used in alerting and rule_registry, for rule task state, within task manager, for migration reasons. Alerting and rule_registry now import this package to get those types, and also task_manager.

No changes were made to the types. The files are not arranged in a super-logical fashion - I left them pretty much in the same files as where they originally came from, to make it easier to diff.


Contains type information for the alerting data persisted in task
manager documents as state.

Because task manager migrations sometimes need this data, it needs
to be in a package outside of alerting.

24 changes: 24 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

export type {
ThrottledActions,
LastScheduledActions,
AlertInstanceMeta,
AlertInstanceState,
AlertInstanceContext,
RawAlertInstance,
} from './src/alert_instance';
export { rawAlertInstance } from './src/alert_instance';

export { DateFromString } from './src/date_from_string';

export type { TrackedLifecycleAlertState, WrappedLifecycleRuleState } from './src/lifecycle_state';
export { wrappedStateRt } from './src/lifecycle_state';

export type { RuleTaskState, RuleTaskParams } from './src/rule_task_instance';
export { ActionsCompletion, ruleStateSchema, ruleParamsSchema } from './src/rule_task_instance';
12 changes: 12 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../..',
roots: ['<rootDir>/x-pack/packages/kbn-alerting-state-types'],
};
5 changes: 5 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/alerting-state-types",
"owner": "@elastic/response-ops"
}
6 changes: 6 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/alerting-state-types",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const metaSchema = t.partial({
// flapping flag that indicates whether the alert is flapping
flapping: t.boolean,
pendingRecoveredCount: t.number,
uuid: t.string,
});
export type AlertInstanceMeta = t.TypeOf<typeof metaSchema>;

Expand Down
48 changes: 48 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/src/lifecycle_state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 * as t from 'io-ts';

const trackedAlertStateRt = t.type({
alertId: t.string,
alertUuid: t.string,
started: t.string,
// an array used to track changes in alert state, the order is based on the rule executions
// true - alert has changed from active/recovered
// false - alert is new or the status has remained either active or recovered
flappingHistory: t.array(t.boolean),
// flapping flag that indicates whether the alert is flapping
flapping: t.boolean,
pendingRecoveredCount: t.number,
});

export type TrackedLifecycleAlertState = t.TypeOf<typeof trackedAlertStateRt>;

type RuleTypeState = Record<string, unknown>;

export const alertTypeStateRt = <State extends RuleTypeState>() =>
t.record(t.string, t.unknown) as t.Type<State, State, unknown>;

export const wrappedStateRt = <State extends RuleTypeState>() =>
t.type({
wrapped: alertTypeStateRt<State>(),
// tracks the active alerts
trackedAlerts: t.record(t.string, trackedAlertStateRt),
// tracks the recovered alerts
trackedAlertsRecovered: t.record(t.string, trackedAlertStateRt),
});

/**
* This is redefined instead of derived from above `wrappedStateRt` because
* there's no easy way to instantiate generic values such as the runtime type
* factory function.
*/
export type WrappedLifecycleRuleState<State extends RuleTypeState> = RuleTypeState & {
wrapped: State;
trackedAlerts: Record<string, TrackedLifecycleAlertState>;
trackedAlertsRecovered: Record<string, TrackedLifecycleAlertState>;
};
17 changes: 17 additions & 0 deletions x-pack/packages/kbn-alerting-state-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts",
],
"exclude": [
"target/**/*"
],
"kbn_references": []
}
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ This factory returns an instance of `Alert`. The `Alert` class has the following

|Method|Description|
|---|---|
|getUuid()|Get the UUID of the alert.|
pmuellr marked this conversation as resolved.
Show resolved Hide resolved
|getState()|Get the current state of the alert.|
|scheduleActions(actionGroup, context)|Call this to schedule the execution of actions. The actionGroup is a string `id` that relates to the group of alert `actions` to execute and the context will be used for templating purposes. `scheduleActions` should only be called once per alert.|
|replaceState(state)|Used to replace the current state of the alert. This doesn't work like React, the entire state must be provided. Use this feature as you see fit. The state that is set will persist between rule executions whenever you re-create an alert with the same id. The alert state will be erased when `scheduleActions`isn't called during an execution.|
Expand All @@ -790,6 +791,8 @@ When an alert executes, the first argument is the `group` of actions to execute

The templating engine is [mustache]. General definition for the [mustache variable] is a double-brace {{}}. All variables are HTML-escaped by default and if there is a requirement to render unescaped HTML, it should be applied with the triple mustache: `{{{name}}}`. Also, `&` can be used to unescape a variable.

The complete list of variables available has grown, and difficult to keep in synch here as well, so refer to the published documentation for the variables available: https://www.elastic.co/guide/en/kibana/master/rule-action-variables.html

### Examples

The following code would be within a rule type. As you can see `cpuUsage` will replace the state of the alert and `server` is the context for the alert to execute. The difference between the two is that `cpuUsage` will be accessible at the next execution.
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/alert_summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface AlertSummary {
}

export interface AlertStatus {
uuid?: string;
pmuellr marked this conversation as resolved.
Show resolved Hide resolved
status: AlertStatusValues;
muted: boolean;
actionGroupId?: string;
Expand Down
22 changes: 20 additions & 2 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ import { AlertsHealth } from './rule';
export * from './rule';
export * from './rules_settings';
export * from './rule_type';
export * from './rule_task_instance';
export * from './alert_instance';
export type {
ThrottledActions,
LastScheduledActions,
AlertInstanceMeta,
AlertInstanceState,
AlertInstanceContext,
RawAlertInstance,
TrackedLifecycleAlertState,
WrappedLifecycleRuleState,
RuleTaskState,
RuleTaskParams,
} from '@kbn/alerting-state-types';
export {
rawAlertInstance,
DateFromString,
wrappedStateRt,
ActionsCompletion,
ruleStateSchema,
ruleParamsSchema,
} from '@kbn/alerting-state-types';
export * from './alert_summary';
export * from './builtin_action_groups';
export * from './bulk_edit';
Expand Down
47 changes: 42 additions & 5 deletions x-pack/plugins/alerting/server/alert/alert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sinon from 'sinon';
import { Alert } from './alert';
import { AlertInstanceState, AlertInstanceContext, DefaultActionGroupId } from '../../common';
import { alertWithAnyUUID } from '../test_utils';

let clock: sinon.SinonFakeTimers;

Expand Down Expand Up @@ -231,6 +232,23 @@ describe('getState()', () => {
});
});

describe('getUUID()', () => {
test('returns a UUID for a new alert', () => {
const alert = new Alert<AlertInstanceState, AlertInstanceContext, DefaultActionGroupId>('1');
const uuid = alert.getUuid();
expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
});

test('returns same uuid from previous run of alert', () => {
const uuid = 'previous-uuid';
const meta = { uuid };
const alert = new Alert<AlertInstanceState, AlertInstanceContext, DefaultActionGroupId>('1', {
meta,
});
expect(alert.getUuid()).toEqual(uuid);
});
});

describe('scheduleActions()', () => {
test('makes hasScheduledActions() return true', () => {
const alert = new Alert<AlertInstanceState, AlertInstanceContext, DefaultActionGroupId>('1', {
Expand Down Expand Up @@ -320,6 +338,7 @@ describe('updateLastScheduledActions()', () => {
expect(alert.toJSON()).toEqual({
state: {},
meta: {
uuid: expect.any(String),
lastScheduledActions: {
date: new Date().toISOString(),
group: 'default',
Expand All @@ -338,6 +357,7 @@ describe('updateLastScheduledActions()', () => {
state: {},
meta: {
flappingHistory: [],
uuid: expect.any(String),
lastScheduledActions: {
date: new Date().toISOString(),
group: 'default',
Expand Down Expand Up @@ -367,6 +387,7 @@ describe('updateLastScheduledActions()', () => {
state: {},
meta: {
flappingHistory: [],
uuid: expect.any(String),
lastScheduledActions: {
date: new Date().toISOString(),
group: 'default',
Expand Down Expand Up @@ -468,9 +489,22 @@ describe('toJSON', () => {
},
}
);
expect(JSON.stringify(alertInstance)).toEqual(
'{"state":{"foo":true},"meta":{"lastScheduledActions":{"date":"1970-01-01T00:00:00.000Z","group":"default"},"flappingHistory":[false,true],"flapping":false,"pendingRecoveredCount":2}}'
);

expect(alertInstance).toMatchObject({
state: {
foo: true,
},
meta: {
lastScheduledActions: {
date: expect.any(Date),
group: 'default',
},
uuid: expect.any(String),
flappingHistory: [false, true],
flapping: false,
pendingRecoveredCount: 2,
},
});
});
});

Expand Down Expand Up @@ -514,6 +548,7 @@ describe('toRaw', () => {
meta: {
flappingHistory: [false, true, true],
flapping: false,
uuid: expect.any(String),
},
});
});
Expand All @@ -529,12 +564,13 @@ describe('setFlappingHistory', () => {
);
alertInstance.setFlappingHistory([false]);
expect(alertInstance.getFlappingHistory()).toEqual([false]);
expect(alertInstance.toRaw()).toMatchInlineSnapshot(`
expect(alertWithAnyUUID(alertInstance.toRaw())).toMatchInlineSnapshot(`
Object {
"meta": Object {
"flappingHistory": Array [
false,
],
"uuid": Any<String>,
},
"state": Object {},
}
Expand All @@ -561,11 +597,12 @@ describe('setFlapping', () => {
);
alertInstance.setFlapping(false);
expect(alertInstance.getFlapping()).toEqual(false);
expect(alertInstance.toRaw()).toMatchInlineSnapshot(`
expect(alertWithAnyUUID(alertInstance.toRaw())).toMatchInlineSnapshot(`
Object {
"meta": Object {
"flapping": false,
"flappingHistory": Array [],
"uuid": Any<String>,
},
"state": Object {},
}
Expand Down
Loading