Skip to content

Commit

Permalink
[ResponseOps] Add kibana.alert.time_range field to Alert-As-Data mapp…
Browse files Browse the repository at this point in the history
…ings and populate it (#141309)

* [ResponseOps] Add kibana.alert.time_range field to Alert-As-Data mappings and populate it

* Fixing snapshots to match new reality

* Removing the lte (end of range) for active alerts.

* Fixing expected resutls for mapping test

* fixing tests

* updating readme

* Fixing field name in README

Co-authored-by: Faisal Kanout <[email protected]>
  • Loading branch information
simianhacker and fkanout authored Oct 4, 2022
1 parent 77eb802 commit 71aebb7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/kbn-rule-data-utils/src/technical_field_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const ALERT_REASON = `${ALERT_NAMESPACE}.reason` as const;
const ALERT_RISK_SCORE = `${ALERT_NAMESPACE}.risk_score` as const;
const ALERT_SEVERITY = `${ALERT_NAMESPACE}.severity` as const;
const ALERT_START = `${ALERT_NAMESPACE}.start` as const;
const ALERT_TIME_RANGE = `${ALERT_NAMESPACE}.time_range` as const;
const ALERT_STATUS = `${ALERT_NAMESPACE}.status` as const;
const ALERT_SYSTEM_STATUS = `${ALERT_NAMESPACE}.system_status` as const;
const ALERT_UUID = `${ALERT_NAMESPACE}.uuid` as const;
Expand Down Expand Up @@ -126,6 +127,7 @@ const fields = {
ALERT_RULE_UPDATED_BY,
ALERT_RULE_VERSION,
ALERT_START,
ALERT_TIME_RANGE,
ALERT_SEVERITY,
ALERT_STATUS,
ALERT_SYSTEM_STATUS,
Expand Down Expand Up @@ -183,6 +185,7 @@ export {
ALERT_RULE_VERSION,
ALERT_SEVERITY,
ALERT_START,
ALERT_TIME_RANGE,
ALERT_SYSTEM_STATUS,
ALERT_UUID,
ECS_VERSION,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/rule_registry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ The following fields are defined in the technical field component template and s
- `kibana.alert.ancestors`: the array of ancestors (if any) for the alert.
- `kibana.alert.depth`: the depth of the alert in the ancestral tree (default 0).
- `kibana.alert.building_block_type`: the building block type of the alert (default undefined).
- `kibana.alert.time_range`: the time range of an alert. (default undefined).

# Alerts as data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ it('matches snapshot', () => {
"required": false,
"type": "keyword",
},
"kibana.alert.time_range": Object {
"format": "epoch_millis||strict_date_optional_time",
"type": "date_range",
},
"kibana.alert.uuid": Object {
"required": true,
"type": "keyword",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const technicalRuleFieldMap = {
[Fields.ALERT_UUID]: { type: 'keyword', required: true },
[Fields.ALERT_INSTANCE_ID]: { type: 'keyword', required: true },
[Fields.ALERT_START]: { type: 'date' },
[Fields.ALERT_TIME_RANGE]: {
type: 'date_range',
format: 'epoch_millis||strict_date_optional_time',
},
[Fields.ALERT_END]: { type: 'date' },
[Fields.ALERT_DURATION]: { type: 'long' },
[Fields.ALERT_SEVERITY]: { type: 'keyword' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { ParsedExperimentalFields } from '../../common/parse_experimental_fields';
import { ParsedTechnicalFields } from '../../common/parse_technical_fields';
import {
ALERT_TIME_RANGE,
ALERT_DURATION,
ALERT_END,
ALERT_INSTANCE_ID,
Expand Down Expand Up @@ -235,7 +236,12 @@ export const createLifecycleExecutor =
...commonRuleFields,
...currentAlertData,
[ALERT_DURATION]: (options.startedAt.getTime() - new Date(started).getTime()) * 1000,

[ALERT_TIME_RANGE]: isRecovered
? {
gte: started,
lte: commonRuleFields[TIMESTAMP],
}
: { gte: started },
[ALERT_INSTANCE_ID]: alertId,
[ALERT_START]: started,
[ALERT_UUID]: alertUuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ALERT_STATUS_ACTIVE,
ALERT_STATUS_RECOVERED,
ALERT_UUID,
ALERT_TIME_RANGE,
} from '@kbn/rule-data-utils';
import { loggerMock } from '@kbn/logging-mocks';
import { castArray, omit } from 'lodash';
Expand Down Expand Up @@ -245,6 +246,9 @@ describe('createLifecycleRuleTypeFactory', () => {
"kibana.alert.rule.uuid": "alertId",
"kibana.alert.start": "2021-06-16T09:01:00.000Z",
"kibana.alert.status": "active",
"kibana.alert.time_range": Object {
"gte": "2021-06-16T09:01:00.000Z",
},
"kibana.alert.workflow_status": "open",
"kibana.space_ids": Array [
"spaceId",
Expand Down Expand Up @@ -273,6 +277,9 @@ describe('createLifecycleRuleTypeFactory', () => {
"kibana.alert.rule.uuid": "alertId",
"kibana.alert.start": "2021-06-16T09:01:00.000Z",
"kibana.alert.status": "active",
"kibana.alert.time_range": Object {
"gte": "2021-06-16T09:01:00.000Z",
},
"kibana.alert.workflow_status": "open",
"kibana.space_ids": Array [
"spaceId",
Expand Down Expand Up @@ -443,6 +450,10 @@ describe('createLifecycleRuleTypeFactory', () => {

expect(opbeansNodeAlertDoc['event.action']).toBe('close');
expect(opbeansNodeAlertDoc[ALERT_STATUS]).toBe(ALERT_STATUS_RECOVERED);
expect(opbeansNodeAlertDoc[ALERT_TIME_RANGE]).toEqual({
gte: '2021-06-16T09:01:00.000Z',
lte: '2021-06-16T09:02:00.000Z',
});
});
});
});
Expand Down

0 comments on commit 71aebb7

Please sign in to comment.