elastic / detection-rules

https://www.elastic.co/guide/en/security/current/detection-engine-overview.html
Other
1.9k stars 484 forks source link

`RuleLoader` cleanup and optimizations #2609

Open brokensound77 opened 1 year ago

brokensound77 commented 1 year ago

Overview

The purpose of this is to identify opportunities to clean up the code that makes up the rule loader (Rule, rule_validators, etc.). Loading the rules has gotten significantly slower and while some of it is due to the necessity of expanding validation, this should explore opportunities for optimization.

rule loader profiling
![image](https://user-images.githubusercontent.com/16747370/222036895-07c5ff00-a5d5-4aa8-9c1f-bce160dbffc7.png)

Observations

terrancedejesus commented 1 year ago

An example of something related is the "validate against ECS/Beats/Non-ECS.json AND THEN validate against integrations schema" logic. Related: https://github.com/elastic/detection-rules/pull/2627

A stop-gap may be to add a small patch to this validation logic. In the meantime any integration rule that uses EQL can have the integration specific fields added to the non-ecs file.

botelastic[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

botelastic[bot] commented 1 year ago

This has been closed due to inactivity. If you feel this is an error, please re-open and include a justifying comment.

Mikaayenson commented 8 months ago

Screenshot 2024-01-02 at 1 59 34 PM

Mikaayenson commented 8 months ago

Adding a couple skips in key places throughout the code where we looped through ALL stack versions or ALL integration versions, etc, essentially limiting validation to the latest versions.

if os.environ["DR_FAST"]:
    break

Toggling the environment variable, I consistently saw faster speeds, which is expected since we're NOT traversing EVERY version.

(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ time python test.py 

real    0m34.850s
user    0m34.218s
sys     0m0.608s
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ export DR_FAST=false
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ time python test.py 

real    3m32.382s
user    3m31.080s
sys     0m0.912s
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ export DR_FAST=false
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ export DR_FAST=true
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ time python test.py 

real    0m35.043s
user    0m34.382s
sys     0m0.589s
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ export DR_FAST=false
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$ time python test.py 

real    3m33.100s
user    3m32.106s
sys     0m0.928s
(venv) ubuntu@trade-linux-testing:~/detection-rules-main$