-
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.
Introduce
LegacyAppender
that forwards log records to the legacy pl…
…atform. (#14354)
- Loading branch information
Showing
25 changed files
with
393 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import { LegacyConfig } from './LegacyPlatformConfig'; | ||
import { LegacyPlatformProxifier } from './LegacyPlatformProxifier'; | ||
|
||
/** | ||
* Represents legacy kbnServer instance, provided by the legacy platform. | ||
* Represents a wrapper around legacy `kbnServer` instance that exposes only | ||
* a subset of `kbnServer` APIs used by the new platform. | ||
* @internal | ||
*/ | ||
export interface LegacyKbnServer { | ||
readonly config: LegacyConfig; | ||
export class LegacyKbnServer { | ||
constructor(private readonly rawKbnServer: any) {} | ||
|
||
/** | ||
* Custom HTTP Listener provided by the new platform and that will be used | ||
* within legacy platform by HapiJS server. | ||
* Custom HTTP Listener used by HapiJS server in the legacy platform. | ||
*/ | ||
newPlatformProxyListener: LegacyPlatformProxifier; | ||
get newPlatformProxyListener() { | ||
return this.rawKbnServer.newPlatform.proxyListener; | ||
} | ||
|
||
/** | ||
* Propagates legacy config updates to the new platform. | ||
* Forwards log request to the legacy platform. | ||
* @param tags A string or array of strings used to briefly identify the event. | ||
* @param [data] Optional string or object to log with the event. | ||
* @param [timestamp] Timestamp value associated with the log record. | ||
*/ | ||
updateNewPlatformConfig: (legacyConfig: LegacyConfig) => void; | ||
log(tags: string | string[], data?: string | Error, timestamp?: Date) { | ||
this.rawKbnServer.server.log(tags, data, timestamp); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { LegacyKbnServer } from '..'; | ||
|
||
test('correctly returns `newPlatformProxyListener`.', () => { | ||
const rawKbnServer = { | ||
newPlatform: { | ||
proxyListener: {} | ||
} | ||
}; | ||
|
||
const legacyKbnServer = new LegacyKbnServer(rawKbnServer); | ||
expect(legacyKbnServer.newPlatformProxyListener).toBe( | ||
rawKbnServer.newPlatform.proxyListener | ||
); | ||
}); | ||
|
||
test('correctly forwards log record.', () => { | ||
const rawKbnServer = { | ||
server: { log: jest.fn() } | ||
}; | ||
|
||
const legacyKbnServer = new LegacyKbnServer(rawKbnServer); | ||
|
||
const timestamp = new Date(Date.UTC(2012, 1, 1, 11, 22, 33, 44)); | ||
legacyKbnServer.log(['one', 'two'], 'message', timestamp); | ||
legacyKbnServer.log('three', new Error('log error'), timestamp); | ||
|
||
expect(rawKbnServer.server.log.mock.calls).toMatchSnapshot(); | ||
}); |
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
19 changes: 19 additions & 0 deletions
19
platform/legacy/__tests__/__snapshots__/LegacyKbnServer.test.ts.snap
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`correctly forwards log record. 1`] = ` | ||
Array [ | ||
Array [ | ||
Array [ | ||
"one", | ||
"two", | ||
], | ||
"message", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
"three", | ||
[Error: log error], | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
] | ||
`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Schema, typeOfSchema } from '../../../types/schema'; | ||
import { LogRecord } from '../../../logging/LogRecord'; | ||
import { DisposableAppender } from '../../../logging/appenders/Appenders'; | ||
import { LegacyKbnServer } from '../../LegacyKbnServer'; | ||
|
||
const createSchema = (schema: Schema) => { | ||
const { literal, object } = schema; | ||
|
||
return object({ kind: literal('legacy-appender') }); | ||
}; | ||
|
||
const schemaType = typeOfSchema(createSchema); | ||
/** @internal */ | ||
export type LegacyAppenderConfigType = typeof schemaType; | ||
|
||
/** | ||
* Simple appender that just forwards `LogRecord` to the legacy KbnServer log. | ||
* @internal | ||
*/ | ||
export class LegacyAppender implements DisposableAppender { | ||
static createConfigSchema = createSchema; | ||
|
||
constructor(private readonly kbnServer: LegacyKbnServer) {} | ||
|
||
/** | ||
* Forwards `LogRecord` to the legacy platform that will layout and | ||
* write record to the configured destination. | ||
* @param record `LogRecord` instance to forward to. | ||
*/ | ||
append(record: LogRecord) { | ||
this.kbnServer.log( | ||
[record.level.id.toLowerCase(), ...record.context.split('.')], | ||
record.error || record.message, | ||
record.timestamp | ||
); | ||
} | ||
|
||
async dispose() {} | ||
} |
Oops, something went wrong.