Skip to content

Commit

Permalink
Correctly pass timestamp from the core to the legacy Kibana. Do not…
Browse files Browse the repository at this point in the history
… try to stop legacy Hapi server if it does not exist. (#23436)
  • Loading branch information
azasypkin authored Sep 25, 2018
1 parent 9b6d0b1 commit 3c6b382
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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,
}
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export default class KbnServer {
}

async close() {
if (!this.server) {
return;
}

await fromNode(cb => this.server.stop(cb));
}

Expand Down

0 comments on commit 3c6b382

Please sign in to comment.