Skip to content

Commit

Permalink
[Reporting] Switch from EventLog integration to ECS logging (#124762)
Browse files Browse the repository at this point in the history
* [Reporting] Remove EventLog Dependency

* calculate duration

* use LogMeta interface of core logger

* fix ts

* rename the debug log tag

* clean up return types for testing

* remove reporting fields from the event log mappings

* unwrap code from iife

* add class for log adapter

* remove useless factory fn

* remove eventLog

* user field was meant to be ECS field

* duration is nanoseconds

* fix nanoseconds application and test

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
tsullivan and kibanamachine authored Feb 10, 2022
1 parent 560ca27 commit 87eaa75
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 299 deletions.
13 changes: 0 additions & 13 deletions x-pack/plugins/event_log/generated/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,6 @@
}
}
},
"reporting": {
"properties": {
"id": {
"type": "keyword"
},
"jobType": {
"type": "keyword"
},
"byteSize": {
"type": "long"
}
}
},
"saved_objects": {
"type": "nested",
"properties": {
Expand Down
7 changes: 0 additions & 7 deletions x-pack/plugins/event_log/generated/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ export const EventSchema = schema.maybe(
),
})
),
reporting: schema.maybe(
schema.object({
id: ecsString(),
jobType: ecsString(),
byteSize: ecsNumber(),
})
),
saved_objects: schema.maybe(
schema.arrayOf(
schema.object({
Expand Down
14 changes: 0 additions & 14 deletions x-pack/plugins/event_log/scripts/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,6 @@ exports.EcsCustomPropertyMappings = {
},
},
},
// reporting specific fields
reporting: {
properties: {
id: {
type: 'keyword',
},
jobType: {
type: 'keyword',
},
byteSize: {
type: 'long',
},
},
},
// array of saved object references, for "linking" via search
saved_objects: {
type: 'nested',
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/reporting/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"licensing",
"uiActions",
"taskManager",
"eventLog",
"embeddable",
"screenshotting",
"screenshotMode",
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
import type { PluginStart as DataPluginStart } from 'src/plugins/data/server';
import type { FieldFormatsStart } from 'src/plugins/field_formats/server';
import { KibanaRequest, ServiceStatusLevels } from '../../../../src/core/server';
import type { IEventLogService } from '../../event_log/server';
import type { PluginSetupContract as FeaturesPluginSetup } from '../../features/server';
import type { LicensingPluginStart } from '../../licensing/server';
import type { ScreenshotResult, ScreenshottingStart } from '../../screenshotting/server';
Expand All @@ -40,7 +39,6 @@ import { ExecuteReportTask, MonitorReportsTask, ReportTaskParams } from './lib/t
import type { ReportingPluginRouter, ScreenshotOptions } from './types';

export interface ReportingInternalSetup {
eventLog: IEventLogService;
basePath: Pick<BasePath, 'set'>;
router: ReportingPluginRouter;
features: FeaturesPluginSetup;
Expand Down Expand Up @@ -390,7 +388,7 @@ export class ReportingCore {
}

public getEventLogger(report: IReport, task?: { id: string }) {
const ReportingEventLogger = reportingEventLoggerFactory(this.pluginSetupDeps!.eventLog);
const ReportingEventLogger = reportingEventLoggerFactory(this.logger);
return new ReportingEventLogger(report, task);
}
}
67 changes: 67 additions & 0 deletions x-pack/plugins/reporting/server/lib/event_logger/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LogMeta } from 'kibana/server';
import { createMockLevelLogger } from '../../test_helpers';
import { EcsLogAdapter } from './adapter';

describe('EcsLogAdapter', () => {
const logger = createMockLevelLogger();
beforeAll(() => {
jest
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => new Date('2021-04-12T16:00:00.000Z').valueOf())
.mockImplementationOnce(() => new Date('2021-04-12T16:02:00.000Z').valueOf());
});

beforeEach(() => {
jest.clearAllMocks();
});

it('captures a log event', () => {
const eventLogger = new EcsLogAdapter(logger, { event: { provider: 'test-adapting' } });

const event = { kibana: { reporting: { wins: 5000 } } } as object & LogMeta; // an object that extends LogMeta
eventLogger.logEvent('hello world', event);

expect(logger.debug).toBeCalledWith('hello world', ['events'], {
event: {
duration: undefined,
end: undefined,
provider: 'test-adapting',
start: undefined,
},
kibana: {
reporting: {
wins: 5000,
},
},
});
});

it('captures timings between start and complete', () => {
const eventLogger = new EcsLogAdapter(logger, { event: { provider: 'test-adapting' } });
eventLogger.startTiming();

const event = { kibana: { reporting: { wins: 9000 } } } as object & LogMeta; // an object that extends LogMeta
eventLogger.logEvent('hello duration', event);

expect(logger.debug).toBeCalledWith('hello duration', ['events'], {
event: {
duration: 120000000000,
end: '2021-04-12T16:02:00.000Z',
provider: 'test-adapting',
start: '2021-04-12T16:00:00.000Z',
},
kibana: {
reporting: {
wins: 9000,
},
},
});
});
});
57 changes: 57 additions & 0 deletions x-pack/plugins/reporting/server/lib/event_logger/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import deepMerge from 'deepmerge';
import { LogMeta } from 'src/core/server';
import { LevelLogger } from '../level_logger';
import { IReportingEventLogger } from './logger';

/** @internal */
export class EcsLogAdapter implements IReportingEventLogger {
start?: Date;
end?: Date;

/**
* This class provides a logging system to Reporting code, using a shape similar to the EventLog service.
* The logging action causes ECS data with Reporting metrics sent to DEBUG logs.
*
* @param {LevelLogger} logger - Reporting's wrapper of the core logger
* @param {Partial<LogMeta>} properties - initial ECS data with template for Reporting metrics
*/
constructor(private logger: LevelLogger, private properties: Partial<LogMeta>) {}

logEvent(message: string, properties: LogMeta) {
if (this.start && !this.end) {
this.end = new Date(Date.now());
}

let duration: number | undefined;
if (this.end && this.start) {
duration = (this.end.valueOf() - this.start.valueOf()) * 1000000; // nanoseconds
}

// add the derived properties for timing between "start" and "complete" logging calls
const newProperties: LogMeta = deepMerge(this.properties, {
event: {
duration,
start: this.start?.toISOString(),
end: this.end?.toISOString(),
},
});

// sends an ECS object with Reporting metrics to the DEBUG logs
this.logger.debug(message, ['events'], deepMerge(newProperties, properties));
}

startTiming() {
this.start = new Date(Date.now());
}

stopTiming() {
this.end = new Date(Date.now());
}
}
7 changes: 1 addition & 6 deletions x-pack/plugins/reporting/server/lib/event_logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
* 2.0.
*/

import { IEventLogService } from '../../../../event_log/server';
import { PLUGIN_ID } from '../../../common/constants';

export enum ActionType {
SCHEDULE_TASK = 'schedule-task',
CLAIM_TASK = 'claim-task',
Expand All @@ -16,7 +13,5 @@ export enum ActionType {
SAVE_REPORT = 'save-report',
RETRY = 'retry',
FAIL_REPORT = 'fail-report',
}
export function registerEventLogProviderActions(eventLog: IEventLogService) {
eventLog.registerProviderActions(PLUGIN_ID, Object.values(ActionType));
EXECUTE_ERROR = 'execute-error',
}
Loading

0 comments on commit 87eaa75

Please sign in to comment.