Skip to content

Commit

Permalink
[APM] refactoring sourcemap api to receive form-data (elastic#103152)
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes authored Jun 24, 2021
1 parent 8a422fd commit 5a76c84
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
45 changes: 42 additions & 3 deletions x-pack/plugins/apm/server/lib/fleet/source_maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,51 @@ const artifacts = [

describe('Source maps', () => {
describe('getPackagePolicyWithSourceMap', () => {
it('returns unchanged package policy when artifacts is empty', () => {
it('removes source map from package policy', () => {
const packagePolicyWithSourceMaps = {
...packagePolicy,
inputs: [
{
...packagePolicy.inputs[0],
compiled_input: {
'apm-server': {
...packagePolicy.inputs[0].compiled_input['apm-server'],
value: {
rum: {
source_mapping: {
metadata: [
{
'service.name': 'service_name',
'service.version': '1.0.0',
'bundle.filepath':
'http://localhost:3000/static/js/main.chunk.js',
'sourcemap.url':
'/api/fleet/artifacts/service_name-1.0.0/my-id-1',
},
{
'service.name': 'service_name',
'service.version': '2.0.0',
'bundle.filepath':
'http://localhost:3000/static/js/main.chunk.js',
'sourcemap.url':
'/api/fleet/artifacts/service_name-2.0.0/my-id-2',
},
],
},
},
},
},
},
},
],
};
const updatedPackagePolicy = getPackagePolicyWithSourceMap({
packagePolicy,
packagePolicy: packagePolicyWithSourceMaps,
artifacts: [],
});
expect(updatedPackagePolicy).toEqual(packagePolicy);
expect(updatedPackagePolicy.inputs[0].config).toEqual({
'apm-server': { value: { rum: { source_mapping: { metadata: [] } } } },
});
});
it('adds source maps into the package policy', () => {
const updatedPackagePolicy = getPackagePolicyWithSourceMap({
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/apm/server/lib/fleet/source_maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ export function getPackagePolicyWithSourceMap({
packagePolicy: PackagePolicy;
artifacts: ArtifactSourceMap[];
}) {
if (!artifacts.length) {
return packagePolicy;
}
const [firstInput, ...restInputs] = packagePolicy.inputs;
return {
...packagePolicy,
Expand Down
30 changes: 18 additions & 12 deletions x-pack/plugins/apm/server/routes/source_maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/
import Boom from '@hapi/boom';
import { jsonRt } from '@kbn/io-ts-utils';
import * as t from 'io-ts';
import { SavedObjectsClientContract } from 'kibana/server';
import {
Expand Down Expand Up @@ -34,7 +35,7 @@ export const sourceMapRt = t.intersection([
const listSourceMapRoute = createApmServerRoute({
endpoint: 'GET /api/apm/sourcemaps',
options: { tags: ['access:apm'] },
handler: async ({ plugins, logger }) => {
handler: async ({ plugins }) => {
try {
const fleetPluginStart = await plugins.fleet?.start();
if (fleetPluginStart) {
Expand All @@ -51,21 +52,26 @@ const listSourceMapRoute = createApmServerRoute({
});

const uploadSourceMapRoute = createApmServerRoute({
endpoint: 'POST /api/apm/sourcemaps/{serviceName}/{serviceVersion}',
options: { tags: ['access:apm', 'access:apm_write'] },
endpoint: 'POST /api/apm/sourcemaps',
options: {
tags: ['access:apm', 'access:apm_write'],
body: { accepts: ['multipart/form-data'] },
},
params: t.type({
path: t.type({
serviceName: t.string,
serviceVersion: t.string,
}),
body: t.type({
bundleFilepath: t.string,
sourceMap: sourceMapRt,
service_name: t.string,
service_version: t.string,
bundle_filepath: t.string,
sourcemap: jsonRt.pipe(sourceMapRt),
}),
}),
handler: async ({ params, plugins, core }) => {
const { serviceName, serviceVersion } = params.path;
const { bundleFilepath, sourceMap } = params.body;
const {
service_name: serviceName,
service_version: serviceVersion,
bundle_filepath: bundleFilepath,
sourcemap: sourceMap,
} = params.body;
const fleetPluginStart = await plugins.fleet?.start();
const coreStart = await core.start();
const esClient = coreStart.elasticsearch.client.asInternalUser;
Expand Down Expand Up @@ -107,7 +113,7 @@ const deleteSourceMapRoute = createApmServerRoute({
id: t.string,
}),
}),
handler: async ({ context, params, plugins, core }) => {
handler: async ({ params, plugins, core }) => {
const fleetPluginStart = await plugins.fleet?.start();
const { id } = params.path;
const coreStart = await core.start();
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/server/routes/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface APMRouteCreateOptions {
| 'access:ml:canGetJobs'
| 'access:ml:canCreateJob'
>;
body?: { accepts: Array<'application/json' | 'multipart/form-data'> };
};
}

Expand Down

0 comments on commit 5a76c84

Please sign in to comment.