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

Convert event log's duration from number to string in Kibana (keep as "long" in Elasticsearch) #130819

Merged
merged 13 commits into from
May 3, 2022
Merged
Original file line number Diff line number Diff line change
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: `${random(2000, 180000)}000000`,
},
};

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']!] = Number(BigInt(ev?.event?.duration!) / BigInt(1000 * 1000));
return res;
}, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IEvent } 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;
const Millis2Nanos = BigInt(1000 * 1000);

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

if (event?.event?.duration) {
const eventDirationMillis = event.event.duration / Millis2Nanos;
const eventDirationMillis = Number(BigInt(event.event.duration) / Millis2Nanos);
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
50 changes: 25 additions & 25 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
20 changes: 12 additions & 8 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,9 @@ export class TaskRunner<

// Copy duration into execution status if available
if (null != event.event?.duration) {
executionStatus.lastDuration = Math.round(event.event?.duration / Millis2Nanos);
executionStatus.lastDuration = Math.round(
Number(BigInt(event.event?.duration) / BigInt(Millis2Nanos))
);
monitoringHistory.duration = executionStatus.lastDuration;
}

Expand Down Expand Up @@ -1062,9 +1064,10 @@ function trackAlertDurations<
const state = originalAlertIds.includes(id)
? originalAlerts[id].getState()
: currentAlerts[id].getState();
const duration = state.start
? (new Date(currentTime).valueOf() - new Date(state.start as string).valueOf()) * 1000 * 1000 // nanoseconds
: undefined;
const durationInMs =
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
const durationInNanoStr = durationInMs !== 0 ? `${durationInMs}000000` : '0';
mikecote marked this conversation as resolved.
Show resolved Hide resolved
const duration = state.start ? durationInNanoStr : undefined;
currentAlerts[id].replaceState({
...state,
...(state.start ? { start: state.start } : {}),
Expand All @@ -1075,9 +1078,10 @@ function trackAlertDurations<
// Inject end time into alert state of recovered alerts
for (const id of recoveredAlertIds) {
const state = recoveredAlerts[id].getState();
const duration = state.start
? (new Date(currentTime).valueOf() - new Date(state.start as string).valueOf()) * 1000 * 1000 // nanoseconds
: undefined;
const durationInMs =
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
const durationInNanoStr = durationInMs !== 0 ? `${durationInMs}000000` : '0';
const duration = state.start ? durationInNanoStr : undefined;
recoveredAlerts[id].replaceState({
...state,
...(duration ? { duration } : {}),
Expand Down Expand Up @@ -1175,7 +1179,7 @@ function generateNewAndRecoveredAlertEvents<
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 @@ -783,7 +783,7 @@ describe('Task Runner Cancel', () => {
action: 'new-instance',
category: ['alerts'],
kind: 'alert',
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
kibana: {
Expand Down Expand Up @@ -825,7 +825,7 @@ describe('Task Runner Cancel', () => {
event: {
action: 'active-instance',
category: ['alerts'],
duration: 0,
duration: '0',
kind: 'alert',
start: '1970-01-01T00:00:00.000Z',
},
Expand Down
Loading