[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
This commit is contained in:
parent
6cf34f24f6
commit
dd0ce0f7f3
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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';
|
|
@ -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', () => ({
|
|
@ -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
|
||||
|
|
|
@ -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: {},
|
||||
|
|
|
@ -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())
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue