From 3c6b382b061b299f03d121a6f07c8a5265d6a7f5 Mon Sep 17 00:00:00 2001 From: Aleh Zasypkin Date: Tue, 25 Sep 2018 18:58:37 +0200 Subject: [PATCH] Correctly pass `timestamp` from the core to the legacy Kibana. Do not try to stop legacy Hapi server if it does not exist. (#23436) --- .../logging/legacy_logging_server.test.ts | 99 +++++++++++++++++++ .../logging/legacy_logging_server.ts | 2 +- src/server/kbn_server.js | 4 + 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/core/server/legacy_compat/logging/legacy_logging_server.test.ts diff --git a/src/core/server/legacy_compat/logging/legacy_logging_server.test.ts b/src/core/server/legacy_compat/logging/legacy_logging_server.test.ts new file mode 100644 index 00000000000000..ab74c250abb4dd --- /dev/null +++ b/src/core/server/legacy_compat/logging/legacy_logging_server.test.ts @@ -0,0 +1,99 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +jest.mock('../../../../server/config'); +jest.mock('../../../../server/logging'); + +import { LogLevel } from '../../logging/log_level'; +import { LegacyLoggingServer } from './legacy_logging_server'; + +test('correctly forwards log records.', () => { + const loggingServer = new LegacyLoggingServer({ events: {} }); + const onLogMock = jest.fn(); + loggingServer.on('log', onLogMock); + + const timestamp = 1554433221100; + const firstLogRecord = { + timestamp: new Date(timestamp), + level: LogLevel.Info, + context: 'some-context', + message: 'some-message', + }; + + const secondLogRecord = { + timestamp: new Date(timestamp), + level: LogLevel.Error, + context: 'some-context.sub-context', + message: 'some-message', + meta: { unknown: 2 }, + error: new Error('some-error'), + }; + + const thirdLogRecord = { + timestamp: new Date(timestamp), + level: LogLevel.Trace, + context: 'some-context.sub-context', + message: 'some-message', + meta: { tags: ['important', 'tags'] }, + }; + + loggingServer.log(firstLogRecord); + loggingServer.log(secondLogRecord); + loggingServer.log(thirdLogRecord); + + expect(onLogMock).toHaveBeenCalledTimes(3); + + const [[firstCall], [secondCall], [thirdCall]] = onLogMock.mock.calls; + expect(firstCall).toMatchInlineSnapshot(` +Object { + "data": "some-message", + "tags": Array [ + "info", + "some-context", + ], + "timestamp": 1554433221100, +} +`); + + expect(secondCall).toMatchInlineSnapshot(` +Object { + "data": [Error: some-error], + "tags": Array [ + "error", + "some-context", + "sub-context", + ], + "timestamp": 1554433221100, +} +`); + + expect(thirdCall).toMatchInlineSnapshot(` +Object { + "data": "some-message", + "tags": Array [ + "trace", + "some-context", + "sub-context", + "important", + "tags", + ], + "timestamp": 1554433221100, +} +`); +}); diff --git a/src/core/server/legacy_compat/logging/legacy_logging_server.ts b/src/core/server/legacy_compat/logging/legacy_logging_server.ts index 23906cc420abca..5a31fb366a5ed1 100644 --- a/src/core/server/legacy_compat/logging/legacy_logging_server.ts +++ b/src/core/server/legacy_compat/logging/legacy_logging_server.ts @@ -72,7 +72,7 @@ export class LegacyLoggingServer extends EventEmitter { this.emit('log', { data: error || message, tags: [level.id.toLowerCase(), ...context.split('.'), ...(meta.tags || [])], - timestamp: timestamp.getMilliseconds(), + timestamp: timestamp.getTime(), }); } diff --git a/src/server/kbn_server.js b/src/server/kbn_server.js index d5be21587f6619..d01e9c98f4011b 100644 --- a/src/server/kbn_server.js +++ b/src/server/kbn_server.js @@ -158,6 +158,10 @@ export default class KbnServer { } async close() { + if (!this.server) { + return; + } + await fromNode(cb => this.server.stop(cb)); }