[Server/Routes] organize status routes together with others (#17558) (#17589)

* [Server/Routes] organize status routes together with others

* remove flaky assertion

* move all the status stuff into src/server/status

* sugar on imports/exports

* fix lint/jest test

* comment and todo
This commit is contained in:
Tim Sullivan 2018-04-06 09:28:19 -07:00 committed by GitHub
parent e2aa793729
commit e98173cf2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 78 additions and 59 deletions

View file

@ -4,12 +4,12 @@ import Promise from 'bluebird';
import { mkdirp as mkdirpNode } from 'mkdirp';
import manageUuid from './server/lib/manage_uuid';
import search from './server/routes/api/search';
import { searchApi } from './server/routes/api/search';
import { scrollSearchApi } from './server/routes/api/scroll_search';
import { importApi } from './server/routes/api/import';
import { exportApi } from './server/routes/api/export';
import { homeApi } from './server/routes/api/home';
import scripts from './server/routes/api/scripts';
import { scriptsApi } from './server/routes/api/scripts';
import { registerSuggestionsApi } from './server/routes/api/suggestions';
import { registerFieldFormats } from './server/field_formats/register';
import { registerTutorials } from './server/tutorials/register';
@ -144,8 +144,8 @@ export default function (kibana) {
// uuid
manageUuid(server);
// routes
search(server);
scripts(server);
searchApi(server);
scriptsApi(server);
scrollSearchApi(server);
importApi(server);
exportApi(server);

View file

@ -1,5 +1,5 @@
import { registerLanguages } from './register_languages';
export default function (server) {
export function scriptsApi(server) {
registerLanguages(server);
}

View file

@ -1,5 +1,5 @@
import registerCount from './count/register_count';
export default function (server) {
export function searchApi(server) {
registerCount(server);
}

View file

@ -8,7 +8,7 @@ import configSetupMixin from './config/setup';
import httpMixin from './http';
import { loggingMixin } from './logging';
import warningsMixin from './warnings';
import statusMixin from './status';
import { statusMixin } from './status';
import pidMixin from './pid';
import { configDeprecationWarningsMixin } from './config/deprecation_warnings';
import configCompleteMixin from './config/complete';

View file

@ -1,8 +1,8 @@
import ServerStatus from './server_status';
import wrapAuthConfig from './wrap_auth_config';
import { Metrics } from './metrics';
import { registerStatusPage, registerStatusApi } from './routes';
export default function (kbnServer, server, config) {
export function statusMixin(kbnServer, server, config) {
kbnServer.status = new ServerStatus(kbnServer.server);
if (server.plugins['even-better']) {
@ -13,50 +13,7 @@ export default function (kbnServer, server, config) {
});
}
const wrapAuth = wrapAuthConfig(config.get('status.allowAnonymous'));
const matchSnapshot = /-SNAPSHOT$/;
server.route(wrapAuth({
method: 'GET',
path: '/api/status',
config: {
tags: ['api']
},
handler: function (request, reply) {
const status = {
name: config.get('server.name'),
uuid: config.get('server.uuid'),
version: {
number: config.get('pkg.version').replace(matchSnapshot, ''),
build_hash: config.get('pkg.buildSha'),
build_number: config.get('pkg.buildNum'),
build_snapshot: matchSnapshot.test(config.get('pkg.version'))
},
status: kbnServer.status.toJSON(),
metrics: kbnServer.metrics
};
return reply(status);
}
}));
server.decorate('reply', 'renderStatusPage', async function () {
const app = server.getHiddenUiAppById('status_page');
const reply = this;
const response = app
? await reply.renderApp(app)
: reply(kbnServer.status.toString());
if (response) {
response.code(kbnServer.status.isGreen() ? 200 : 503);
return response;
}
});
server.route(wrapAuth({
method: 'GET',
path: '/status',
handler(request, reply) {
reply.renderStatusPage();
}
}));
// init routes
registerStatusPage(kbnServer, server, config);
registerStatusApi(kbnServer, server, config);
}

View file

@ -0,0 +1,31 @@
import { wrapAuthConfig } from '../../wrap_auth_config';
const matchSnapshot = /-SNAPSHOT$/;
export function registerStatusApi(kbnServer, server, config) {
const wrapAuth = wrapAuthConfig(config.get('status.allowAnonymous'));
server.route(wrapAuth({
method: 'GET',
path: '/api/status',
config: {
tags: ['api']
},
async handler(request, reply) {
const status = {
name: config.get('server.name'),
uuid: config.get('server.uuid'),
version: {
number: config.get('pkg.version').replace(matchSnapshot, ''),
build_hash: config.get('pkg.buildSha'),
build_number: config.get('pkg.buildNum'),
build_snapshot: matchSnapshot.test(config.get('pkg.version'))
},
status: kbnServer.status.toJSON(),
metrics: kbnServer.metrics
};
return reply(status);
}
}));
}

View file

@ -0,0 +1,2 @@
export { registerStatusPage } from './page/register_status';
export { registerStatusApi } from './api/register_status';

View file

@ -0,0 +1,26 @@
import { wrapAuthConfig } from '../../wrap_auth_config';
export function registerStatusPage(kbnServer, server, config) {
const wrapAuth = wrapAuthConfig(config.get('status.allowAnonymous'));
server.decorate('reply', 'renderStatusPage', async function () {
const app = server.getHiddenUiAppById('status_page');
const reply = this;
const response = app
? await reply.renderApp(app)
: reply(kbnServer.status.toString());
if (response) {
response.code(kbnServer.status.isGreen() ? 200 : 503);
return response;
}
});
server.route(wrapAuth({
method: 'GET',
path: '/status',
handler(request, reply) {
reply.renderStatusPage();
}
}));
}

View file

@ -1,6 +1,8 @@
import { assign, identity } from 'lodash';
export default (allowAnonymous) => {
if (allowAnonymous) return options => assign(options, { config: { auth: false } });
export const wrapAuthConfig = allowAnonymous => {
if (allowAnonymous) {
return options => assign(options, { config: { auth: false } });
}
return identity;
};

View file

@ -1,4 +1,4 @@
import wrapAuthConfig from './wrap_auth_config';
import { wrapAuthConfig } from './wrap_auth_config';
describe('Status wrapAuthConfig', () => {
let options;

View file

@ -34,7 +34,8 @@ export default function ({ getService }) {
expect(body.metrics.os.cpu.load_average['5m']).to.be.a('number');
expect(body.metrics.os.cpu.load_average['15m']).to.be.a('number');
expect(body.metrics.response_times.avg_in_millis).not.to.be(undefined); // a number, but is null if no measurements have yet been collected for averaging
// TODO: fix this in the status/metrics class so this is always defined
// expect(body.metrics.response_times.avg_in_millis).not.to.be(undefined); // a number, but is null if no measurements have yet been collected for averaging
expect(body.metrics.response_times.max_in_millis).to.be.a('number');
expect(body.metrics.requests.total).to.be.a('number');