From 12b760afb787530cf83d0a8b0358e1de349cb7ce Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Tue, 28 May 2024 18:16:42 +0200 Subject: [PATCH] [APM\ Update index template names considered in the diagnostics page (#184343) closes [#184333](https://github.com/elastic/kibana/issues/184333) ## Summary This PR updates the index template names that the diagnostics page verifies the existence of. After the changes made in the [APM package](https://github.com/elastic/integrations/pull/9949), to favor APM indices and ingest pipelines configured by elasticsearch, the index templates now have a `@template` suffix. eg: `metrics-apm.service_destination.1m` is now `metrics-apm.service_destination.1m@template` image image **When no indices are found** image ### How to test - Start a local ES and kibana instance - navigate to `/apm/diagnostics/index-templates?rangeFrom=now-15m&rangeTo=now` --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../bundle/get_existing_index_templates.ts | 10 ++++--- .../get_index_templates_by_index_pattern.ts | 13 +++++---- .../diagnostics/bundle/get_indices_states.ts | 10 ++++--- .../helpers/get_apm_index_template_names.ts | 29 ++++++++++++------- .../diagnostics/index_pattern_settings.ts | 4 ++- .../tests/diagnostics/index_templates.spec.ts | 4 ++- 6 files changed, 44 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_existing_index_templates.ts b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_existing_index_templates.ts index f217f72953ca5fa..228be7028659586 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_existing_index_templates.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_existing_index_templates.ts @@ -19,10 +19,12 @@ export async function getExistingApmIndexTemplates({ }) { const apmIndexTemplateNames = getApmIndexTemplateNames(); const values = await Promise.all( - apmIndexTemplateNames.map(async (indexTemplateName) => { - const res = await getIndexTemplate(esClient, { name: indexTemplateName }); - return res.index_templates[0]; - }) + Object.values(apmIndexTemplateNames) + .flat() + .map(async (indexTemplateName) => { + const res = await getIndexTemplate(esClient, { name: indexTemplateName }); + return res.index_templates[0]; + }) ); return values.filter((v) => v !== undefined); diff --git a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_index_templates_by_index_pattern.ts b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_index_templates_by_index_pattern.ts index 0d9708344c370a6..6d87c98ea5e2230 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_index_templates_by_index_pattern.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_index_templates_by_index_pattern.ts @@ -79,12 +79,13 @@ async function getTemplatePriority(esClient: ElasticsearchClient, name: string) function getIsNonStandardIndexTemplate(templateName: string) { const apmIndexTemplateNames = getApmIndexTemplateNames(); const stackIndexTemplateNames = ['logs', 'metrics']; - const isNonStandard = [...apmIndexTemplateNames, ...stackIndexTemplateNames].every( - (apmIndexTemplateName) => { - const notMatch = templateName !== apmIndexTemplateName; - return notMatch; - } - ); + const isNonStandard = [ + ...Object.values(apmIndexTemplateNames).flat(), + ...stackIndexTemplateNames, + ].every((apmIndexTemplateName) => { + const notMatch = templateName !== apmIndexTemplateName; + return notMatch; + }); return isNonStandard; } diff --git a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_indices_states.ts b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_indices_states.ts index 8e3e25a3ebae003..aa8e2ef94d80547 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_indices_states.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/bundle/get_indices_states.ts @@ -85,8 +85,10 @@ export function validateIngestPipelineName( } const indexTemplateNames = getApmIndexTemplateNames(); - return indexTemplateNames.some( - (indexTemplateName) => - dataStream.startsWith(indexTemplateName) && ingestPipelineId.startsWith(indexTemplateName) - ); + return Object.values(indexTemplateNames) + .flat() + .some( + (indexTemplateName) => + dataStream.startsWith(indexTemplateName) && ingestPipelineId.startsWith(indexTemplateName) + ); } diff --git a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/helpers/get_apm_index_template_names.ts b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/helpers/get_apm_index_template_names.ts index 23ec0c969821ef3..2a5f421ce67d9e8 100644 --- a/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/helpers/get_apm_index_template_names.ts +++ b/x-pack/plugins/observability_solution/apm/server/routes/diagnostics/helpers/get_apm_index_template_names.ts @@ -7,6 +7,7 @@ import { IndicesGetIndexTemplateIndexTemplateItem } from '@elastic/elasticsearch/lib/api/types'; +const suffix = 'template'; export function getApmIndexTemplateNames() { const indexTemplateNames = [ 'logs-apm.app', @@ -27,22 +28,30 @@ export function getApmIndexTemplateNames() { ].map((ds) => `${ds}.${interval}`); }); - return [...indexTemplateNames, ...rollupIndexTemplateNames]; + // For retrocompatibility, it returns index template names both pre and post APM integration package v8.15.0 + return [...indexTemplateNames, ...rollupIndexTemplateNames].reduce((acc, indexTemplateName) => { + acc[indexTemplateName] = [indexTemplateName, `${indexTemplateName}@${suffix}`]; + return acc; + }, {} as Record); } export function getApmIndexTemplates( existingIndexTemplates: IndicesGetIndexTemplateIndexTemplateItem[] ) { const apmIndexTemplateNames = getApmIndexTemplateNames(); - const standardIndexTemplates = apmIndexTemplateNames.map((templateName) => { - const matchingTemplate = existingIndexTemplates.find(({ name }) => name === templateName); - - return { - name: templateName, - exists: Boolean(matchingTemplate), - isNonStandard: false, - }; - }); + const standardIndexTemplates = Object.entries(apmIndexTemplateNames).map( + ([baseTemplateName, validIndexTemplateNames]) => { + const matchingTemplate = validIndexTemplateNames.find((templateName) => + existingIndexTemplates.find(({ name }) => name === templateName) + ); + + return { + name: matchingTemplate ?? baseTemplateName, + exists: Boolean(matchingTemplate), + isNonStandard: false, + }; + } + ); const nonStandardIndexTemplates = existingIndexTemplates .filter( diff --git a/x-pack/test/apm_api_integration/tests/diagnostics/index_pattern_settings.ts b/x-pack/test/apm_api_integration/tests/diagnostics/index_pattern_settings.ts index 64749066a2e04db..d0ba7b1850d3114 100644 --- a/x-pack/test/apm_api_integration/tests/diagnostics/index_pattern_settings.ts +++ b/x-pack/test/apm_api_integration/tests/diagnostics/index_pattern_settings.ts @@ -24,7 +24,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { describe('When there is no data', () => { before(async () => { // delete APM index templates - await es.indices.deleteIndexTemplate({ name: getApmIndexTemplateNames() }); + await es.indices.deleteIndexTemplate({ + name: Object.values(getApmIndexTemplateNames()).flat(), + }); }); it('returns the built-in (non-APM) index templates`', async () => { diff --git a/x-pack/test/apm_api_integration/tests/diagnostics/index_templates.spec.ts b/x-pack/test/apm_api_integration/tests/diagnostics/index_templates.spec.ts index 2ece2835b594448..5c94de56abb30d9 100644 --- a/x-pack/test/apm_api_integration/tests/diagnostics/index_templates.spec.ts +++ b/x-pack/test/apm_api_integration/tests/diagnostics/index_templates.spec.ts @@ -24,7 +24,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { describe('When there is no data', () => { before(async () => { // delete APM index templates - await es.indices.deleteIndexTemplate({ name: getApmIndexTemplateNames() }); + await es.indices.deleteIndexTemplate({ + name: Object.values(getApmIndexTemplateNames()).flat(), + }); }); it('verifies that none of the default APM index templates exists`', async () => {