Skip to content

Commit

Permalink
[Alerting] formalize alert status and add status fields to alert save…
Browse files Browse the repository at this point in the history
…d object

resolves elastic#51099

This formalizes the concept of "alert status", in terms of it's execution, with
some new fields in the alert saved object and types used with the alert client
and http APIs.

These fields are read-only from the client point-of-view; they are provided in
the alert structures, but are only updated by the alerting framework itself.
The values will be updated after each run of the alert type executor.

interim commits:

calculate the execution status, some refactoring
write the execution status to the alert after execution
use real date in execution status on create
add an await to an async fn
comment out status update to see if SIEM FT succeeds
fix SIEM FT alert deletion issue
use partial updates and retries in alerts clients to avoid conflicts
fix jest tests
clean up conflict-fixin code
moar conflict-prevention fixing
fix type error with find result
add reasons to alert execution errors
add some jest tests
add some function tests
fix status update to use alert namespace
fix function test
  • Loading branch information
pmuellr committed Sep 4, 2020
1 parent fef21ab commit dfba7d5
Show file tree
Hide file tree
Showing 45 changed files with 1,014 additions and 110 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/alerts/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export interface IntervalSchedule extends SavedObjectAttributes {
interval: string;
}

export type AlertExecutionStatuses = 'ok' | 'active' | 'error' | 'noData' | 'unknown';
export type AlertExecutionStatusErrorReasons = 'read' | 'decrypt' | 'execute' | 'unknown';

export interface AlertExecutionStatus {
status: AlertExecutionStatuses;
date: Date;
error?: {
reason: AlertExecutionStatusErrorReasons;
message: string;
};
}

export type AlertActionParams = SavedObjectAttributes;

export interface AlertAction {
Expand Down Expand Up @@ -44,6 +56,7 @@ export interface Alert {
throttle: string | null;
muteAll: boolean;
mutedInstanceIds: string[];
executionStatus: AlertExecutionStatus;
}

export type SanitizedAlert = Omit<Alert, 'apiKey'>;
78 changes: 60 additions & 18 deletions x-pack/plugins/alerts/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ describe('create()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -372,6 +376,11 @@ describe('create()', () => {
"createdAt": "2019-02-12T21:01:22.479Z",
"createdBy": "elastic",
"enabled": true,
"executionStatus": Object {
"date": "2019-02-12T21:01:22.479Z",
"error": null,
"status": "unknown",
},
"muteAll": false,
"mutedInstanceIds": Array [],
"name": "abc",
Expand Down Expand Up @@ -564,6 +573,10 @@ describe('create()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -638,6 +651,10 @@ describe('create()', () => {
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"enabled": false,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -1002,6 +1019,11 @@ describe('create()', () => {
muteAll: false,
mutedInstanceIds: [],
tags: ['foo'],
executionStatus: {
date: '2019-02-12T21:01:22.479Z',
status: 'unknown',
error: null,
},
},
{
references: [
Expand Down Expand Up @@ -1115,6 +1137,11 @@ describe('create()', () => {
muteAll: false,
mutedInstanceIds: [],
tags: ['foo'],
executionStatus: {
date: '2019-02-12T21:01:22.479Z',
status: 'unknown',
error: null,
},
},
{
references: [
Expand Down Expand Up @@ -1797,15 +1824,10 @@ describe('muteInstance()', () => {
});

await alertsClient.muteInstance({ alertId: '1', alertInstanceId: '2' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
mutedInstanceIds: ['2'],
updatedBy: 'elastic',
},
{ version: '123' }
);
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
mutedInstanceIds: ['2'],
updatedBy: 'elastic',
});
});

test('skips muting when alert instance already muted', async () => {
Expand Down Expand Up @@ -1930,15 +1952,10 @@ describe('unmuteInstance()', () => {
});

await alertsClient.unmuteInstance({ alertId: '1', alertInstanceId: '2' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
mutedInstanceIds: [],
updatedBy: 'elastic',
},
{ version: '123' }
);
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
mutedInstanceIds: [],
updatedBy: 'elastic',
});
});

test('skips unmuting when alert instance not muted', async () => {
Expand Down Expand Up @@ -2089,6 +2106,10 @@ describe('get()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -2411,6 +2432,11 @@ const BaseAlertStatusSavedObject: SavedObject<RawAlert> = {
throttle: null,
muteAll: false,
mutedInstanceIds: [],
executionStatus: {
status: 'unknown',
date: '2020-08-20T19:23:38Z',
error: null,
},
},
references: [],
};
Expand Down Expand Up @@ -2682,6 +2708,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -3127,6 +3157,10 @@ describe('update()', () => {
],
"createdAt": 2019-02-12T21:01:22.479Z,
"enabled": true,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -3304,6 +3338,10 @@ describe('update()', () => {
"apiKey": "MTIzOmFiYw==",
"createdAt": 2019-02-12T21:01:22.479Z,
"enabled": true,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down Expand Up @@ -3455,6 +3493,10 @@ describe('update()', () => {
"apiKey": null,
"createdAt": 2019-02-12T21:01:22.479Z,
"enabled": false,
"executionStatus": Object {
"date": 2019-02-12T21:01:22.479Z,
"status": "unknown",
},
"id": "1",
"params": Object {
"bar": true,
Expand Down
Loading

0 comments on commit dfba7d5

Please sign in to comment.