From dd0ce0f7f311add37968e48401c718924fd09988 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Thu, 17 Jan 2019 12:23:45 -0500 Subject: [PATCH] [Monitoring] Move os info into OSS collection (#28605) * Move os info into OSS collection so the stats api can access it * Fix tests * copy this back for now * Use server decorate to share from OSS to x-pack/plugins --- src/server/status/index.js | 4 ++++ .../server/status/lib}/get_os_info.js | 19 ++++++++++++++++--- .../server/status/lib}/get_os_info.test.js | 19 ++++++++++++++++--- src/server/status/lib/metrics.js | 8 +++++--- src/server/status/lib/metrics.test.js | 12 +++++++----- .../collectors/ops_buffer/ops_buffer.js | 3 +-- 6 files changed, 49 insertions(+), 16 deletions(-) rename {x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer => src/server/status/lib}/get_os_info.js (51%) rename {x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer => src/server/status/lib}/get_os_info.test.js (59%) diff --git a/src/server/status/index.js b/src/server/status/index.js index 02b0f4d3a8e1..db628cd12831 100644 --- a/src/server/status/index.js +++ b/src/server/status/index.js @@ -23,6 +23,7 @@ import { registerStatusPage, registerStatusApi, registerStatsApi } from './route import { getOpsStatsCollector } from './collectors'; import Oppsy from 'oppsy'; import { cloneDeep } from 'lodash'; +import { getOSInfo } from './lib/get_os_info'; export function statusMixin(kbnServer, server, config) { kbnServer.status = new ServerStatus(kbnServer.server); @@ -53,4 +54,7 @@ export function statusMixin(kbnServer, server, config) { registerStatusPage(kbnServer, server, config); registerStatusApi(kbnServer, server, config); registerStatsApi(kbnServer, server, config); + + // expore shared functionality + server.decorate('server', 'getOSInfo', getOSInfo); } diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.js b/src/server/status/lib/get_os_info.js similarity index 51% rename from x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.js rename to src/server/status/lib/get_os_info.js index b511cddafb97..88a74af4e980 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.js +++ b/src/server/status/lib/get_os_info.js @@ -1,7 +1,20 @@ /* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. + * 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. */ import os from 'os'; diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.test.js b/src/server/status/lib/get_os_info.test.js similarity index 59% rename from x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.test.js rename to src/server/status/lib/get_os_info.test.js index 4220ea85b7ce..ac97f3c46dcf 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/get_os_info.test.js +++ b/src/server/status/lib/get_os_info.test.js @@ -1,7 +1,20 @@ /* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. + * 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('os', () => ({ diff --git a/src/server/status/lib/metrics.js b/src/server/status/lib/metrics.js index dc5b99aefc7a..9b6647570e06 100644 --- a/src/server/status/lib/metrics.js +++ b/src/server/status/lib/metrics.js @@ -22,6 +22,7 @@ import v8 from 'v8'; import { get, isObject, merge } from 'lodash'; import { keysToSnakeCaseShallow } from '../../../utils/case_conversion'; import { getAllStats as cGroupStats } from './cgroup'; +import { getOSInfo } from './get_os_info'; const requestDefaults = { disconnects: 0, @@ -55,7 +56,7 @@ export class Metrics { async capture(hapiEvent) { const timestamp = new Date().toISOString(); - const event = this.captureEvent(hapiEvent); + const event = await this.captureEvent(hapiEvent); const cgroup = await this.captureCGroupsIfAvailable(); const metrics = { @@ -66,7 +67,7 @@ export class Metrics { return merge(metrics, event, cgroup); } - captureEvent(hapiEvent) { + async captureEvent(hapiEvent) { const heapStats = v8.getHeapStatistics(); const port = this.config.get('server.port'); const avgInMillis = get(hapiEvent, ['responseTimes', port, 'avg']); // sadly, it's possible for this to be NaN @@ -98,7 +99,8 @@ export class Metrics { free_in_bytes: os.freemem(), used_in_bytes: get(hapiEvent, 'osmem.total') - get(hapiEvent, 'osmem.free') }, - uptime_in_millis: os.uptime() * 1000 + uptime_in_millis: os.uptime() * 1000, + ...(await getOSInfo()) }, response_times: { avg_in_millis: isNaN(avgInMillis) ? undefined : avgInMillis, // convert NaN to undefined diff --git a/src/server/status/lib/metrics.test.js b/src/server/status/lib/metrics.test.js index d8b86164cbe0..d610490e2261 100644 --- a/src/server/status/lib/metrics.test.js +++ b/src/server/status/lib/metrics.test.js @@ -24,7 +24,9 @@ jest.mock('fs', () => ({ jest.mock('os', () => ({ freemem: jest.fn(), totalmem: jest.fn(), - uptime: jest.fn() + uptime: jest.fn(), + platform: jest.fn(), + release: jest.fn() })); jest.mock('process', () => ({ @@ -81,7 +83,7 @@ describe('Metrics', function () { }); describe('captureEvent', () => { - it('parses the hapi event', () => { + it('parses the hapi event', async () => { sinon.stub(os, 'uptime').returns(12000); sinon.stub(process, 'uptime').returns(5000); @@ -105,7 +107,7 @@ describe('Metrics', function () { 'host': 'blahblah.local' }; - expect(metrics.captureEvent(hapiEvent)).toMatchObject({ + expect(await metrics.captureEvent(hapiEvent)).toMatchObject({ 'concurrent_connections': 0, 'os': { 'load': { @@ -140,7 +142,7 @@ describe('Metrics', function () { }); }); - it('parses event with missing fields / NaN for responseTimes.avg', () => { + it('parses event with missing fields / NaN for responseTimes.avg', async () => { const hapiEvent = { requests: { '5603': { total: 22, disconnects: 0, statusCodes: { '200': 22 } }, @@ -149,7 +151,7 @@ describe('Metrics', function () { host: 'blahblah.local', }; - expect(metrics.captureEvent(hapiEvent)).toMatchObject({ + expect(await metrics.captureEvent(hapiEvent)).toMatchObject({ process: { memory: { heap: {} }, pid: 8675309, uptime_in_millis: 5000000 }, os: { load: {}, diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js index 850f0711e417..b8c670edf129 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/ops_buffer/ops_buffer.js @@ -6,7 +6,6 @@ import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from '../../../../common/constants'; import { EventRoller } from './event_roller'; -import { getOSInfo } from './get_os_info'; import { CloudDetector } from '../../../cloud'; /** @@ -41,7 +40,7 @@ export function opsBuffer(server) { if (eventRollup && eventRollup.os) { eventRollup.os = { ...eventRollup.os, - ...(await getOSInfo()) + ...(await server.getOSInfo()) }; }