Skip to content

Commit

Permalink
[Cloud Security][Telemetry] add error handling incase an individual c…
Browse files Browse the repository at this point in the history
…ollector 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.
  • Loading branch information
Omolola-Akinleye authored Sep 13, 2023
1 parent 6e74efe commit 0e2775f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -35,6 +35,22 @@ export function registerCspmUsageCollector(
return true;
},
fetch: async (collectorFetchContext: CollectorFetchContext) => {
const awaitPromiseSafe = async <T>(
taskName: CloudSecurityUsageCollectorType,
promise: Promise<T>
) => {
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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down

0 comments on commit 0e2775f

Please sign in to comment.