-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement writing rule execution events to event_log (#112286)
- Loading branch information
Showing
45 changed files
with
653 additions
and
797 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ty_solution/server/lib/detection_engine/rule_execution_log/event_log_adapter/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const RULE_EXECUTION_LOG_PROVIDER = 'rule-execution.security'; | ||
|
||
export const ALERT_SAVED_OBJECT_TYPE = 'alert'; | ||
|
||
export enum RuleExecutionLogAction { | ||
'status-change' = 'status-change', | ||
'execution-metrics' = 'execution-metrics', | ||
} |
87 changes: 87 additions & 0 deletions
87
...ion/server/lib/detection_engine/rule_execution_log/event_log_adapter/event_log_adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { IEventLogService } from '../../../../../../event_log/server'; | ||
import { | ||
FindBulkExecutionLogArgs, | ||
FindExecutionLogArgs, | ||
IRuleExecutionLogClient, | ||
LogExecutionMetricsArgs, | ||
LogStatusChangeArgs, | ||
UpdateExecutionLogArgs, | ||
} from '../types'; | ||
import { EventLogClient } from './event_log_client'; | ||
|
||
export class EventLogAdapter implements IRuleExecutionLogClient { | ||
private eventLogClient: EventLogClient; | ||
|
||
constructor(eventLogService: IEventLogService) { | ||
this.eventLogClient = new EventLogClient(eventLogService); | ||
} | ||
|
||
public async find({ ruleId, logsCount = 1, spaceId }: FindExecutionLogArgs) { | ||
return []; // TODO Implement | ||
} | ||
|
||
public async findBulk({ ruleIds, logsCount = 1, spaceId }: FindBulkExecutionLogArgs) { | ||
return {}; // TODO Implement | ||
} | ||
|
||
public async update({ attributes, spaceId, ruleName, ruleType }: UpdateExecutionLogArgs) { | ||
// execution events are immutable, so we just log a status change istead of updating previous | ||
if (attributes.status) { | ||
this.eventLogClient.logStatusChange({ | ||
ruleName, | ||
ruleType, | ||
ruleId: attributes.alertId, | ||
newStatus: attributes.status, | ||
spaceId, | ||
}); | ||
} | ||
} | ||
|
||
public async delete(id: string) { | ||
// execution events are immutable, nothing to do here | ||
} | ||
|
||
public async logExecutionMetrics({ | ||
ruleId, | ||
spaceId, | ||
ruleType, | ||
ruleName, | ||
metrics, | ||
}: LogExecutionMetricsArgs) { | ||
this.eventLogClient.logExecutionMetrics({ | ||
ruleId, | ||
ruleName, | ||
ruleType, | ||
spaceId, | ||
metrics: { | ||
executionGapDuration: metrics.executionGap?.asSeconds(), | ||
totalIndexingDuration: metrics.indexingDurations?.reduce( | ||
(acc, cur) => acc + Number(cur), | ||
0 | ||
), | ||
totalSearchDuration: metrics.searchDurations?.reduce((acc, cur) => acc + Number(cur), 0), | ||
}, | ||
}); | ||
} | ||
|
||
public async logStatusChange(args: LogStatusChangeArgs) { | ||
if (args.metrics) { | ||
this.logExecutionMetrics({ | ||
ruleId: args.ruleId, | ||
ruleName: args.ruleName, | ||
ruleType: args.ruleType, | ||
spaceId: args.spaceId, | ||
metrics: args.metrics, | ||
}); | ||
} | ||
|
||
this.eventLogClient.logStatusChange(args); | ||
} | ||
} |
Oops, something went wrong.