Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.2] [APM] Moves the APM index creation from server startup (#37965) #38235

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 8 additions & 21 deletions x-pack/plugins/apm/public/services/rest/savedObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { memoize } from 'lodash';
import chrome from 'ui/chrome';
import { callApi } from './callApi';

export interface ISavedObject {
Expand All @@ -16,25 +15,13 @@ export interface ISavedObject {
type: string;
}

interface ISavedObjectAPIResponse {
saved_objects: ISavedObject[];
}

export const getAPMIndexPattern = memoize(async () => {
const apmIndexPatternTitle: string = chrome.getInjected(
'apmIndexPatternTitle'
);
const res = await callApi<ISavedObjectAPIResponse>({
pathname: `/api/saved_objects/_find`,
query: {
type: 'index-pattern',
search: `"${apmIndexPatternTitle}"`,
search_fields: 'title',
per_page: 200
}
});

return res.saved_objects.find(
savedObject => savedObject.attributes.title === apmIndexPatternTitle
);
try {
return await callApi<ISavedObject>({
method: 'GET',
pathname: `/api/apm/index_pattern`
});
} catch (error) {
return;
}
});
25 changes: 14 additions & 11 deletions x-pack/plugins/apm/server/lib/index_pattern/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
*/
import { CoreSetup } from 'src/core/server';
import { getSavedObjectsClient } from '../helpers/saved_objects_client';
import indexPattern from '../../../../../../src/legacy/core_plugins/kibana/server/tutorials/apm/index_pattern.json';
import apmIndexPattern from '../../../../../../src/legacy/core_plugins/kibana/server/tutorials/apm/index_pattern.json';

export async function ensureIndexPatternExists(core: CoreSetup) {
export async function getIndexPattern(core: CoreSetup) {
const { server } = core.http;
const config = server.config();
const apmIndexPatternTitle = config.get('apm_oss.indexPattern');
const savedObjectsClient = getSavedObjectsClient(server);
const savedObjects = [
{
...indexPattern,
attributes: {
...indexPattern.attributes,
try {
return await savedObjectsClient.get('index-pattern', apmIndexPattern.id);
} catch (error) {
// if GET fails, then create a new index pattern saved object
return await savedObjectsClient.create(
'index-pattern',
{
...apmIndexPattern.attributes,
title: apmIndexPatternTitle
}
}
];
await savedObjectsClient.bulkCreate(savedObjects, { overwrite: false });
},
{ id: apmIndexPattern.id, overwrite: false }
);
}
}
4 changes: 2 additions & 2 deletions x-pack/plugins/apm/server/new-platform/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

import { CoreSetup } from 'src/core/server';
import { makeApmUsageCollector } from '../lib/apm_telemetry';
import { ensureIndexPatternExists } from '../lib/index_pattern';
import { CoreSetupWithUsageCollector } from '../lib/apm_telemetry/make_apm_usage_collector';
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';
import { initUIFiltersApi } from '../routes/ui_filters';
import { initIndexPatternApi } from '../routes/index_pattern';

export class Plugin {
public setup(core: CoreSetup) {
Expand All @@ -23,7 +23,7 @@ export class Plugin {
initServicesApi(core);
initErrorsApi(core);
initMetricsApi(core);
initIndexPatternApi(core);
makeApmUsageCollector(core as CoreSetupWithUsageCollector);
ensureIndexPatternExists(core);
}
}
30 changes: 30 additions & 0 deletions x-pack/plugins/apm/server/routes/index_pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 Boom from 'boom';
import { CoreSetup } from 'src/core/server';
import { getIndexPattern } from '../lib/index_pattern';

const ROOT = '/api/apm/index_pattern';
const defaultErrorHandler = (err: Error & { status?: number }) => {
// eslint-disable-next-line
console.error(err.stack);
throw Boom.boomify(err, { statusCode: err.status || 500 });
};

export function initIndexPatternApi(core: CoreSetup) {
const { server } = core.http;
server.route({
method: 'GET',
path: ROOT,
options: {
tags: ['access:apm']
},
handler: async req => {
return await getIndexPattern(core).catch(defaultErrorHandler);
}
});
}