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..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 @@ -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 0c04de498509a..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 @@ -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[];