From 0e2775f678918bf45b6ef2f8ef5bf36942c92a78 Mon Sep 17 00:00:00 2001 From: Lola Date: Wed, 13 Sep 2023 10:35:50 -0400 Subject: [PATCH] [Cloud Security][Telemetry] add error handling incase an individual collector fails (#165918) ## Summary Summarize your PR. If it involves visual changes include a screenshot or gif. The kibana `cloud security` usage collector payload fails when one feature collector fails. I applied error handling and enabled the feature collector to fail gracefully with a default value. We are now using `Promise.allSettled` vs `Promse.all` to avoid all collectors failing in case one collector fails. --- .../installation_stats_collector.ts | 2 +- .../lib/telemetry/collectors/register.ts | 42 +++++++++++-------- .../server/lib/telemetry/collectors/types.ts | 8 ++++ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/installation_stats_collector.ts b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/installation_stats_collector.ts index 09b197245cd5a4..038ed35abac97d 100644 --- a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/installation_stats_collector.ts +++ b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/installation_stats_collector.ts @@ -25,7 +25,7 @@ const getEnabledInputStreamVars = (packagePolicy: PackagePolicy) => { const getAccountTypeField = ( packagePolicy: PackagePolicy ): CloudSecurityInstallationStats['account_type'] => { - if (packagePolicy.vars?.posture.value !== 'cspm') return; + if (packagePolicy.vars?.posture?.value !== 'cspm') return; const inputStreamVars = getEnabledInputStreamVars(packagePolicy); const cloudProvider = packagePolicy.vars?.deployment?.value; diff --git a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/register.ts b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/register.ts index c9495c03eccdb9..f41497cd80bb4e 100644 --- a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/register.ts +++ b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/register.ts @@ -11,7 +11,7 @@ import { CspServerPluginStart, CspServerPluginStartDeps } from '../../../types'; import { getIndicesStats } from './indices_stats_collector'; import { getResourcesStats } from './resources_stats_collector'; import { cspmUsageSchema } from './schema'; -import { CspmUsage } from './types'; +import { CspmUsage, type CloudSecurityUsageCollectorType } from './types'; import { getAccountsStats } from './accounts_stats_collector'; import { getRulesStats } from './rules_stats_collector'; import { getInstallationStats } from './installation_stats_collector'; @@ -35,6 +35,22 @@ export function registerCspmUsageCollector( return true; }, fetch: async (collectorFetchContext: CollectorFetchContext) => { + const awaitPromiseSafe = async ( + taskName: CloudSecurityUsageCollectorType, + promise: Promise + ) => { + try { + const val = await promise; + return val; + } catch (error) { + logger.error(`${taskName} task failed: ${error.message}`); + logger.error(error.stack); + return error; + } + }; + + const esClient = collectorFetchContext.esClient; + const soClient = collectorFetchContext.soClient; const [ indicesStats, accountsStats, @@ -43,24 +59,16 @@ export function registerCspmUsageCollector( installationStats, alertsStats, ] = await Promise.all([ - getIndicesStats( - collectorFetchContext.esClient, - collectorFetchContext.soClient, - coreServices, - logger + awaitPromiseSafe('Indices', getIndicesStats(esClient, soClient, coreServices, logger)), + awaitPromiseSafe('Accounts', getAccountsStats(esClient, logger)), + awaitPromiseSafe('Resources', getResourcesStats(esClient, logger)), + awaitPromiseSafe('Rules', getRulesStats(esClient, logger)), + awaitPromiseSafe( + 'Installation', + getInstallationStats(esClient, soClient, coreServices, logger) ), - getAccountsStats(collectorFetchContext.esClient, logger), - getResourcesStats(collectorFetchContext.esClient, logger), - getRulesStats(collectorFetchContext.esClient, logger), - getInstallationStats( - collectorFetchContext.esClient, - collectorFetchContext.soClient, - coreServices, - logger - ), - getAlertsStats(collectorFetchContext.esClient, logger), + awaitPromiseSafe('Alerts', getAlertsStats(esClient, logger)), ]); - return { indices: indicesStats, accounts_stats: accountsStats, diff --git a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/types.ts b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/types.ts index 0c04de498509a5..f1162e24d3168d 100644 --- a/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/types.ts +++ b/x-pack/plugins/cloud_security_posture/server/lib/telemetry/collectors/types.ts @@ -7,6 +7,14 @@ import { CspStatusCode } from '../../../../common/types'; +export type CloudSecurityUsageCollectorType = + | 'Indices' + | 'Accounts' + | 'Resources' + | 'Rules' + | 'Installation' + | 'Alerts'; + export interface CspmUsage { indices: CspmIndicesStats; resources_stats: CspmResourcesStats[];