Skip to content

Commit

Permalink
Add enrichment event log time (#141433) (#142079)
Browse files Browse the repository at this point in the history
* Add enrichment event log time

* fix types

* Fix test

* Add avg field

* Fix enrichments event log

* Add telemetry

* Update schema

Co-authored-by: Kibana Machine <[email protected]>
(cherry picked from commit 5f057ff)

Co-authored-by: Khristinin Nikita <[email protected]>
  • Loading branch information
kibanamachine and nkhristinin authored Sep 29, 2022
1 parent 60b55b6 commit 069c122
Show file tree
Hide file tree
Showing 30 changed files with 1,394 additions and 37 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/event_log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Below is a document in the expected structure, with descriptions of the fields:
es_search_duration_ms: "total time spent performing ES searches as measured by Elasticsearch",
total_search_duration_ms: "total time spent performing ES searches as measured by Kibana; includes network latency and time spent serializing/deserializing request/response",
total_indexing_duration_ms: "total time spent indexing documents during current rule execution cycle",
total_enrichment_duration_ms: "total time spent enriching documents during current rule execution cycle",
execution_gap_duration_s: "duration in seconds of execution gap"
}
}
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/event_log/generated/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@
},
"total_run_duration_ms": {
"type": "long"
},
"total_enrichment_duration_ms": {
"type": "long"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/event_log/generated/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const EventSchema = schema.maybe(
claim_to_start_duration_ms: ecsStringOrNumber(),
prepare_rule_duration_ms: ecsStringOrNumber(),
total_run_duration_ms: ecsStringOrNumber(),
total_enrichment_duration_ms: ecsStringOrNumber(),
})
),
})
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/event_log/scripts/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ exports.EcsCustomPropertyMappings = {
total_run_duration_ms: {
type: 'long',
},
total_enrichment_duration_ms: {
type: 'long',
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ export type DurationMetric = t.TypeOf<typeof DurationMetric>;
export const DurationMetric = PositiveInteger;

export type RuleExecutionMetrics = t.TypeOf<typeof RuleExecutionMetrics>;

/**
@property total_search_duration_ms - "total time spent performing ES searches as measured by Kibana;
includes network latency and time spent serializing/deserializing request/response",
@property total_indexing_duration_ms - "total time spent indexing documents during current rule execution cycle",
@property total_enrichment_duration_ms - total time spent enriching documents during current rule execution cycle
@property execution_gap_duration_s - "duration in seconds of execution gap"
*/
export const RuleExecutionMetrics = t.partial({
total_search_duration_ms: DurationMetric,
total_indexing_duration_ms: DurationMetric,
total_enrichment_duration_ms: DurationMetric,
execution_gap_duration_s: DurationMetric,
});
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const normalizeStatusChangeArgs = (args: StatusChangeArgs): NormalizedStatusChan
? {
total_search_duration_ms: normalizeDurations(metrics.searchDurations),
total_indexing_duration_ms: normalizeDurations(metrics.indexingDurations),
total_enrichment_duration_ms: normalizeDurations(metrics.enrichmentDurations),
execution_gap_duration_s: normalizeGap(metrics.executionGap),
}
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ export interface StatusChangeArgs {
export interface MetricsArgs {
searchDurations?: string[];
indexingDurations?: string[];
enrichmentDurations?: string[];
executionGap?: Duration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const ruleExecutionMappings: SavedObjectsType['mappings'] = {
total_indexing_duration_ms: {
type: 'long',
},
total_enrichment_duration_ms: {
type: 'long',
},
execution_gap_duration_s: {
type: 'long',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
const warningMessages = result.warningMessages.concat(runResult.warningMessages);
result = {
bulkCreateTimes: result.bulkCreateTimes.concat(runResult.bulkCreateTimes),
enrichmentTimes: result.enrichmentTimes.concat(runResult.enrichmentTimes),
createdSignals,
createdSignalsCount: createdSignals.length,
errors: result.errors.concat(runResult.errors),
Expand All @@ -358,6 +359,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
} else {
result = {
bulkCreateTimes: [],
enrichmentTimes: [],
createdSignals: [],
createdSignalsCount: 0,
errors: [],
Expand Down Expand Up @@ -434,6 +436,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
metrics: {
searchDurations: result.searchAfterTimes,
indexingDurations: result.bulkCreateTimes,
enrichmentDurations: result.enrichmentTimes,
},
});
}
Expand All @@ -452,6 +455,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
metrics: {
searchDurations: result.searchAfterTimes,
indexingDurations: result.bulkCreateTimes,
enrichmentDurations: result.enrichmentTimes,
},
});
}
Expand All @@ -464,6 +468,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
metrics: {
searchDurations: result.searchAfterTimes,
indexingDurations: result.bulkCreateTimes,
enrichmentDurations: result.enrichmentTimes,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
export interface GenericBulkCreateResponse<T extends BaseFieldsLatest> {
success: boolean;
bulkCreateDuration: string;
enrichmentDuration: string;
createdItemsCount: number;
createdItems: Array<AlertWithCommonFieldsLatest<T> & { _id: string; _index: string }>;
errors: string[];
Expand All @@ -45,6 +46,7 @@ export const bulkCreateFactory =
return {
errors: [],
success: true,
enrichmentDuration: '0',
bulkCreateDuration: '0',
createdItemsCount: 0,
createdItems: [],
Expand All @@ -54,6 +56,24 @@ export const bulkCreateFactory =

const start = performance.now();

let enrichmentsTimeStart = 0;
let enrichmentsTimeFinish = 0;
let enrichAlertsWrapper: typeof enrichAlerts;
if (enrichAlerts) {
enrichAlertsWrapper = async (alerts, params) => {
enrichmentsTimeStart = performance.now();
try {
const enrichedAlerts = await enrichAlerts(alerts, params);
return enrichedAlerts;
} catch (error) {
ruleExecutionLogger.error(`Enrichments failed ${error}`);
throw error;
} finally {
enrichmentsTimeFinish = performance.now();
}
};
}

const { createdAlerts, errors, alertsWereTruncated } = await alertWithPersistence(
wrappedDocs.map((doc) => ({
_id: doc._id,
Expand All @@ -62,7 +82,7 @@ export const bulkCreateFactory =
})),
refreshForBulkCreate,
maxAlerts,
enrichAlerts
enrichAlertsWrapper
);

const end = performance.now();
Expand All @@ -78,6 +98,7 @@ export const bulkCreateFactory =
return {
errors: Object.keys(errors),
success: false,
enrichmentDuration: makeFloatString(enrichmentsTimeFinish - enrichmentsTimeStart),
bulkCreateDuration: makeFloatString(end - start),
createdItemsCount: createdAlerts.length,
createdItems: createdAlerts,
Expand All @@ -88,6 +109,7 @@ export const bulkCreateFactory =
errors: [],
success: true,
bulkCreateDuration: makeFloatString(end - start),
enrichmentDuration: makeFloatString(enrichmentsTimeFinish - enrichmentsTimeStart),
createdItemsCount: createdAlerts.length,
createdItems: createdAlerts,
alertsWereTruncated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type { IRuleExecutionLogForExecutors, IRuleExecutionLogService } from '..

export interface SecurityAlertTypeReturnValue<TState extends RuleTypeState> {
bulkCreateTimes: string[];
enrichmentTimes: string[];
createdSignalsCount: number;
createdSignals: unknown[];
errors: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { SecurityAlertTypeReturnValue } from '../types';

export const createResultObject = <TState extends RuleTypeState>(state: TState) => {
const result: SecurityAlertTypeReturnValue<TState> = {
enrichmentTimes: [],
bulkCreateTimes: [],
createdSignalsCount: 0,
createdSignals: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const createThreatSignals = async ({
let results: SearchAfterAndBulkCreateReturnType = {
success: true,
warning: false,
enrichmentTimes: [],
bulkCreateTimes: [],
searchAfterTimes: [],
lastLookBackDate: null,
Expand Down
Loading

0 comments on commit 069c122

Please sign in to comment.