Skip to content

Commit

Permalink
[APM\ Update index template names considered in the diagnostics page (e…
Browse files Browse the repository at this point in the history
…lastic#184343)

closes [elastic#184333](elastic#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](elastic/integrations#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`

<img width="709" alt="image"
src="https://github.com/elastic/kibana/assets/2767137/4608da37-679a-411c-85c4-409b2af62cec">

<img width="709" alt="image"
src="https://github.com/elastic/kibana/assets/2767137/837c8ae0-2c92-4763-811c-22fc619ba5f6">

**When no indices are found**
<img width="709" alt="image"
src="https://github.com/elastic/kibana/assets/2767137/33d7264a-dc90-49a0-a3f4-f169dcbda861">


### 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>
  • Loading branch information
2 people authored and rshen91 committed May 29, 2024
1 parent c7bdb35 commit 12b760a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { IndicesGetIndexTemplateIndexTemplateItem } from '@elastic/elasticsearch/lib/api/types';

const suffix = 'template';
export function getApmIndexTemplateNames() {
const indexTemplateNames = [
'logs-apm.app',
Expand All @@ -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<string, string[]>);
}

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 12b760a

Please sign in to comment.