From 6cdfca6af33c79b773f8acb9eb1118a3c2baba80 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Thu, 6 Jun 2019 00:10:57 -0700 Subject: [PATCH] [APM] Moves the APM index creation from server startup (#37965) * [APM] Closes #37499 by moving the APM index creation from server startup to savedObject request for the index pattern. It first checks if the index pattern is saved, if not it creates it. * [APM] provide more meaninful status codes in the default error handler --- .../apm/public/services/rest/savedObjects.ts | 29 +++++------------- .../apm/server/lib/index_pattern/index.ts | 25 +++++++++------- .../plugins/apm/server/new-platform/plugin.ts | 4 +-- .../apm/server/routes/index_pattern.ts | 30 +++++++++++++++++++ 4 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 x-pack/plugins/apm/server/routes/index_pattern.ts diff --git a/x-pack/plugins/apm/public/services/rest/savedObjects.ts b/x-pack/plugins/apm/public/services/rest/savedObjects.ts index 58d2d01fefddf..7a80918eabacd 100644 --- a/x-pack/plugins/apm/public/services/rest/savedObjects.ts +++ b/x-pack/plugins/apm/public/services/rest/savedObjects.ts @@ -5,7 +5,6 @@ */ import { memoize } from 'lodash'; -import chrome from 'ui/chrome'; import { callApi } from './callApi'; export interface ISavedObject { @@ -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({ - 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({ + method: 'GET', + pathname: `/api/apm/index_pattern` + }); + } catch (error) { + return; + } }); diff --git a/x-pack/plugins/apm/server/lib/index_pattern/index.ts b/x-pack/plugins/apm/server/lib/index_pattern/index.ts index efde52e44fc0e..cb099d11e1dda 100644 --- a/x-pack/plugins/apm/server/lib/index_pattern/index.ts +++ b/x-pack/plugins/apm/server/lib/index_pattern/index.ts @@ -5,21 +5,24 @@ */ import { InternalCoreSetup } 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: InternalCoreSetup) { +export async function getIndexPattern(core: InternalCoreSetup) { 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 } + ); + } } diff --git a/x-pack/plugins/apm/server/new-platform/plugin.ts b/x-pack/plugins/apm/server/new-platform/plugin.ts index 8ba8c8dba4861..1bcdb3c0066bc 100644 --- a/x-pack/plugins/apm/server/new-platform/plugin.ts +++ b/x-pack/plugins/apm/server/new-platform/plugin.ts @@ -6,7 +6,6 @@ import { InternalCoreSetup } 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'; @@ -14,6 +13,7 @@ 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: InternalCoreSetup) { @@ -23,7 +23,7 @@ export class Plugin { initServicesApi(core); initErrorsApi(core); initMetricsApi(core); + initIndexPatternApi(core); makeApmUsageCollector(core as CoreSetupWithUsageCollector); - ensureIndexPatternExists(core); } } diff --git a/x-pack/plugins/apm/server/routes/index_pattern.ts b/x-pack/plugins/apm/server/routes/index_pattern.ts new file mode 100644 index 0000000000000..20ae3c3363663 --- /dev/null +++ b/x-pack/plugins/apm/server/routes/index_pattern.ts @@ -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 { InternalCoreSetup } 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: InternalCoreSetup) { + const { server } = core.http; + server.route({ + method: 'GET', + path: ROOT, + options: { + tags: ['access:apm'] + }, + handler: async req => { + return await getIndexPattern(core).catch(defaultErrorHandler); + } + }); +}