Skip to content

Commit

Permalink
[APM] Migrate server routes to NP
Browse files Browse the repository at this point in the history
Closes elastic#49238.

Pass legacy API to NP plugin in order to use internal SO client

Fix issues with agent configuration APIs

Update tsconfig template for TS optimization

Fix typo
  • Loading branch information
dgieselaar committed Nov 21, 2019
1 parent db401e4 commit 9b368b4
Show file tree
Hide file tree
Showing 102 changed files with 1,212 additions and 849 deletions.
1 change: 0 additions & 1 deletion src/legacy/core_plugins/apm_oss/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default function apmOss(kibana) {
spanIndices: Joi.string().default('apm-*'),
metricsIndices: Joi.string().default('apm-*'),
onboardingIndices: Joi.string().default('apm-*'),
apmAgentConfigurationIndex: Joi.string().default('.apm-agent-configuration'),
}).default();
},

Expand Down
11 changes: 11 additions & 0 deletions src/plugins/apm_oss/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "apm_oss",
"version": "8.0.0",
"server": true,
"kibanaVersion": "kibana",
"configPath": [
"apm_oss"
],
"ui": false,
"requiredPlugins": []
}
41 changes: 41 additions & 0 deletions src/plugins/apm_oss/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 { schema, TypeOf } from '@kbn/config-schema';
import { PluginInitializerContext } from '../../../core/server';
import { APMOSSPlugin } from './plugin';

export const config = {
schema: schema.object({
transactionIndices: schema.string({ defaultValue: 'apm-*' }),
spanIndices: schema.string({ defaultValue: 'apm-*' }),
errorIndices: schema.string({ defaultValue: 'apm-*' }),
metricsIndices: schema.string({ defaultValue: 'apm-*' }),
sourcemapIndices: schema.string({ defaultValue: 'apm-*' }),
onboardingIndices: schema.string({ defaultValue: 'apm-*' }),
indexPattern: schema.string({ defaultValue: 'apm-*' }),
}),
};

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

export type APMOSSConfig = TypeOf<typeof config.schema>;

export { APMOSSPlugin as Plugin };
38 changes: 38 additions & 0 deletions src/plugins/apm_oss/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { Plugin, CoreSetup, PluginInitializerContext } from 'src/core/server';
import { Observable } from 'rxjs';
import { APMOSSConfig } from './';

export class APMOSSPlugin implements Plugin<{ config$: Observable<APMOSSConfig> }> {
constructor(private readonly initContext: PluginInitializerContext) {
this.initContext = initContext;
}

public setup(core: CoreSetup) {
const config$ = this.initContext.config.create<APMOSSConfig>();

return {
config$,
};
}

start() {}
stop() {}
}
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/common/projections/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
Expand All @@ -16,7 +16,7 @@ export function getErrorGroupsProjection({
setup,
serviceName
}: {
setup: Setup;
setup: SetupWithAllFilters;
serviceName: string;
}) {
const { start, end, uiFiltersES, indices } = setup;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/common/projections/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import {
SERVICE_NAME,
PROCESSOR_EVENT,
Expand All @@ -30,7 +30,7 @@ export function getMetricsProjection({
serviceName,
serviceNodeName
}: {
setup: Setup;
setup: SetupWithAllFilters;
serviceName: string;
serviceNodeName?: string;
}) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/common/projections/service_nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import { SERVICE_NODE_NAME } from '../elasticsearch_fieldnames';
import { mergeProjection } from './util/merge_projection';
import { getMetricsProjection } from './metrics';
Expand All @@ -14,7 +14,7 @@ export function getServiceNodesProjection({
serviceName,
serviceNodeName
}: {
setup: Setup;
setup: SetupWithAllFilters;
serviceName: string;
serviceNodeName?: string;
}) {
Expand Down
8 changes: 6 additions & 2 deletions x-pack/legacy/plugins/apm/common/projections/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import { SERVICE_NAME, PROCESSOR_EVENT } from '../elasticsearch_fieldnames';
import { rangeFilter } from '../../server/lib/helpers/range_filter';

export function getServicesProjection({ setup }: { setup: Setup }) {
export function getServicesProjection({
setup
}: {
setup: SetupWithAllFilters;
}) {
const { start, end, uiFiltersES, indices } = setup;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { omit } from 'lodash';
import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import { TRANSACTION_NAME, PARENT_ID } from '../elasticsearch_fieldnames';
import { Options } from '../../server/lib/transaction_groups/fetcher';
import { getTransactionsProjection } from './transactions';
Expand All @@ -14,7 +14,7 @@ export function getTransactionGroupsProjection({
setup,
options
}: {
setup: Setup;
setup: SetupWithAllFilters;
options: Options;
}) {
const transactionsProjection = getTransactionsProjection({
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/common/projections/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Setup } from '../../server/lib/helpers/setup_request';
import { SetupWithAllFilters } from '../../server/lib/helpers/setup_request';
import {
SERVICE_NAME,
TRANSACTION_TYPE,
Expand All @@ -19,7 +19,7 @@ export function getTransactionsProjection({
transactionName,
transactionType
}: {
setup: Setup;
setup: SetupWithAllFilters;
serviceName?: string;
transactionName?: string;
transactionType?: string;
Expand Down
16 changes: 6 additions & 10 deletions x-pack/legacy/plugins/apm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import { i18n } from '@kbn/i18n';
import { Server } from 'hapi';
import { resolve } from 'path';
import { PluginInitializerContext } from '../../../../src/core/server';
import { APMPluginContract } from '../../../plugins/apm/server/plugin';
import { LegacyPluginInitializer } from '../../../../src/legacy/types';
import mappings from './mappings.json';
import { plugin } from './server/new-platform';
import { makeApmUsageCollector } from './server/lib/apm_telemetry';

export const apm: LegacyPluginInitializer = kibana => {
return new kibana.Plugin({
Expand Down Expand Up @@ -112,15 +112,11 @@ export const apm: LegacyPluginInitializer = kibana => {
}
}
});
makeApmUsageCollector(server);
const apmPlugin = server.newPlatform.setup.plugins
.apm as APMPluginContract;

const initializerContext = {} as PluginInitializerContext;
const legacySetup = {
server
};
plugin(initializerContext).setup(
server.newPlatform.setup.core,
legacySetup
);
apmPlugin.registerLegacyAPI({ server });
}
});
};
3 changes: 0 additions & 3 deletions x-pack/legacy/plugins/apm/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@
},
"apm_oss.metricsIndices": {
"type": "keyword"
},
"apm_oss.apmAgentConfigurationIndex": {
"type": "keyword"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ const APM_INDEX_LABELS = [
label: i18n.translate('xpack.apm.settings.apmIndices.metricsIndicesLabel', {
defaultMessage: 'Metrics Indices'
})
},
{
configurationName: 'apm_oss.apmAgentConfigurationIndex',
label: i18n.translate(
'xpack.apm.settings.apmIndices.apmAgentConfigurationIndexLabel',
{ defaultMessage: 'Agent Configuration Index' }
)
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('callApi', () => {
it('should not add debug param for non-APM endpoints', async () => {
await callApi(http, { pathname: `/api/kibana` });

expect(http.get).toHaveBeenCalledWith('/api/kibana', {});
expect(http.get).toHaveBeenCalledWith('/api/kibana', { query: {} });
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ describe('callApmApi', () => {
expect.objectContaining({
pathname: '/api/apm',
method: 'POST',
body: JSON.stringify({
body: {
foo: 'bar',
bar: 'foo'
})
}
})
);
});
Expand Down
16 changes: 12 additions & 4 deletions x-pack/legacy/plugins/apm/public/services/rest/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ function fetchOptionsWithDebug(fetchOptions: FetchOptions) {
sessionStorage.getItem('apm_debug') === 'true' &&
startsWith(fetchOptions.pathname, '/api/apm');

if (!debugEnabled) {
return fetchOptions;
}
const isGet = !fetchOptions.method || fetchOptions.method === 'GET';

// Need an empty body to pass route validation
const body = isGet
? {}
: {
body: JSON.stringify(
fetchOptions.body || ({} as HttpFetchOptions['body'])
)
};

return {
...fetchOptions,
...body,
query: {
...fetchOptions.query,
_debug: true
...(debugEnabled ? { _debug: true } : {})
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export const createCallApmApi = (http: HttpServiceBase) =>
const { pathname, params = {}, ...opts } = options;

const path = (params.path || {}) as Record<string, any>;
const body = params.body
? { body: JSON.stringify(params.body) }
: undefined;
const query = params.query ? { query: params.query } : undefined;

const formattedPathname = Object.keys(path).reduce((acc, paramName) => {
return acc.replace(`{${paramName}}`, path[paramName]);
Expand All @@ -34,7 +30,7 @@ export const createCallApmApi = (http: HttpServiceBase) =>
return callApi(http, {
...opts,
pathname: formattedPathname,
...body,
...query
body: params.body,
query: params.query
});
}) as APMClient;
20 changes: 10 additions & 10 deletions x-pack/legacy/plugins/apm/public/utils/testHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Moment } from 'moment-timezone';
import React from 'react';
import { render, waitForElement } from 'react-testing-library';
import { MemoryRouter } from 'react-router-dom';
import { APMConfig } from '../../../../../plugins/apm/server';
import { LocationProvider } from '../context/LocationContext';
import { PromiseReturnType } from '../../typings/common';
import { ESFilter } from '../../typings/elasticsearch';
Expand Down Expand Up @@ -99,10 +100,7 @@ interface MockSetup {
end: number;
client: any;
internalClient: any;
config: {
get: any;
has: any;
};
config: APMConfig;
uiFiltersES: ESFilter[];
indices: {
'apm_oss.sourcemapIndices': string;
Expand All @@ -111,7 +109,7 @@ interface MockSetup {
'apm_oss.spanIndices': string;
'apm_oss.transactionIndices': string;
'apm_oss.metricsIndices': string;
'apm_oss.apmAgentConfigurationIndex': string;
apmAgentConfigurationIndex: string;
};
}

Expand Down Expand Up @@ -139,10 +137,12 @@ export async function inspectSearchParams(
internalClient: {
search: internalClientSpy
} as any,
config: {
get: () => 'myIndex' as any,
has: () => true
},
config: new Proxy(
{},
{
get: () => 'myIndex'
}
) as APMConfig,
uiFiltersES: [
{
term: { 'service.environment': 'prod' }
Expand All @@ -155,7 +155,7 @@ export async function inspectSearchParams(
'apm_oss.spanIndices': 'myIndex',
'apm_oss.transactionIndices': 'myIndex',
'apm_oss.metricsIndices': 'myIndex',
'apm_oss.apmAgentConfigurationIndex': 'myIndex'
apmAgentConfigurationIndex: 'myIndex'
},
dynamicIndexPattern: null as any
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../../apm.tsconfig.json",
"include": [
"./**/*",
"../../../plugins/apm/**/*",
"../../../typings/**/*"
],
"exclude": [
Expand Down
Loading

0 comments on commit 9b368b4

Please sign in to comment.