Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Monitoring] New platform migration - Server shim (#46507) #48014

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions x-pack/legacy/plugins/monitoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/

import { resolve } from 'path';
import { init, postInit } from './init';
import { config } from './config';
import { deprecations } from './deprecations';
import { getUiExports } from './ui_exports';
import { Plugin } from './server/plugin';
import { initInfraSource } from './server/lib/logs/init_infra_source';

/**
* Invokes plugin modules to instantiate the Monitoring plugin for Kibana
Expand All @@ -20,11 +21,70 @@ export const monitoring = (kibana) => new kibana.Plugin({
id: 'monitoring',
configPrefix: 'xpack.monitoring',
publicDir: resolve(__dirname, 'public'),
init(server, _options) { init(this, server); },
init(server, _options) {
const configs = [
'xpack.monitoring.ui.enabled',
'xpack.monitoring.kibana.collection.enabled',
'xpack.monitoring.max_bucket_size',
'xpack.monitoring.min_interval_seconds',
'kibana.index',
'pkg.version',
'xpack.monitoring.show_license_expiration',
'xpack.monitoring.ui.container.elasticsearch.enabled',
'xpack.monitoring.ui.container.logstash.enabled',
'xpack.monitoring.tests.cloud_detector.enabled',
'xpack.monitoring.kibana.collection.interval',
'xpack.monitoring.elasticsearch.hosts',
'xpack.monitoring.elasticsearch',
'xpack.monitoring.xpack_api_polling_frequency_millis',
'server.uuid',
'server.name',
'server.host',
'server.port',
'xpack.monitoring.cluster_alerts.email_notifications.enabled',
'xpack.monitoring.cluster_alerts.email_notifications.email_address',
'xpack.monitoring.ccs.enabled',
'xpack.monitoring.elasticsearch.logFetchCount',
];

const serverConfig = server.config();
const serverFacade = {
config: () => ({
get: key => {
if (configs.includes(key)) {
return serverConfig.get(key);
}
throw `Unknown key '${key}'`;
}
}),
usage: {
collectorSet: server.usage.collectorSet
},
injectUiAppVars: server.injectUiAppVars,
log: (...args) => server.log(...args),
getOSInfo: server.getOSInfo,
events: {
on: (...args) => server.events.on(...args)
},
expose: (...args) => server.expose(...args),
route: (...args) => server.route(...args),
_hapi: server,
_kbnServer: this.kbnServer
};

const plugins = {
xpack_main: server.plugins.xpack_main,
elasticsearch: server.plugins.elasticsearch,
infra: server.plugins.infra,
};

new Plugin().setup(serverFacade, plugins);
},
config,
deprecations,
uiExports: getUiExports(),
postInit(server) {
postInit(server);
const serverConfig = server.config();
initInfraSource(serverConfig, server.plugins.infra);
},
});
123 changes: 0 additions & 123 deletions x-pack/legacy/plugins/monitoring/init.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ function getMockServerFromConnectionUrl(monitoringClusterUrl) {
},
};

const config = () => {
return {
get: (path) => { return get(server, path); },
set: noop
};
const config = {
get: (path) => { return get(server, path); },
set: noop
};

return {
config,
plugins: {
elasticsearch: {
getCluster: sinon.stub().withArgs('admin').returns({
config: sinon.stub().returns(server.elasticsearch)
}),
createCluster: sinon.stub(),
}
elasticsearchPlugin: {
getCluster: sinon.stub().withArgs('admin').returns({
config: sinon.stub().returns(server.elasticsearch)
}),
createCluster: sinon.stub(),
},
events: {
on: noop,
Expand Down Expand Up @@ -81,7 +77,7 @@ describe('Instantiate Client', () => {

exposeClient(server);

const createCluster = server.plugins.elasticsearch.createCluster;
const createCluster = server.elasticsearchPlugin.createCluster;
const createClusterCall = createCluster.getCall(0);

sinon.assert.calledOnce(createCluster);
Expand All @@ -94,7 +90,7 @@ describe('Instantiate Client', () => {

exposeClient(server);

const createCluster = server.plugins.elasticsearch.createCluster;
const createCluster = server.elasticsearchPlugin.createCluster;
const createClusterCall = createCluster.getCall(0);

sinon.assert.calledOnce(createCluster);
Expand All @@ -110,7 +106,7 @@ describe('Instantiate Client', () => {
const server = getMockServerFromConnectionUrl(null); // pass null for URL to create the client using prod config
exposeClient(server);

const createCluster = server.plugins.elasticsearch.createCluster;
const createCluster = server.elasticsearchPlugin.createCluster;
const createClusterCall = createCluster.getCall(0);
const createClientOptions = createClusterCall.args[1];

Expand All @@ -125,7 +121,7 @@ describe('Instantiate Client', () => {
const server = getMockServerFromConnectionUrl('http://monitoring-cluster.test:9200');
exposeClient(server);

const createCluster = server.plugins.elasticsearch.createCluster;
const createCluster = server.elasticsearchPlugin.createCluster;
const createClusterCall = createCluster.getCall(0);
const createClientOptions = createClusterCall.args[1];

Expand All @@ -140,12 +136,12 @@ describe('Instantiate Client', () => {
describe('hasMonitoringCluster', () => {
it('returns true if monitoring is configured', () => {
const server = getMockServerFromConnectionUrl('http://monitoring-cluster.test:9200'); // pass null for URL to create the client using prod config
expect(hasMonitoringCluster(server)).to.be(true);
expect(hasMonitoringCluster(server.config)).to.be(true);
});

it('returns false if monitoring is not configured', () => {
const server = getMockServerFromConnectionUrl(null);
expect(hasMonitoringCluster(server)).to.be(false);
expect(hasMonitoringCluster(server.config)).to.be(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import { LOGGING_TAG } from '../../common/constants';
* Kibana itself is connected to a production cluster.
*/

export function exposeClient(server) {
const config = hasMonitoringCluster(server) ? server.config().get('xpack.monitoring.elasticsearch') : {};
const cluster = server.plugins.elasticsearch.createCluster('monitoring', {
...config,
export function exposeClient({ config, events, log, elasticsearchPlugin }) {
const elasticsearchConfig = hasMonitoringCluster(config) ? config.get('xpack.monitoring.elasticsearch') : {};
const cluster = elasticsearchPlugin.createCluster('monitoring', {
...elasticsearchConfig,
plugins: [monitoringBulk],
logQueries: Boolean(config.logQueries),
logQueries: Boolean(elasticsearchConfig.logQueries),
});

server.events.on('stop', bindKey(cluster, 'close'));
const configSource = hasMonitoringCluster(server) ? 'monitoring' : 'production';
server.log([LOGGING_TAG, 'es-client'], `config sourced from: ${configSource} cluster`);
events.on('stop', bindKey(cluster, 'close'));
const configSource = hasMonitoringCluster(config) ? 'monitoring' : 'production';
log([LOGGING_TAG, 'es-client'], `config sourced from: ${configSource} cluster`);
}

export function hasMonitoringCluster(server) {
const hosts = server.config().get('xpack.monitoring.elasticsearch.hosts');
export function hasMonitoringCluster(config) {
const hosts = config.get('xpack.monitoring.elasticsearch.hosts');
return Boolean(hosts && hosts.length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import { LOGGING_TAG } from '../common/constants';
/*
* Expose xpackInfo for the Monitoring cluster as server.plugins.monitoring.info
*/
export const initMonitoringXpackInfo = async server => {
const config = server.config();
const xpackInfo = hasMonitoringCluster(server) ? server.plugins.xpack_main.createXPackInfo({
export const initMonitoringXpackInfo = async ({ config, xpackMainPlugin, expose, log }) => {
const xpackInfo = hasMonitoringCluster(config) ? xpackMainPlugin.createXPackInfo({
clusterSource: 'monitoring',
pollFrequencyInMillis: config.get('xpack.monitoring.xpack_api_polling_frequency_millis')
}) : server.plugins.xpack_main.info;
}) : xpackMainPlugin.info;

xpackInfo.feature('monitoring').registerLicenseCheckResultsGenerator(checkLicenseGenerator);
server.expose('info', xpackInfo);
expose('info', xpackInfo);

// check if X-Pack is installed on Monitoring Cluster
const xpackInfoTest = await xpackInfo.refreshNow();
if (!xpackInfoTest.isAvailable()) {
server.log([LOGGING_TAG, 'warning'], `X-Pack Monitoring Cluster Alerts will not be available: ${xpackInfoTest.unavailableReason()}`);
log([LOGGING_TAG, 'warning'], `X-Pack Monitoring Cluster Alerts will not be available: ${xpackInfoTest.unavailableReason()}`);
}
};
Loading