Skip to content

Commit

Permalink
Convert event log's duration from number to string in Kibana (keep as…
Browse files Browse the repository at this point in the history
… "long" in Elasticsearch) (#130819)

* Convert event.duration to string in TypeScript, keep as long in Elasticsearch

* Fix jest test

* Fix functional tests

* Add ecsStringOrNumber to event log schema

* Fix jest test

* Add utility functions to event log plugin

* Use new event log utility functions

* PR fixes

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mikecote and kibanamachine authored May 3, 2022
1 parent 06b958d commit d3ac9aa
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { random, mean } from 'lodash';
import { SanitizedRule, AlertSummary } from '../types';
import { IValidatedEvent } from '@kbn/event-log-plugin/server';
import { IValidatedEvent, millisToNanos, nanosToMillis } from '@kbn/event-log-plugin/server';
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';
import { alertSummaryFromEventLog } from './alert_summary_from_event_log';

Expand Down Expand Up @@ -643,7 +643,7 @@ export class EventsFactory {
event: {
provider: EVENT_LOG_PROVIDER,
action: EVENT_LOG_ACTIONS.execute,
duration: random(2000, 180000) * 1000 * 1000,
duration: millisToNanos(random(2000, 180000)),
},
};

Expand Down Expand Up @@ -710,7 +710,7 @@ export class EventsFactory {
return this.events
.filter((ev) => ev?.event?.action === 'execute' && ev?.event?.duration !== undefined)
.reduce((res: Record<string, number>, ev) => {
res[ev?.['@timestamp']!] = ev?.event?.duration! / (1000 * 1000);
res[ev?.['@timestamp']!] = nanosToMillis(ev?.event?.duration!);
return res;
}, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
*/

import { mean } from 'lodash';
import { IEvent } from '@kbn/event-log-plugin/server';
import { IEvent, nanosToMillis } from '@kbn/event-log-plugin/server';
import { SanitizedRule, AlertSummary, AlertStatus } from '../types';
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';

const Millis2Nanos = 1000 * 1000;

export interface AlertSummaryFromEventLogParams {
rule: SanitizedRule<{ bar: boolean }>;
events: IEvent[];
Expand Down Expand Up @@ -111,7 +109,7 @@ export function alertSummaryFromEventLog(params: AlertSummaryFromEventLogParams)
}

if (event?.event?.duration) {
const eventDirationMillis = event.event.duration / Millis2Nanos;
const eventDirationMillis = nanosToMillis(event.event.duration);
eventDurations.push(eventDirationMillis);
eventDurationsWithTimestamp[event['@timestamp']!] = eventDirationMillis;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function createAlertEventLogRecordObject(params: CreateAlertEventLogRecor
category: [ruleType.producer],
...(state?.start ? { start: state.start as string } : {}),
...(state?.end ? { end: state.end as string } : {}),
...(state?.duration !== undefined ? { duration: state.duration as number } : {}),
...(state?.duration !== undefined ? { duration: state.duration as string } : {}),
},
kibana: {
alert: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ const findResults = {
kind: 'action',
start: '2022-03-23T17:37:07.105Z',
end: '2022-03-23T17:37:07.105Z',
duration: 0,
duration: '0',
outcome: 'failure',
},
kibana: {
Expand Down Expand Up @@ -345,7 +345,7 @@ const findResults = {
kind: 'action',
start: '2022-03-23T17:37:07.101Z',
end: '2022-03-23T17:37:07.102Z',
duration: 1000000,
duration: '1000000',
outcome: 'failure',
},
kibana: {
Expand Down Expand Up @@ -394,7 +394,7 @@ const findResults = {
kind: 'action',
start: '2022-03-23T17:37:07.098Z',
end: '2022-03-23T17:37:07.098Z',
duration: 0,
duration: '0',
outcome: 'failure',
},
kibana: {
Expand Down Expand Up @@ -443,7 +443,7 @@ const findResults = {
kind: 'action',
start: '2022-03-23T17:37:07.095Z',
end: '2022-03-23T17:37:07.096Z',
duration: 1000000,
duration: '1000000',
outcome: 'failure',
},
kibana: {
Expand Down Expand Up @@ -492,7 +492,7 @@ const findResults = {
kind: 'action',
start: '2022-03-23T17:37:07.084Z',
end: '2022-03-23T17:37:07.086Z',
duration: 2000000,
duration: '2000000',
outcome: 'failure',
},
kibana: {
Expand Down Expand Up @@ -543,7 +543,7 @@ const findResults = {
start: '2022-03-23T17:23:05.131Z',
outcome: 'failure',
end: '2022-03-23T17:23:05.248Z',
duration: 117000000,
duration: '117000000',
reason: 'execute',
},
kibana: {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/task_runner/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const DATE_1969 = '1969-12-31T00:00:00.000Z';
export const DATE_1970 = '1970-01-01T00:00:00.000Z';
export const DATE_1970_5_MIN = '1969-12-31T23:55:00.000Z';
export const DATE_9999 = '9999-12-31T12:34:56.789Z';
export const MOCK_DURATION = 86400000000000;
export const MOCK_DURATION = '86400000000000';

export const SAVED_OBJECT = {
id: '1',
Expand Down
54 changes: 27 additions & 27 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
2,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionSubgroup: 'subDefault',
Expand All @@ -356,7 +356,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
3,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand Down Expand Up @@ -459,7 +459,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
2,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionGroupId: 'default',
Expand All @@ -470,7 +470,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
3,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand Down Expand Up @@ -1025,7 +1025,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
2,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionGroupId: 'default',
Expand All @@ -1036,7 +1036,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
3,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand Down Expand Up @@ -1109,15 +1109,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: DATE_1969,
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: {},
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -1169,7 +1169,7 @@ describe('Task Runner', () => {
2,
generateEventLog({
action: EVENT_LOG_ACTIONS.recoveredInstance,
duration: 64800000000000,
duration: '64800000000000',
instanceId: '2',
start: '1969-12-31T06:00:00.000Z',
end: DATE_1970,
Expand Down Expand Up @@ -1419,15 +1419,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: DATE_1969,
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: { lastScheduledActions: { group: 'default', date } },
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -1459,7 +1459,7 @@ describe('Task Runner', () => {
generateEventLog({
action: EVENT_LOG_ACTIONS.recoveredInstance,
actionGroupId: 'default',
duration: 64800000000000,
duration: '64800000000000',
instanceId: '2',
start: '1969-12-31T06:00:00.000Z',
end: DATE_1970,
Expand Down Expand Up @@ -2045,7 +2045,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
2,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionGroupId: 'default',
Expand All @@ -2056,7 +2056,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
3,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionGroupId: 'default',
Expand All @@ -2067,7 +2067,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
4,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand All @@ -2078,7 +2078,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
5,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand Down Expand Up @@ -2132,15 +2132,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: DATE_1969,
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: {},
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -2185,7 +2185,7 @@ describe('Task Runner', () => {
generateEventLog({
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
duration: 64800000000000,
duration: '64800000000000',
start: '1969-12-31T06:00:00.000Z',
instanceId: '2',
consumer: 'bar',
Expand Down Expand Up @@ -2315,15 +2315,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: DATE_1969,
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: {},
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -2366,7 +2366,7 @@ describe('Task Runner', () => {
3,
generateEventLog({
action: EVENT_LOG_ACTIONS.recoveredInstance,
duration: 64800000000000,
duration: '64800000000000',
start: '1969-12-31T06:00:00.000Z',
end: DATE_1970,
consumer: 'bar',
Expand Down Expand Up @@ -2789,7 +2789,7 @@ describe('Task Runner', () => {
},
},
state: {
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
},
Expand Down Expand Up @@ -2819,7 +2819,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
2,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.newInstance,
actionGroupId: 'default',
Expand All @@ -2830,7 +2830,7 @@ describe('Task Runner', () => {
expect(eventLogger.logEvent).toHaveBeenNthCalledWith(
3,
generateEventLog({
duration: 0,
duration: '0',
start: DATE_1970,
action: EVENT_LOG_ACTIONS.activeInstance,
actionGroupId: 'default',
Expand Down Expand Up @@ -2989,7 +2989,7 @@ describe('Task Runner', () => {
},
},
state: {
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
},
Expand All @@ -3001,7 +3001,7 @@ describe('Task Runner', () => {
},
},
state: {
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
},
Expand Down
Loading

0 comments on commit d3ac9aa

Please sign in to comment.