-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reporting] Switch from EventLog integration to ECS logging (#124762)
* [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
1 parent
560ca27
commit 87eaa75
Showing
19 changed files
with
220 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
x-pack/plugins/reporting/server/lib/event_logger/adapter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
x-pack/plugins/reporting/server/lib/event_logger/adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.