Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0967c89
WIP, trying things.
justinkambic Sep 5, 2019
96b5568
Delete call to hapi register function, use route for bootstrapping GQL.
justinkambic Sep 5, 2019
edfd343
Add files to use for new platform shim.
justinkambic Sep 6, 2019
7ad44f3
Add server shim.
justinkambic Sep 9, 2019
5fdf8c0
Update mock in test file to conform to new shim interface.
justinkambic Sep 9, 2019
a8518f4
Delete unneeded import and var declaration.
justinkambic Sep 10, 2019
c4e1a96
Fix broken type files.
justinkambic Sep 10, 2019
5e7a6e6
Add plugin shim initializer to app bootstrapper.
justinkambic Sep 12, 2019
787038a
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 12, 2019
3a01810
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 19, 2019
453b6c5
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 24, 2019
d918cb7
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 10, 2019
9267c03
Extract plugins to separate param for shim.
justinkambic Oct 10, 2019
7af1a87
Rename interface and update tests.
justinkambic Oct 10, 2019
49bc115
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 10, 2019
e79059f
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 11, 2019
c53d6a6
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 14, 2019
70add2f
Merge branch 'master' into uptime_server-new-platform
elasticmachine Oct 18, 2019
890d349
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 18, 2019
e13e5d0
Delete unneeded file.
justinkambic Oct 21, 2019
c654473
Merge branch 'uptime_server-new-platform' of github.com:justinkambic/…
justinkambic Oct 21, 2019
fca8229
Merge branch 'master' into uptime_server-new-platform
elasticmachine Oct 21, 2019
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
12 changes: 10 additions & 2 deletions x-pack/legacy/plugins/uptime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import { i18n } from '@kbn/i18n';
import { resolve } from 'path';
import { PluginInitializerContext } from 'src/core/server';
import { PLUGIN } from './common/constants';
import { initServerWithKibana, KibanaServer } from './server';
import { KibanaServer, plugin } from './server';

export const uptime = (kibana: any) =>
new kibana.Plugin({
Expand All @@ -33,6 +34,13 @@ export const uptime = (kibana: any) =>
home: ['plugins/uptime/register_feature'],
},
init(server: KibanaServer) {
initServerWithKibana(server);
const initializerContext = {} as PluginInitializerContext;
const { elasticsearch, xpack_main } = server.plugins;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: you can also use the NP elasticsearch client by taking it from server.newPlatform.setup.core.elasticsearch

plugin(initializerContext).setup({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature for setup is setup(core, plugins) with all plugin dependencies being passed in as the second argument. Usage collector will be exposed from the telemetry plugin and xpack licencing is available as a NP plugin. So it would be a bit more accurate to move these dependencies into a second argument.

elasticsearch,
usageCollector: server.usage,
xpack: xpack_main,
route: (arg: any) => server.route(arg),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical as a first step, but at some point it would be good to switch to using the real NP core router inside your shim. You can use server.newPlatform.setup.core.http.createRouter().

});
},
});
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/uptime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

export { initServerWithKibana, KibanaServer } from './kibana.index';
export { plugin } from './plugin';
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/uptime/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PLUGIN } from '../common/constants';
import { KibanaTelemetryAdapter } from './lib/adapters/telemetry';
import { compose } from './lib/compose/kibana';
import { initUptimeServer } from './uptime_server';
import { KibanaCore } from './lib/adapters/framework';

export interface KibanaRouteOptions {
path: string;
Expand All @@ -28,13 +29,12 @@ export interface KibanaServer extends Server {
};
}

export const initServerWithKibana = (server: KibanaServer) => {
export const initServerWithKibana = (server: KibanaCore) => {
const libs = compose(server);
server.usage.collectorSet.register(KibanaTelemetryAdapter.initUsageCollector(server));
server.usageCollector.collectorSet.register(KibanaTelemetryAdapter.initUsageCollector(server));
initUptimeServer(libs);

const xpackMainPlugin = server.plugins.xpack_main;
xpackMainPlugin.registerFeature({
server.xpack.registerFeature({
id: PLUGIN.ID,
name: i18n.translate('xpack.uptime.featureRegistry.uptimeFeatureName', {
defaultMessage: 'Uptime',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export interface UMFrameworkRouteOptions<
config?: any;
}

export interface KibanaCore {
elasticsearch: any;
usageCollector: any;
xpack: any;
route: any;
}

export type UMFrameworkRouteHandler<RouteRequest extends UMFrameworkRequest> = (
request: UMFrameworkRequest,
h: ResponseToolkit
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
*/

import { GraphQLSchema } from 'graphql';
import { Server } from 'hapi';
import { Request, ResponseToolkit } from 'hapi';
import { runHttpQuery } from 'apollo-server-core';
import { KibanaCore } from './adapter_types';
import {
UMBackendFrameworkAdapter,
UMFrameworkRequest,
UMFrameworkResponse,
UMFrameworkRouteOptions,
UMHapiGraphQLPluginOptions,
} from './adapter_types';
import { uptimeGraphQLHapiPlugin } from './apollo_framework_adapter';
import { DEFAULT_GRAPHQL_PATH } from '../../../graphql';

export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapter {
private server: Server;

constructor(hapiServer: Server) {
this.server = hapiServer;
constructor(private readonly server: KibanaCore) {
this.server = server;
}

public registerRoute<
Expand All @@ -30,18 +29,46 @@ export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapte
}

public registerGraphQLEndpoint(routePath: string, schema: GraphQLSchema): void {
this.server.register<UMHapiGraphQLPluginOptions>({
options: {
graphQLOptions: (req: any) => ({
context: { req },
schema,
}),
path: routePath,
route: {
tags: ['access:uptime'],
},
const options = {
graphQLOptions: (req: any) => ({
context: { req },
schema,
}),
path: routePath,
route: {
tags: ['access:uptime'],
},
};
this.server.route({
options: options.route,
handler: async (request: Request, h: ResponseToolkit) => {
try {
const { method } = request;
const query =
method === 'post'
? (request.payload as Record<string, any>)
: (request.query as Record<string, any>);

const graphQLResponse = await runHttpQuery([request], {
method: method.toUpperCase(),
options: options.graphQLOptions,
query,
});

return h.response(graphQLResponse).type('application/json');
} catch (error) {
if (error.isGraphQLError === true) {
return h
.response(error.message)
.code(error.statusCode)
.type('application/json');
}
return h.response(error).type('application/json');
}
},
plugin: uptimeGraphQLHapiPlugin,
method: ['get', 'post'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a useful example for other teams for how to replace the hapi plugin. When you switch to using the NP router in your shim (from server.newPlatform.setup.core.http.createRouter()) you won't be able to define one route with two methods (get & post) so this will have to be split into two routes.

path: options.path || DEFAULT_GRAPHQL_PATH,
vhost: undefined,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
*/

import { KibanaTelemetryAdapter } from '../kibana_telemetry_adapter';
import { KibanaCore } from '../../framework';

describe('KibanaTelemetryAdapter', () => {
let server: any;
let server: KibanaCore;
let collector: { type: string; fetch: () => Promise<any>; isReady: () => boolean };
beforeEach(() => {
server = {
usage: {
elasticsearch: {},
route: {},
usageCollector: {
collectorSet: {
makeUsageCollector: (val: any) => {
collector = val;
},
},
},
xpack: {},
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { KibanaCore } from '../framework';

interface UptimeTelemetry {
overview_page: number;
monitor_page: number;
Expand All @@ -19,8 +21,8 @@ const BUCKET_SIZE = 3600;
const BUCKET_NUMBER = 24;

export class KibanaTelemetryAdapter {
public static initUsageCollector(server: any) {
const { collectorSet } = server.usage;
public static initUsageCollector(server: KibanaCore) {
const { collectorSet } = server.usageCollector;
return collectorSet.makeUsageCollector({
type: 'uptime',
fetch: async () => {
Expand Down
13 changes: 6 additions & 7 deletions x-pack/legacy/plugins/uptime/server/lib/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { UMAuthDomain, UMMonitorsDomain, UMPingsDomain } from '../domains';
import { UMDomainLibs, UMServerLibs } from '../lib';
import { UMMonitorStatesDomain } from '../domains/monitor_states';
import { ElasticsearchMonitorStatesAdapter } from '../adapters/monitor_states';
import { KibanaCore } from '../adapters/framework';

export function compose(hapiServer: any): UMServerLibs {
const framework = new UMKibanaBackendFrameworkAdapter(hapiServer);
const database = new UMKibanaDatabaseAdapter(hapiServer.plugins.elasticsearch);
export function compose(server: KibanaCore): UMServerLibs {
const framework = new UMKibanaBackendFrameworkAdapter(server);
const database = new UMKibanaDatabaseAdapter(server.elasticsearch);

const pingsDomain = new UMPingsDomain(new ElasticsearchPingsAdapter(database), {});
const authDomain = new UMAuthDomain(new UMXPackAuthAdapter(hapiServer.plugins.xpack_main), {});
const authDomain = new UMAuthDomain(new UMXPackAuthAdapter(server.xpack), {});
const monitorsDomain = new UMMonitorsDomain(new ElasticsearchMonitorsAdapter(database), {});
const monitorStatesDomain = new UMMonitorStatesDomain(
new ElasticsearchMonitorStatesAdapter(database),
Expand All @@ -33,11 +34,9 @@ export function compose(hapiServer: any): UMServerLibs {
pings: pingsDomain,
};

const libs: UMServerLibs = {
return {
framework,
database,
...domainLibs,
};

return libs;
}
19 changes: 19 additions & 0 deletions x-pack/legacy/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/

import { PluginInitializerContext } from 'src/core/server';
import { initServerWithKibana } from './kibana.index';
import { KibanaCore } from './lib/adapters/framework';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin();
}

export class Plugin {
public setup(core: KibanaCore) {
initServerWithKibana(core);
}
}
Empty file.