Skip to content

Commit

Permalink
[Fleet] Add apm spans for new package lifecycle states (#180355)
Browse files Browse the repository at this point in the history
## Summary

Add more APM spans in the state machine steps added in
#178657 to measure the
performances of the installation process

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
criamico and kibanamachine authored Apr 9, 2024
1 parent 6bb1282 commit 8c05e25
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MAX_TIME_COMPLETE_INSTALL } from '../../../../../constants';
import { restartInstallation, createInstallation } from '../../install';

import type { InstallContext } from '../_state_machine_package_install';
import { withPackageSpan } from '../../utils';

export async function stepCreateRestartInstallation(context: InstallContext) {
const {
Expand Down Expand Up @@ -42,13 +43,15 @@ export async function stepCreateRestartInstallation(context: InstallContext) {

if (force) {
logger.debug(`Package install - Forced installation, restarting`);
await restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
});
await withPackageSpan('Restarting installation with force flag', () =>
restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
})
);
} else {
throw new ConcurrentInstallOperationError(
`Concurrent installation or upgrade of ${pkgName || 'unknown'}-${
Expand All @@ -62,23 +65,27 @@ export async function stepCreateRestartInstallation(context: InstallContext) {
logger.debug(
`Package install - no installation running or the installation has been running longer than ${MAX_TIME_COMPLETE_INSTALL}, restarting`
);
await restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
});
await withPackageSpan('Restarting installation', () =>
restartInstallation({
savedObjectsClient,
pkgName,
pkgVersion,
installSource,
verificationResult,
})
);
}
} else {
logger.debug(`Package install - Create installation`);

await createInstallation({
savedObjectsClient,
packageInfo,
installSource,
spaceId,
verificationResult,
});
await withPackageSpan('Creating installation', () =>
createInstallation({
savedObjectsClient,
packageInfo,
installSource,
spaceId,
verificationResult,
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,30 @@ export async function stepDeletePreviousPipelines(context: InstallContext) {
installedPkg
) {
logger.debug(`Package install - installType ${installType} Deleting previous ingest pipelines`);
updatedESReferences = await withPackageSpan('Delete previous ingest pipelines', () =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.version,
esReferences || []
)
updatedESReferences = await withPackageSpan(
'Delete previous ingest pipelines with installType update or reupdate',
() =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.version,
esReferences || []
)
);
} else if (installType === 'rollback' && installedPkg) {
// pipelines from a different version may have been installed during a failed update
logger.debug(`Package install - installType ${installType} Deleting previous ingest pipelines`);
updatedESReferences = await withPackageSpan('Delete previous ingest pipelines', () =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.install_version,
esReferences || []
)
updatedESReferences = await withPackageSpan(
'Delete previous ingest pipelines with installType rollback',
() =>
deletePreviousPipelines(
esClient,
savedObjectsClient,
pkgName,
installedPkg!.attributes.install_version,
esReferences || []
)
);
} else {
// if none of the previous cases, return the original esReferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getNormalizedDataStreams } from '../../../../../../common/services';
import { installIndexTemplatesAndPipelines } from '../../install_index_template_pipeline';

import type { InstallContext } from '../_state_machine_package_install';
import { withPackageSpan } from '../../utils';

export async function stepInstallIndexTemplatePipelines(context: InstallContext) {
const { esClient, savedObjectsClient, packageInstallContext, logger, installedPkg } = context;
Expand All @@ -20,15 +21,18 @@ export async function stepInstallIndexTemplatePipelines(context: InstallContext)
logger.debug(
`Package install - Installing index templates and pipelines, packageInfo.type: ${packageInfo.type}`
);
const { installedTemplates, esReferences: templateEsReferences } =
await installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
});
const { installedTemplates, esReferences: templateEsReferences } = await withPackageSpan(
'Install index templates and pipelines with packageInfo integration',
() =>
installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
})
);
return {
esReferences: templateEsReferences,
indexTemplates: installedTemplates,
Expand All @@ -50,16 +54,19 @@ export async function stepInstallIndexTemplatePipelines(context: InstallContext)
);

if (dataStreams.length) {
const { installedTemplates, esReferences: templateEsReferences } =
await installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
onlyForDataStreams: dataStreams,
});
const { installedTemplates, esReferences: templateEsReferences } = await withPackageSpan(
'Install index templates and pipelines with packageInfo input',
() =>
installIndexTemplatesAndPipelines({
installedPkg: installedPkg ? installedPkg.attributes : undefined,
packageInstallContext,
esClient,
savedObjectsClient,
logger,
esReferences,
onlyForDataStreams: dataStreams,
})
);
return { esReferences: templateEsReferences, indexTemplates: installedTemplates };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import { removeLegacyTemplates } from '../../../elasticsearch/template/remove_legacy';

import type { InstallContext } from '../_state_machine_package_install';
import { withPackageSpan } from '../../utils';

export async function stepRemoveLegacyTemplates(context: InstallContext) {
const { esClient, packageInstallContext, logger } = context;
const { packageInfo } = packageInstallContext;
try {
await removeLegacyTemplates({ packageInfo, esClient, logger });
await withPackageSpan('Remove legacy templates', () =>
removeLegacyTemplates({ packageInfo, esClient, logger })
);
} catch (e) {
logger.warn(`Error removing legacy templates: ${e.message}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../constants';
import { auditLoggingService } from '../../../../audit_logging';

import type { InstallContext } from '../_state_machine_package_install';
import { withPackageSpan } from '../../utils';

// Function invoked after each transition
export const updateLatestExecutedState = async (context: InstallContext) => {
Expand All @@ -28,9 +29,11 @@ export const updateLatestExecutedState = async (context: InstallContext) => {
id: pkgName,
savedObjectType: PACKAGES_SAVED_OBJECT_TYPE,
});
return await savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, {
latest_executed_state: latestExecutedState,
});
return await withPackageSpan('Update latest executed state', () =>
savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, {
latest_executed_state: latestExecutedState,
})
);
} catch (err) {
if (!SavedObjectsErrorHelpers.isNotFoundError(err)) {
logger.error(`Failed to update SO with latest executed state: ${err}`);
Expand Down

0 comments on commit 8c05e25

Please sign in to comment.