Skip to content

Commit

Permalink
[APM] Shim new platform
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Apr 4, 2019
1 parent afaf8ca commit 7b30d19
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 59 deletions.
21 changes: 9 additions & 12 deletions x-pack/plugins/apm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import { i18n } from '@kbn/i18n';
import { Server } from 'hapi';
import { resolve } from 'path';
import { CoreSetup } from 'src/core/server/index.js';
import mappings from './mappings.json';
import { makeApmUsageCollector } from './server/lib/apm_telemetry';
import { initErrorsApi } from './server/routes/errors';
import { initMetricsApi } from './server/routes/metrics';
import { initServicesApi } from './server/routes/services';
import { initTracesApi } from './server/routes/traces';
import { initTransactionGroupsApi } from './server/routes/transaction_groups';
import { plugin } from './server/new-platform/index';

// TODO: get proper types
export function apm(kibana: any) {
Expand Down Expand Up @@ -74,12 +70,13 @@ export function apm(kibana: any) {

// TODO: get proper types
init(server: any) {
initTransactionGroupsApi(server);
initTracesApi(server);
initServicesApi(server);
initErrorsApi(server);
initMetricsApi(server);
makeApmUsageCollector(server);
const initializerContext = {} as any;
const core = {
http: {
server
}
} as CoreSetup;
plugin(initializerContext).setup(core);
}
});
}
45 changes: 15 additions & 30 deletions x-pack/plugins/apm/public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import 'react-vis/dist/style.css';
import 'ui/autoload/all';
import 'ui/autoload/styles';
import chrome from 'ui/chrome';
import { I18nContext } from 'ui/i18n';
// @ts-ignore
import { uiModules } from 'ui/modules';
import 'uiExports/autocompleteProviders';
import { GlobalHelpExtension } from './components/app/GlobalHelpExtension';
import { Main } from './components/app/Main';
import { history } from './components/shared/Links/url_helpers';
// @ts-ignore
import configureStore from './store/config/configureStore';
import { plugin } from './new-platform';
import { REACT_APP_ROOT_ID } from './new-platform/plugin';
import './style/global_overrides.css';
import template from './templates/index.html';

Expand All @@ -31,32 +26,22 @@ chrome.helpExtension.set(domElement => {
ReactDOM.unmountComponentAtNode(domElement);
};
});
const REACT_APP_ROOT_ID = 'react-apm-root';

type PromiseResolver = (value?: {} | PromiseLike<{}> | undefined) => void;

// @ts-ignore
chrome.setRootTemplate(template);
const store = configureStore();
const checkForRoot = (resolve: PromiseResolver) => {
const ready = !!document.getElementById(REACT_APP_ROOT_ID);
if (ready) {
resolve();
} else {
setTimeout(() => checkForRoot(resolve), 10);
}

const checkForRoot = () => {
return new Promise(resolve => {
const ready = !!document.getElementById(REACT_APP_ROOT_ID);
if (ready) {
resolve();
} else {
setTimeout(() => resolve(checkForRoot()), 10);
}
});
};
const waitForRoot = new Promise(resolve => checkForRoot(resolve));

waitForRoot.then(() => {
ReactDOM.render(
<I18nContext>
<Provider store={store}>
<Router history={history}>
<Main />
</Router>
</Provider>
</I18nContext>,
document.getElementById(REACT_APP_ROOT_ID)
);
checkForRoot().then(() => {
const initializerContext = {} as any;
plugin(initializerContext).setup();
});
12 changes: 12 additions & 0 deletions x-pack/plugins/apm/public/new-platform/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { Plugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin();
}
33 changes: 33 additions & 0 deletions x-pack/plugins/apm/public/new-platform/plugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import { I18nContext } from 'ui/i18n';
import { Main } from '../components/app/Main';
import { history } from '../components/shared/Links/url_helpers';
// @ts-ignore
import configureStore from '../store/config/configureStore';

export const REACT_APP_ROOT_ID = 'react-apm-root';

export class Plugin {
public setup() {
const store = configureStore();
ReactDOM.render(
<I18nContext>
<Provider store={store}>
<Router history={history}>
<Main />
</Router>
</Provider>
</I18nContext>,
document.getElementById(REACT_APP_ROOT_ID)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Server } from 'hapi';
import { CoreSetup } from 'src/core/server';
import {
APM_TELEMETRY_DOC_ID,
createApmTelementry,
getSavedObjectsClient
} from './apm_telemetry';

// TODO this type should be defined by the platform
interface KibanaHapiServer extends Server {
usage: {
collectorSet: {
makeUsageCollector: (options: unknown) => unknown;
register: (options: unknown) => unknown;
interface ExtendedCoreSetup extends CoreSetup {
http: CoreSetup['http'] & {
server: {
usage: {
collectorSet: {
makeUsageCollector: (options: unknown) => unknown;
register: (options: unknown) => unknown;
};
};
};
};
}

export function makeApmUsageCollector(server: KibanaHapiServer): void {
export function makeApmUsageCollector(core: ExtendedCoreSetup) {
const { server } = core.http;

const apmUsageCollector = server.usage.collectorSet.makeUsageCollector({
type: 'apm',
fetch: async () => {
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/apm/server/new-platform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { Plugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin();
}
24 changes: 24 additions & 0 deletions x-pack/plugins/apm/server/new-platform/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { CoreSetup } from 'src/core/server';
import { makeApmUsageCollector } from '../lib/apm_telemetry';
import { initErrorsApi } from '../routes/errors';
import { initMetricsApi } from '../routes/metrics';
import { initServicesApi } from '../routes/services';
import { initTracesApi } from '../routes/traces';
import { initTransactionGroupsApi } from '../routes/transaction_groups';

export class Plugin {
public setup(core: CoreSetup) {
initTransactionGroupsApi(core);
initTracesApi(core);
initServicesApi(core);
initErrorsApi(core);
initMetricsApi(core);
makeApmUsageCollector(core as any);
}
}
5 changes: 3 additions & 2 deletions x-pack/plugins/apm/server/routes/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

import Boom from 'boom';
import { Server } from 'hapi';
import Joi from 'joi';
import { Legacy } from 'kibana';
import { CoreSetup } from 'src/core/server';
import { getDistribution } from '../lib/errors/distribution/get_distribution';
import { getErrorGroup } from '../lib/errors/get_error_group';
import { getErrorGroups } from '../lib/errors/get_error_groups';
Expand All @@ -21,7 +21,8 @@ const defaultErrorHandler = (err: Error) => {
throw Boom.boomify(err, { statusCode: 400 });
};

export function initErrorsApi(server: Server) {
export function initErrorsApi(core: CoreSetup) {
const { server } = core.http;
server.route({
method: 'GET',
path: ROOT,
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/apm/server/routes/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Boom from 'boom';
import { Server } from 'hapi';
import { CoreSetup } from 'src/core/server';
import { withDefaultValidators } from '../lib/helpers/input_validation';
import { setupRequest } from '../lib/helpers/setup_request';
import { getAllMetricsChartData } from '../lib/metrics/get_all_metrics_chart_data';
Expand All @@ -16,7 +16,8 @@ const defaultErrorHandler = (err: Error) => {
throw Boom.boomify(err, { statusCode: 400 });
};

export function initMetricsApi(server: Server) {
export function initMetricsApi(core: CoreSetup) {
const { server } = core.http;
server.route({
method: 'GET',
path: `/api/apm/services/{serviceName}/metrics/charts`,
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/apm/server/routes/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Boom from 'boom';
import { Server } from 'hapi';
import { CoreSetup } from 'src/core/server';
import { AgentName } from '../../typings/es_schemas/ui/fields/Agent';
import { createApmTelementry, storeApmTelemetry } from '../lib/apm_telemetry';
import { withDefaultValidators } from '../lib/helpers/input_validation';
Expand All @@ -20,7 +20,8 @@ const defaultErrorHandler = (err: Error) => {
throw Boom.boomify(err, { statusCode: 400 });
};

export function initServicesApi(server: Server) {
export function initServicesApi(core: CoreSetup) {
const { server } = core.http;
server.route({
method: 'GET',
path: ROOT,
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/apm/server/routes/traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import Boom from 'boom';
import { Server } from 'hapi';

import { CoreSetup } from 'src/core/server';
import { withDefaultValidators } from '../lib/helpers/input_validation';
import { setupRequest } from '../lib/helpers/setup_request';
import { getTopTraces } from '../lib/traces/get_top_traces';
Expand All @@ -18,7 +19,9 @@ const defaultErrorHandler = (err: Error) => {
throw Boom.boomify(err, { statusCode: 400 });
};

export function initTracesApi(server: Server) {
export function initTracesApi(core: CoreSetup) {
const { server } = core.http;

// Get trace list
server.route({
method: 'GET',
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/apm/server/routes/transaction_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import Boom from 'boom';
import { Server } from 'hapi';
import Joi from 'joi';
import { CoreSetup } from 'src/core/server';
import { withDefaultValidators } from '../lib/helpers/input_validation';
import { setupRequest } from '../lib/helpers/setup_request';
import { getChartsData } from '../lib/transactions/charts';
Expand All @@ -19,7 +19,9 @@ const defaultErrorHandler = (err: Error) => {
throw Boom.boomify(err, { statusCode: 400 });
};

export function initTransactionGroupsApi(server: Server) {
export function initTransactionGroupsApi(core: CoreSetup) {
const { server } = core.http;

server.route({
method: 'GET',
path:
Expand Down

0 comments on commit 7b30d19

Please sign in to comment.