Skip to content

Commit

Permalink
[APM] Remove index_pattern.json and add custom field formatters (elas…
Browse files Browse the repository at this point in the history
…tic#119915)

* [APM] Remove index_pattern.json and add custom field formatters

* Fix tests

* Fix tests

* Fix tutorial
  • Loading branch information
sorenlouv authored and TinLe committed Dec 22, 2021
1 parent 80848b3 commit 0795acd
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Error details', () => {
});

describe('when error has no occurrences', () => {
it('shows empty an message', () => {
it('shows an empty message', () => {
cy.visit(
url.format({
pathname:
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/apm/ftr_e2e/cypress_start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export async function cypressStart(
) {
const config = getService('config');

const archiveName = 'apm_mappings_only_8.0.0';

const kibanaUrl = Url.format({
protocol: config.get('servers.kibana.protocol'),
hostname: config.get('servers.kibana.hostname'),
Expand Down Expand Up @@ -50,8 +48,9 @@ export async function cypressStart(
});

const esRequestTimeout = config.get('timeouts.esRequestTimeout');
const archiveName = 'apm_mappings_only_8.0.0';

console.log(`Loading ES archive "${archiveName}"`);
console.log(`Creating APM mappings`);
await esArchiverLoad(archiveName);

const spec = argv.grep as string | undefined;
Expand All @@ -66,7 +65,7 @@ export async function cypressStart(
},
});

console.log('Unloading ES archives...');
console.log('Removing APM mappings');
await esArchiverUnload(archiveName);

return res;
Expand Down
28 changes: 26 additions & 2 deletions x-pack/plugins/apm/scripts/test/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/* eslint-disable no-console */

const { times } = require('lodash');
const path = require('path');
const yargs = require('yargs');
const childProcess = require('child_process');
Expand Down Expand Up @@ -45,6 +46,11 @@ const { argv } = yargs(process.argv.slice(2))
type: 'boolean',
description: 'stop tests after the first failure',
})
.option('times', {
default: 1,
type: 'number',
description: 'Repeat the test n number of times',
})
.help();

const { server, runner, open, grep, bail, kibanaInstallDir } = argv;
Expand All @@ -63,5 +69,23 @@ const grepArg = grep ? `--grep "${grep}"` : '';
const bailArg = bail ? `--bail` : '';
const cmd = `node ../../../../scripts/${ftrScript} --config ${config} ${grepArg} ${bailArg} --kibana-install-dir '${kibanaInstallDir}'`;

console.log(`Running ${cmd}`);
childProcess.execSync(cmd, { cwd: e2eDir, stdio: 'inherit' });
console.log(`Running "${cmd}"`);

if (argv.times > 1) {
console.log(`The command will be executed ${argv.times} times`);
}

const runCounter = { succeeded: 0, failed: 0, remaining: argv.times };
times(argv.times, () => {
try {
childProcess.execSync(cmd, { cwd: e2eDir, stdio: 'inherit' });
runCounter.succeeded++;
} catch (e) {
runCounter.failed++;
}
runCounter.remaining--;

if (argv.times > 1) {
console.log(runCounter);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@

import { SavedObjectsErrorHelpers } from '../../../../../../src/core/server';
import { APM_STATIC_INDEX_PATTERN_ID } from '../../../common/index_pattern_constants';
import apmDataView from '../../tutorial/index_pattern.json';
import { hasHistoricalAgentData } from '../../routes/historical_data/has_historical_agent_data';
import { Setup } from '../../lib/helpers/setup_request';
import { APMRouteHandlerResources } from '../../routes/typings';
import { InternalSavedObjectsClient } from '../../lib/helpers/get_internal_saved_objects_client.js';
import { withApmSpan } from '../../utils/with_apm_span';
import { getApmDataViewTitle } from './get_apm_data_view_title';
import { getApmDataViewAttributes } from './get_apm_data_view_attributes';

type ApmDataViewAttributes = typeof apmDataView.attributes & {
interface ApmDataViewAttributes {
title: string;
};
}

export async function createStaticDataView({
setup,
config,
savedObjectsClient,
spaceId,
overwrite = false,
}: {
setup: Setup;
config: APMRouteHandlerResources['config'];
savedObjectsClient: InternalSavedObjectsClient;
spaceId?: string;
overwrite?: boolean;
}): Promise<boolean> {
return withApmSpan('create_static_data_view', async () => {
// don't autocreate APM data view if it's been disabled via the config
Expand All @@ -48,25 +46,22 @@ export async function createStaticDataView({
const apmDataViewTitle = getApmDataViewTitle(setup.indices);
const forceOverwrite = await getForceOverwrite({
apmDataViewTitle,
overwrite,
savedObjectsClient,
});

try {
await withApmSpan('create_index_pattern_saved_object', () =>
savedObjectsClient.create(
'index-pattern',
{
...apmDataView.attributes,
title: apmDataViewTitle,
},
getApmDataViewAttributes(apmDataViewTitle),
{
id: APM_STATIC_INDEX_PATTERN_ID,
overwrite: forceOverwrite ? true : overwrite,
overwrite: forceOverwrite,
namespace: spaceId,
}
)
);

return true;
} catch (e) {
// if the data view (saved object) already exists a conflict error (code: 409) will be thrown
Expand All @@ -82,30 +77,26 @@ export async function createStaticDataView({
// force an overwrite of the data view if the data view has been changed
async function getForceOverwrite({
savedObjectsClient,
overwrite,
apmDataViewTitle,
}: {
savedObjectsClient: InternalSavedObjectsClient;
overwrite: boolean;
apmDataViewTitle: string;
}) {
if (!overwrite) {
try {
const existingDataView =
await savedObjectsClient.get<ApmDataViewAttributes>(
'index-pattern',
APM_STATIC_INDEX_PATTERN_ID
);

// if the existing data view does not matches the new one, force an update
return existingDataView.attributes.title !== apmDataViewTitle;
} catch (e) {
// ignore exception if the data view (saved object) is not found
if (SavedObjectsErrorHelpers.isNotFoundError(e)) {
return false;
}
try {
const existingDataView =
await savedObjectsClient.get<ApmDataViewAttributes>(
'index-pattern',
APM_STATIC_INDEX_PATTERN_ID
);

throw e;
// if the existing data view does not matches the new one, force an update
return existingDataView.attributes.title !== apmDataViewTitle;
} catch (e) {
// ignore exception if the data view (saved object) is not found
if (SavedObjectsErrorHelpers.isNotFoundError(e)) {
return false;
}

throw e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import {
TRACE_ID,
TRANSACTION_ID,
} from '../../../common/elasticsearch_fieldnames';

export function getApmDataViewAttributes(title: string) {
return {
// required fields (even if empty)
title,
fieldAttrs: '{}',
fields: '[]',
runtimeFieldMap: '{}',
timeFieldName: '@timestamp',
typeMeta: '{}',

// link to APM from Discover
fieldFormatMap: JSON.stringify({
[TRACE_ID]: {
id: 'url',
params: {
urlTemplate: 'apm/link-to/trace/{{value}}',
labelTemplate: '{{value}}',
},
},
[TRANSACTION_ID]: {
id: 'url',
params: {
urlTemplate: 'apm/link-to/transaction/{{value}}',
labelTemplate: '{{value}}',
},
},
}),
};
}
12 changes: 4 additions & 8 deletions x-pack/plugins/apm/server/tutorial/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
} from '../../../../../src/plugins/home/server';
import { CloudSetup } from '../../../cloud/server';
import { APM_STATIC_INDEX_PATTERN_ID } from '../../common/index_pattern_constants';
import { getApmDataViewAttributes } from '../routes/data_view/get_apm_data_view_attributes';
import { getApmDataViewTitle } from '../routes/data_view/get_apm_data_view_title';
import { ApmIndicesConfig } from '../routes/settings/apm_indices/get_apm_indices';
import { createElasticCloudInstructions } from './envs/elastic_cloud';
import { onPremInstructions } from './envs/on_prem';
import apmDataView from './index_pattern.json';

const apmIntro = i18n.translate('xpack.apm.tutorial.introduction', {
defaultMessage:
Expand All @@ -39,16 +39,12 @@ export const tutorialProvider =
isFleetPluginEnabled: boolean;
}) =>
() => {
const indexPatternTitle = getApmDataViewTitle(apmIndices);

const dataViewTitle = getApmDataViewTitle(apmIndices);
const savedObjects = [
{
...apmDataView,
id: APM_STATIC_INDEX_PATTERN_ID,
attributes: {
...apmDataView.attributes,
title: indexPatternTitle,
},
attributes: getApmDataViewAttributes(dataViewTitle),
type: 'index-pattern',
},
];

Expand Down
11 changes: 0 additions & 11 deletions x-pack/plugins/apm/server/tutorial/index_pattern.json

This file was deleted.

Loading

0 comments on commit 0795acd

Please sign in to comment.