Skip to content

Commit b64604a

Browse files
authored
Implement hybrid approach to writing rule execution event logs (#114852)
1 parent 95e8595 commit b64604a

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

x-pack/plugins/security_solution/server/lib/detection_engine/rule_execution_log/event_log_adapter/event_log_adapter.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* 2.0.
66
*/
77

8+
import { sum } from 'lodash';
9+
import { SavedObjectsClientContract } from '../../../../../../../../src/core/server';
810
import { IEventLogService } from '../../../../../../event_log/server';
11+
import { SavedObjectsAdapter } from '../saved_objects_adapter/saved_objects_adapter';
912
import {
1013
FindBulkExecutionLogArgs,
1114
FindExecutionLogArgs,
@@ -18,21 +21,32 @@ import { EventLogClient } from './event_log_client';
1821

1922
export class EventLogAdapter implements IRuleExecutionLogClient {
2023
private eventLogClient: EventLogClient;
24+
/**
25+
* @deprecated Saved objects adapter is used during the transition period while the event log doesn't support all features needed to implement the execution log.
26+
* We use savedObjectsAdapter to write/read the latest rule execution status and eventLogClient to read/write historical execution data.
27+
* We can remove savedObjectsAdapter as soon as the event log supports all methods that we need (find, findBulk).
28+
*/
29+
private savedObjectsAdapter: IRuleExecutionLogClient;
2130

22-
constructor(eventLogService: IEventLogService) {
31+
constructor(eventLogService: IEventLogService, savedObjectsClient: SavedObjectsClientContract) {
2332
this.eventLogClient = new EventLogClient(eventLogService);
33+
this.savedObjectsAdapter = new SavedObjectsAdapter(savedObjectsClient);
2434
}
2535

26-
public async find({ ruleId, logsCount = 1, spaceId }: FindExecutionLogArgs) {
27-
return []; // TODO Implement
36+
public async find(args: FindExecutionLogArgs) {
37+
return this.savedObjectsAdapter.find(args);
2838
}
2939

30-
public async findBulk({ ruleIds, logsCount = 1, spaceId }: FindBulkExecutionLogArgs) {
31-
return {}; // TODO Implement
40+
public async findBulk(args: FindBulkExecutionLogArgs) {
41+
return this.savedObjectsAdapter.findBulk(args);
3242
}
3343

34-
public async update({ attributes, spaceId, ruleName, ruleType }: UpdateExecutionLogArgs) {
35-
// execution events are immutable, so we just log a status change istead of updating previous
44+
public async update(args: UpdateExecutionLogArgs) {
45+
const { attributes, spaceId, ruleName, ruleType } = args;
46+
47+
await this.savedObjectsAdapter.update(args);
48+
49+
// EventLog execution events are immutable, so we just log a status change istead of updating previous
3650
if (attributes.status) {
3751
this.eventLogClient.logStatusChange({
3852
ruleName,
@@ -45,33 +59,35 @@ export class EventLogAdapter implements IRuleExecutionLogClient {
4559
}
4660

4761
public async delete(id: string) {
48-
// execution events are immutable, nothing to do here
62+
await this.savedObjectsAdapter.delete(id);
63+
64+
// EventLog execution events are immutable, nothing to do here
4965
}
5066

51-
public async logExecutionMetrics({
52-
ruleId,
53-
spaceId,
54-
ruleType,
55-
ruleName,
56-
metrics,
57-
}: LogExecutionMetricsArgs) {
67+
public async logExecutionMetrics(args: LogExecutionMetricsArgs) {
68+
const { ruleId, spaceId, ruleType, ruleName, metrics } = args;
69+
await this.savedObjectsAdapter.logExecutionMetrics(args);
70+
5871
this.eventLogClient.logExecutionMetrics({
5972
ruleId,
6073
ruleName,
6174
ruleType,
6275
spaceId,
6376
metrics: {
6477
executionGapDuration: metrics.executionGap?.asSeconds(),
65-
totalIndexingDuration: metrics.indexingDurations?.reduce(
66-
(acc, cur) => acc + Number(cur),
67-
0
68-
),
69-
totalSearchDuration: metrics.searchDurations?.reduce((acc, cur) => acc + Number(cur), 0),
78+
totalIndexingDuration: metrics.indexingDurations
79+
? sum(metrics.indexingDurations.map(Number))
80+
: undefined,
81+
totalSearchDuration: metrics.searchDurations
82+
? sum(metrics.searchDurations.map(Number))
83+
: undefined,
7084
},
7185
});
7286
}
7387

7488
public async logStatusChange(args: LogStatusChangeArgs) {
89+
await this.savedObjectsAdapter.logStatusChange(args);
90+
7591
if (args.metrics) {
7692
this.logExecutionMetrics({
7793
ruleId: args.ruleId,

x-pack/plugins/security_solution/server/lib/detection_engine/rule_execution_log/rule_execution_log_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class RuleExecutionLogClient implements IRuleExecutionLogClient {
3838
this.client = new SavedObjectsAdapter(savedObjectsClient);
3939
break;
4040
case UnderlyingLogClient.eventLog:
41-
this.client = new EventLogAdapter(eventLogService);
41+
this.client = new EventLogAdapter(eventLogService, savedObjectsClient);
4242
break;
4343
}
4444
}

0 commit comments

Comments
 (0)