From 21ba89e800567ec9a2f1fb404f02738a914e791d Mon Sep 17 00:00:00 2001 From: Omolola Akinleye Date: Wed, 6 Sep 2023 17:39:59 -0400 Subject: [PATCH 1/4] add error handling incase an individual collector fails --- .../installation_stats_collector.ts | 2 +- .../lib/telemetry/collectors/register.ts | 27 +++++++++++++------ .../server/lib/telemetry/collectors/types.ts | 2 +- 3 files changed, 21 insertions(+), 10 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 09b197245cd5a..038ed35abac97 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 c9495c03eccdb..cefc5df93ff25 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 @@ -35,14 +35,18 @@ export function registerCspmUsageCollector( return true; }, fetch: async (collectorFetchContext: CollectorFetchContext) => { - const [ - indicesStats, - accountsStats, - resourcesStats, - rulesStats, - installationStats, - alertsStats, - ] = await Promise.all([ + const handleResult = (taskName: string, result: PromiseSettledResult) => { + if (result.status === 'fulfilled') { + return result.value; + } else { + // Handle the error and log it + logger.error(`${taskName} task failed: ${result.reason}`); + + // Return null or some default value, depending on your needs + return []; + } + }; + const results = await Promise.allSettled([ getIndicesStats( collectorFetchContext.esClient, collectorFetchContext.soClient, @@ -61,6 +65,13 @@ export function registerCspmUsageCollector( getAlertsStats(collectorFetchContext.esClient, logger), ]); + const indicesStats = handleResult('Indices', results[0]); + const accountsStats = handleResult('Accounts', results[1]); + const resourcesStats = handleResult('Resources', results[2]); + const rulesStats = handleResult('Rules', results[3]); + const installationStats = handleResult('Installation', results[4]); + const alertsStats = handleResult('Alerts', results[5]); + 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 0c04de498509a..db6a986063891 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 @@ -8,7 +8,7 @@ import { CspStatusCode } from '../../../../common/types'; export interface CspmUsage { - indices: CspmIndicesStats; + indices: CspmIndicesStats | never[]; resources_stats: CspmResourcesStats[]; accounts_stats: CspmAccountsStats[]; rules_stats: CspmRulesStats[]; From d3b89907db858b36600bee2c36e4ffee0a1a0655 Mon Sep 17 00:00:00 2001 From: Omolola Akinleye Date: Mon, 11 Sep 2023 06:43:09 -0400 Subject: [PATCH 2/4] address pr comments --- .../lib/telemetry/collectors/register.ts | 23 ++++++++++--------- .../server/lib/telemetry/collectors/types.ts | 20 +++++++++++----- 2 files changed, 26 insertions(+), 17 deletions(-) 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 cefc5df93ff25..52faeb72de089 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,15 +35,16 @@ export function registerCspmUsageCollector( return true; }, fetch: async (collectorFetchContext: CollectorFetchContext) => { - const handleResult = (taskName: string, result: PromiseSettledResult) => { + const getPromiseValue = ( + taskName: CloudSecurityUsageCollectorType, + result: PromiseSettledResult + ) => { if (result.status === 'fulfilled') { return result.value; } else { - // Handle the error and log it logger.error(`${taskName} task failed: ${result.reason}`); - // Return null or some default value, depending on your needs - return []; + return undefined; } }; const results = await Promise.allSettled([ @@ -65,12 +66,12 @@ export function registerCspmUsageCollector( getAlertsStats(collectorFetchContext.esClient, logger), ]); - const indicesStats = handleResult('Indices', results[0]); - const accountsStats = handleResult('Accounts', results[1]); - const resourcesStats = handleResult('Resources', results[2]); - const rulesStats = handleResult('Rules', results[3]); - const installationStats = handleResult('Installation', results[4]); - const alertsStats = handleResult('Alerts', results[5]); + const indicesStats = getPromiseValue('Indices', results[0]); + const accountsStats = getPromiseValue('Accounts', results[1]); + const resourcesStats = getPromiseValue('Resources', results[2]); + const rulesStats = getPromiseValue('Rules', results[3]); + const installationStats = getPromiseValue('Installation', results[4]); + const alertsStats = getPromiseValue('Alerts', results[5]); return { indices: indicesStats, 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 db6a986063891..9bcdd30efaba6 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,13 +7,21 @@ import { CspStatusCode } from '../../../../common/types'; +export type CloudSecurityUsageCollectorType = + | 'Indices' + | 'Accounts' + | 'Resources' + | 'Rules' + | 'Installation' + | 'Alerts'; + export interface CspmUsage { - indices: CspmIndicesStats | never[]; - resources_stats: CspmResourcesStats[]; - accounts_stats: CspmAccountsStats[]; - rules_stats: CspmRulesStats[]; - installation_stats: CloudSecurityInstallationStats[]; - alerts_stats: CloudSecurityAlertsStats[]; + indices: CspmIndicesStats | undefined; + resources_stats: CspmResourcesStats[] | undefined; + accounts_stats: CspmAccountsStats[] | undefined; + rules_stats: CspmRulesStats[] | undefined; + installation_stats: CloudSecurityInstallationStats[] | undefined; + alerts_stats: CloudSecurityAlertsStats[] | undefined; } export interface PackageSetupStatus { From ed90ea8baaeca1daec009e3673d01042dc79815f Mon Sep 17 00:00:00 2001 From: Omolola Akinleye Date: Wed, 13 Sep 2023 04:22:54 -0400 Subject: [PATCH 3/4] address nit comment --- .../lib/telemetry/collectors/register.ts | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-) 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 52faeb72de089..21f208bde3bec 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 @@ -35,44 +35,40 @@ export function registerCspmUsageCollector( return true; }, fetch: async (collectorFetchContext: CollectorFetchContext) => { - const getPromiseValue = ( + const awaitPromiseSafe = async ( taskName: CloudSecurityUsageCollectorType, - result: PromiseSettledResult + promise: Promise ) => { - if (result.status === 'fulfilled') { - return result.value; - } else { - logger.error(`${taskName} task failed: ${result.reason}`); - + try { + const val = await promise; + return val; + } catch (error) { + logger.error(`${taskName} task failed: ${error.message}`); + logger.error(error.stack); return undefined; } }; - const results = await Promise.allSettled([ - getIndicesStats( - collectorFetchContext.esClient, - collectorFetchContext.soClient, - coreServices, - logger - ), - getAccountsStats(collectorFetchContext.esClient, logger), - getResourcesStats(collectorFetchContext.esClient, logger), - getRulesStats(collectorFetchContext.esClient, logger), - getInstallationStats( - collectorFetchContext.esClient, - collectorFetchContext.soClient, - coreServices, - logger + + const esClient = collectorFetchContext.esClient; + const soClient = collectorFetchContext.soClient; + const [ + indicesStats, + accountsStats, + resourcesStats, + rulesStats, + installationStats, + alertsStats, + ] = await Promise.all([ + 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) ), - getAlertsStats(collectorFetchContext.esClient, logger), + awaitPromiseSafe('Alerts', getAlertsStats(esClient, logger)), ]); - - const indicesStats = getPromiseValue('Indices', results[0]); - const accountsStats = getPromiseValue('Accounts', results[1]); - const resourcesStats = getPromiseValue('Resources', results[2]); - const rulesStats = getPromiseValue('Rules', results[3]); - const installationStats = getPromiseValue('Installation', results[4]); - const alertsStats = getPromiseValue('Alerts', results[5]); - return { indices: indicesStats, accounts_stats: accountsStats, From b54b3a7d90c1cac18820d94368a8e2686aa74aa2 Mon Sep 17 00:00:00 2001 From: Omolola Akinleye Date: Wed, 13 Sep 2023 09:26:44 -0400 Subject: [PATCH 4/4] type issue and return error --- .../server/lib/telemetry/collectors/register.ts | 2 +- .../server/lib/telemetry/collectors/types.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) 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 21f208bde3bec..f41497cd80bb4 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 @@ -45,7 +45,7 @@ export function registerCspmUsageCollector( } catch (error) { logger.error(`${taskName} task failed: ${error.message}`); logger.error(error.stack); - return undefined; + return error; } }; 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 9bcdd30efaba6..f1162e24d3168 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 @@ -16,12 +16,12 @@ export type CloudSecurityUsageCollectorType = | 'Alerts'; export interface CspmUsage { - indices: CspmIndicesStats | undefined; - resources_stats: CspmResourcesStats[] | undefined; - accounts_stats: CspmAccountsStats[] | undefined; - rules_stats: CspmRulesStats[] | undefined; - installation_stats: CloudSecurityInstallationStats[] | undefined; - alerts_stats: CloudSecurityAlertsStats[] | undefined; + indices: CspmIndicesStats; + resources_stats: CspmResourcesStats[]; + accounts_stats: CspmAccountsStats[]; + rules_stats: CspmRulesStats[]; + installation_stats: CloudSecurityInstallationStats[]; + alerts_stats: CloudSecurityAlertsStats[]; } export interface PackageSetupStatus {