Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions blackout_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def _fetch_blackouts(self):
blackouts = []
return blackouts

def _re_search(self, pattern, string):
"""
A simple wrapper around re.search that handles the case where the
pattern is invalid.
"""
try:
r = re.compile(pattern)
except re.error:
# consider the pattern is just a string
log.debug("Invalid regex pattern: %s", pattern)
return pattern in string
return r.search(string)

def _apply_blackout(self, alert):
"""
The regex blackouts are evaluated in the ``post_receive`` in order to
Expand Down Expand Up @@ -109,7 +122,7 @@ def _apply_blackout(self, alert):
log.debug(blackout)
match = False
if blackout.environment:
if not re.search(blackout.environment, alert.environment):
if not self._re_search(blackout.environment, alert.environment):
log.debug(
"%s doesn't match the blackout environment %s",
alert.environment,
Expand All @@ -123,7 +136,7 @@ def _apply_blackout(self, alert):
alert.environment,
)
if blackout.customer:
if not re.search(blackout.customer, alert.customer):
if not self._re_search(blackout.customer, alert.customer):
log.debug(
"%s doesn't match the blackout customer %s",
alert.customer,
Expand All @@ -137,7 +150,7 @@ def _apply_blackout(self, alert):
alert.customer,
)
if blackout.group:
if not re.search(blackout.group, alert.group):
if not self._re_search(blackout.group, alert.group):
log.debug(
"%s doesn't match the blackout group %s",
alert.group,
Expand All @@ -147,7 +160,7 @@ def _apply_blackout(self, alert):
match = True
log.debug("%s matched %s", blackout.group, alert.group)
if blackout.event:
if not re.search(blackout.event, alert.event):
if not self._re_search(blackout.event, alert.event):
log.debug(
"%s doesn't match the blackout event %s",
alert.event,
Expand All @@ -157,7 +170,7 @@ def _apply_blackout(self, alert):
match = True
log.debug("%s matched %s", blackout.event, alert.event)
if blackout.resource:
if not re.search(blackout.resource, alert.resource):
if not self._re_search(blackout.resource, alert.resource):
log.debug(
"%s doesn't match the blackout resource %s",
alert.resource,
Expand All @@ -171,7 +184,7 @@ def _apply_blackout(self, alert):
continue
if not all(
[
re.search(blackout.service[index], alert.service[index])
self._re_search(blackout.service[index], alert.service[index])
for index in range(len(alert.service))
]
):
Expand All @@ -191,7 +204,9 @@ def _apply_blackout(self, alert):
continue
if not all(
[
re.search(blackout_tags[blackout_tag], alert_tags[blackout_tag])
self._re_search(
blackout_tags[blackout_tag], alert_tags[blackout_tag]
)
for blackout_tag in blackout_tags
if blackout_tag in alert_tags
]
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tox==4.6.4
mock==5.1.0
nose==1.3.7
pytest==8.4.1
black==24.3.0
pylama==8.4.1
alerta==8.5.2
12 changes: 12 additions & 0 deletions test_blackout_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
"duration": 3600,
"id": "8",
},
{
"status": "active",
"environment": "customers",
"customer": "acme",
"tags": [],
"service": [],
"resource": r"*invalid\d",
"event": None,
"group": None,
"duration": 3600,
"id": "9",
},
]


Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ deps =
passenv = *

commands =
nosetests -v test_blackout_regex.py
pytest -v test_blackout_regex.py

[testenv:black]
deps = black==23.7.0
Expand Down