Skip to content

Commit

Permalink
prevent spike rule crash when reference time window is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jertel committed Jan 23, 2025
1 parent 667be2b commit ba3a3d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Upgrade dependency tencentcloud-sdk-python to 3.0.1295 - [#1599](https://github.com/jertel/elastalert2/pull/1599) - @jertel
- Upgrade dependency twilio to 9.4.1 - [#1599](https://github.com/jertel/elastalert2/pull/1599) - @jertel
- [Spike] Fixes spike rule error when no data exists in the current time window - [#1605](https://github.com/jertel/elastalert2/pull/1605) - @jertel
- [Spike] Fixes spike rule error when no data exists in the reference time window - [#1610](https://github.com/jertel/elastalert2/pull/1610) - @jertel

# 2.22.0

Expand Down
2 changes: 1 addition & 1 deletion elastalert/ruletypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def add_match(self, match, qk):
def find_matches(self, ref, cur):
""" Determines if an event spike or dip happening. """
# Apply threshold limits
if self.field_value is None and cur is not None:
if self.field_value is None and cur is not None and ref is not None:
if (cur < self.rules.get('threshold_cur', 0) or
ref < self.rules.get('threshold_ref', 0)):
return False
Expand Down
13 changes: 13 additions & 0 deletions tests/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ def test_spike_no_data():
assert not result


def test_spike_no_ref_data():
rules = {'threshold_ref': 10,
'spike_height': 2,
'timeframe': datetime.timedelta(seconds=10),
'spike_type': 'both',
'timestamp_field': '@timestamp',
'query_key': 'foo.bar.baz',
'field_value': None}
rule = SpikeRule(rules)
result = rule.find_matches(None, 1)
assert not result


def test_spike():
# Events are 1 per second
events = hits(100, timestamp_field='ts')
Expand Down

0 comments on commit ba3a3d1

Please sign in to comment.