From a00e5e378800d0eda047b59228a48b816541e132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Marcondes?= <55978943+cauemarcondes@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:29:42 +0100 Subject: [PATCH] [Profiling] fix bug when APM integration is no available (#165809) We've identified a buggy scenario when a user has a cluster prior 8.5 and wants to upgrade to >8.9. In this case, Profiling looks after the `elastic-cloud-apm` integration to see if Profiling had been enabled previously. But for the versions before 8.5 this integration did not exist under this name causing the set up to crash. I fixed the issue by catching the exception and returning that profiling is not enabled on the apm server, as it's not installed. Before: ``` Error: Saved object [ingest-package-policies/elastic-cloud-apm] not found at Function.createGenericNotFoundError (saved_objects_error_helpers.ts:258:28) at performGet (get.ts:80:36) at processTicksAndRejections (node:internal/process/task_queues:95:5) at SavedObjectsRepository.get (repository.ts:370:12) at SavedObjectsClient.get (saved_objects_client.ts:119:12) at PackagePolicyClientImpl.get (package_policy.ts:495:29) at validateProfilingInApmPackagePolicy (fleet_policies.ts:193:23) at async Promise.all (index 5) at setup.ts:99:31 at Router.handle (router.ts:212:30) at handler (router.ts:162:13) at exports.Manager.execute (/Users/caue.marcondes/elastic/other_kibana/node_modules/@hapi/hapi/lib ``` After: ``` { "has_setup": false, "has_data": false, "pre_8_9_1_data": false } ``` --------- Co-authored-by: Francesco Gualazzi --- .../server/lib/setup/fleet_policies.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/profiling/server/lib/setup/fleet_policies.ts b/x-pack/plugins/profiling/server/lib/setup/fleet_policies.ts index 8ffe13695cddb..ccba170b5fed9 100644 --- a/x-pack/plugins/profiling/server/lib/setup/fleet_policies.ts +++ b/x-pack/plugins/profiling/server/lib/setup/fleet_policies.ts @@ -189,17 +189,23 @@ export async function validateProfilingInApmPackagePolicy({ soClient, packagePolicyClient, }: ProfilingSetupOptions): Promise { - const apmPolicy = await getApmPolicy({ packagePolicyClient, soClient }); - - return { - policies: { - apm: { - profilingEnabled: !!( - apmPolicy && apmPolicy?.inputs[0].config?.['apm-server'].value?.profiling - ), + try { + const apmPolicy = await getApmPolicy({ packagePolicyClient, soClient }); + return { + policies: { + apm: { + profilingEnabled: !!( + apmPolicy && apmPolicy?.inputs[0].config?.['apm-server'].value?.profiling + ), + }, }, - }, - }; + }; + } catch (e) { + // In case apm integration is not available ignore the error and return as profiling is not enabled on the integration + return { + policies: { apm: { profilingEnabled: false } }, + }; + } } export async function removeProfilingFromApmPackagePolicy({