Skip to content

Commit

Permalink
[RAM] Add window maintenance column to rule logs and rule tables (#15…
Browse files Browse the repository at this point in the history
…5333)

## Summary
Resolves: #153775

This PR adds the `maintenance_window_id` column to the following tables:

## O11Y/Security solutions Alerts table

![o11y-alerts-table](https://user-images.githubusercontent.com/74562234/233227165-03c105d9-3890-4462-91ec-cd7c6ad26d7b.png)

## Rule details alerts table

![rule_details_maintenance_window_ids](https://user-images.githubusercontent.com/74562234/233226920-6f903ddf-401f-49e7-bb9c-9a36334fc7ce.png)

## Rule run event log

![rule_event_log_maintenance_window](https://user-images.githubusercontent.com/74562234/233226784-c6e804e6-eabe-4500-b51a-aae7aafbcff1.png)

## To test:
1. Set `ENABLE_MAINTENANCE_WINDOWS` to true in
`x-pack/plugins/alerting/public/plugin.ts`
2. Create 1 or more active maintenance windows in stack management
3. Create a rule, trigger some alerts
4. Go to the rule details page alerts table, assert the `maintenance
window` column is there, and renders the maintenance window ids
5. Go to the rule details page event log table, assert the `maintenance
window` column can enabled, and renders the maintenance window ids
6. Create a O11Y rule, trigger some alerts
7. Go to the O11Y alerts table, assert that the `maintenance_window_id`
field can be enabled, and renders the maintenance window ids
### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Lisa Cawley <lcawley@elastic.co>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 21, 2023
1 parent 9b6c6bc commit 9bb127f
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 30 deletions.
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 @@ -38,4 +38,5 @@ export interface AlertStatus {
actionGroupId?: string;
activeStartDate?: string;
flapping: boolean;
maintenanceWindowIds?: string[];
}
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/execution_log_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface IExecutionLog {
rule_id: string;
space_ids: string[];
rule_name: string;
maintenance_window_ids: string[];
}

export interface IExecutionErrors {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export function alertSummaryFromEventLog(params: AlertSummaryFromEventLogParams)
status.flapping = true;
}

if (event?.kibana?.alert?.maintenance_window_ids?.length) {
status.maintenanceWindowIds = event.kibana.alert.maintenance_window_ids as string[];
}

switch (action) {
case EVENT_LOG_ACTIONS.newInstance:
status.activeStartDate = timeStamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const createAlertingEventLoggerMock = () => {
setRuleName: jest.fn(),
setExecutionSucceeded: jest.fn(),
setExecutionFailed: jest.fn(),
setMaintenanceWindowIds: jest.fn(),
logTimeout: jest.fn(),
logAlert: jest.fn(),
logAction: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,51 @@ describe('AlertingEventLogger', () => {
});
});

describe('setMaintenanceWindowIds()', () => {
test('should throw error if alertingEventLogger has not been initialized', () => {
expect(() =>
alertingEventLogger.setMaintenanceWindowIds([])
).toThrowErrorMatchingInlineSnapshot(`"AlertingEventLogger not initialized"`);
});

test('should throw error if event is null', () => {
alertingEventLogger.initialize(context);
expect(() =>
alertingEventLogger.setMaintenanceWindowIds([])
).toThrowErrorMatchingInlineSnapshot(`"AlertingEventLogger not initialized"`);
});

it('should update event maintenance window IDs correctly', () => {
alertingEventLogger.initialize(context);
alertingEventLogger.start();
alertingEventLogger.setMaintenanceWindowIds([]);

const event = initializeExecuteRecord(contextWithScheduleDelay);
expect(alertingEventLogger.getEvent()).toEqual({
...event,
kibana: {
...event.kibana,
alert: {
...event.kibana?.alert,
maintenance_window_ids: [],
},
},
});

alertingEventLogger.setMaintenanceWindowIds(['test-id-1', 'test-id-2']);
expect(alertingEventLogger.getEvent()).toEqual({
...event,
kibana: {
...event.kibana,
alert: {
...event.kibana?.alert,
maintenance_window_ids: ['test-id-1', 'test-id-2'],
},
},
});
});
});

describe('logTimeout()', () => {
test('should throw error if alertingEventLogger has not been initialized', () => {
expect(() => alertingEventLogger.logTimeout()).toThrowErrorMatchingInlineSnapshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export class AlertingEventLogger {
updateEvent(this.event, { message, outcome: 'success', alertingOutcome: 'success' });
}

public setMaintenanceWindowIds(maintenanceWindowIds: string[]) {
if (!this.isInitialized || !this.event) {
throw new Error('AlertingEventLogger not initialized');
}

updateEvent(this.event, { maintenanceWindowIds });
}

public setExecutionFailed(message: string, errorMessage: string) {
if (!this.isInitialized || !this.event) {
throw new Error('AlertingEventLogger not initialized');
Expand Down Expand Up @@ -351,11 +359,22 @@ interface UpdateEventOpts {
reason?: string;
metrics?: RuleRunMetrics;
timings?: TaskRunnerTimings;
maintenanceWindowIds?: string[];
}

export function updateEvent(event: IEvent, opts: UpdateEventOpts) {
const { message, outcome, error, ruleName, status, reason, metrics, timings, alertingOutcome } =
opts;
const {
message,
outcome,
error,
ruleName,
status,
reason,
metrics,
timings,
alertingOutcome,
maintenanceWindowIds,
} = opts;
if (!event) {
throw new Error('Cannot update event because it is not initialized.');
}
Expand Down Expand Up @@ -431,4 +450,10 @@ export function updateEvent(event: IEvent, opts: UpdateEventOpts) {
...timings,
};
}

if (maintenanceWindowIds) {
event.kibana = event.kibana || {};
event.kibana.alert = event.kibana.alert || {};
event.kibana.alert.maintenance_window_ids = maintenanceWindowIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('getExecutionLogAggregation', () => {
},
},
executionDuration: { max: { field: 'event.duration' } },
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
top_hits: {
size: 1,
_source: {
Expand All @@ -283,6 +283,7 @@ describe('getExecutionLogAggregation', () => {
'kibana.space_ids',
'rule.name',
'kibana.alerting.outcome',
'kibana.alert.maintenance_window_ids',
],
},
},
Expand Down Expand Up @@ -477,7 +478,7 @@ describe('getExecutionLogAggregation', () => {
},
},
executionDuration: { max: { field: 'event.duration' } },
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
top_hits: {
size: 1,
_source: {
Expand All @@ -490,6 +491,7 @@ describe('getExecutionLogAggregation', () => {
'kibana.space_ids',
'rule.name',
'kibana.alerting.outcome',
'kibana.alert.maintenance_window_ids',
],
},
},
Expand Down Expand Up @@ -684,7 +686,7 @@ describe('getExecutionLogAggregation', () => {
},
},
executionDuration: { max: { field: 'event.duration' } },
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
top_hits: {
size: 1,
_source: {
Expand All @@ -697,6 +699,7 @@ describe('getExecutionLogAggregation', () => {
'kibana.space_ids',
'rule.name',
'kibana.alerting.outcome',
'kibana.alert.maintenance_window_ids',
],
},
},
Expand Down Expand Up @@ -774,7 +777,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 0.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand Down Expand Up @@ -861,7 +864,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 5.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand All @@ -880,6 +883,9 @@ describe('formatExecutionLogResult', () => {
},
kibana: {
version: '8.2.0',
alert: {
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
alerting: {
outcome: 'success',
},
Expand Down Expand Up @@ -958,6 +964,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: [],
},
{
id: '41b2755e-765a-4044-9745-b03875d5e79a',
Expand All @@ -981,6 +988,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
],
});
Expand Down Expand Up @@ -1022,7 +1030,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 0.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand Down Expand Up @@ -1112,7 +1120,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 5.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand All @@ -1131,6 +1139,9 @@ describe('formatExecutionLogResult', () => {
},
kibana: {
version: '8.2.0',
alert: {
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
alerting: {
outcome: 'success',
},
Expand Down Expand Up @@ -1209,6 +1220,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: [],
},
{
id: '41b2755e-765a-4044-9745-b03875d5e79a',
Expand All @@ -1232,6 +1244,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
],
});
Expand Down Expand Up @@ -1273,7 +1286,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 0.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand Down Expand Up @@ -1355,7 +1368,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 5.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand All @@ -1374,6 +1387,9 @@ describe('formatExecutionLogResult', () => {
},
kibana: {
version: '8.2.0',
alert: {
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
alerting: {
outcome: 'success',
},
Expand Down Expand Up @@ -1452,6 +1468,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: [],
},
{
id: '41b2755e-765a-4044-9745-b03875d5e79a',
Expand All @@ -1475,6 +1492,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
],
});
Expand Down Expand Up @@ -1516,7 +1534,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 5.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand Down Expand Up @@ -1603,7 +1621,7 @@ describe('formatExecutionLogResult', () => {
numRecoveredAlerts: {
value: 5.0,
},
outcomeAndMessage: {
outcomeMessageAndMaintenanceWindow: {
hits: {
total: {
value: 1,
Expand All @@ -1622,6 +1640,9 @@ describe('formatExecutionLogResult', () => {
},
kibana: {
version: '8.2.0',
alert: {
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
alerting: {
outcome: 'success',
},
Expand Down Expand Up @@ -1700,6 +1721,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: [],
},
{
id: '61bb867b-661a-471f-bf92-23471afa10b3',
Expand All @@ -1723,6 +1745,7 @@ describe('formatExecutionLogResult', () => {
rule_id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
rule_name: 'rule_name',
space_ids: [],
maintenance_window_ids: ['254699b0-dfb2-11ed-bb3d-c91b918d0260'],
},
],
});
Expand Down
Loading

0 comments on commit 9bb127f

Please sign in to comment.