From 4db8c10cd21a0c6e1576c0c7f703db10f92246fb Mon Sep 17 00:00:00 2001 From: criamico Date: Tue, 9 Apr 2024 12:00:07 +0200 Subject: [PATCH] [Fleet] Add apm spans for new package lifecycle states --- .../steps/step_create_restart_installation.ts | 49 +++++++++++-------- .../steps/step_delete_previous_pipelines.ts | 36 ++++++++------ .../step_install_index_template_pipelines.ts | 45 ++++++++++------- .../steps/step_remove_legacy_templates.ts | 5 +- .../steps/update_latest_executed_state.ts | 9 ++-- 5 files changed, 84 insertions(+), 60 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_create_restart_installation.ts b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_create_restart_installation.ts index 58daa6c379134..af0fb0fea9caf 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_create_restart_installation.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_create_restart_installation.ts @@ -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 { @@ -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'}-${ @@ -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, + }) + ); } } diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_delete_previous_pipelines.ts b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_delete_previous_pipelines.ts index eb80ef16dbcb0..4a524e3e07939 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_delete_previous_pipelines.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_delete_previous_pipelines.ts @@ -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 diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_install_index_template_pipelines.ts b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_install_index_template_pipelines.ts index e2b6918b722cf..18c0956da31c9 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_install_index_template_pipelines.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_install_index_template_pipelines.ts @@ -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; @@ -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, @@ -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 }; } } diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_remove_legacy_templates.ts b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_remove_legacy_templates.ts index 0c70989a67096..b93197af5e521 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_remove_legacy_templates.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/step_remove_legacy_templates.ts @@ -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}`); } diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/update_latest_executed_state.ts b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/update_latest_executed_state.ts index 55d7997ad58f7..39b2492d77994 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/update_latest_executed_state.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install_state_machine/steps/update_latest_executed_state.ts @@ -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) => { @@ -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}`);