Skip to content

Commit

Permalink
Simplify alert creation
Browse files Browse the repository at this point in the history
  • Loading branch information
iskhakov committed Jan 26, 2024
1 parent 74b68a8 commit 64fe5e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 27 additions & 23 deletions engine/apps/alerts/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from apps.alerts import tasks
from apps.alerts.constants import TASK_DELAY_SECONDS
from apps.alerts.incident_appearance.templaters import TemplateLoader
from apps.alerts.signals import alert_group_escalation_snapshot_built
from apps.alerts.tasks.distribute_alert import send_alert_create_signal
from apps.labels.alert_group_labels import assign_labels
from common.jinja_templater import apply_jinja_template
from common.jinja_templater.apply_jinja_template import JinjaTemplateError, JinjaTemplateWarning
Expand Down Expand Up @@ -102,28 +104,15 @@ def create(
if channel_filter is None:
channel_filter = ChannelFilter.select_filter(alert_receive_channel, raw_request_data, force_route_id)

# Get or create group
group, group_created = AlertGroup.objects.get_or_create_grouping(
channel=alert_receive_channel,
channel_filter=channel_filter,
group_data=group_data,
received_at=received_at,
)

if group_created:
assign_labels(group, alert_receive_channel, raw_request_data)
group.log_records.create(type=AlertGroupLogRecord.TYPE_REGISTERED)
group.log_records.create(type=AlertGroupLogRecord.TYPE_ROUTE_ASSIGNED)

mark_as_resolved = (
enable_autoresolve and group_data.is_resolve_signal and alert_receive_channel.allow_source_based_resolving
)
if not group.resolved and mark_as_resolved:
group.resolve_by_source()

mark_as_acknowledged = group_data.is_acknowledge_signal
if not group.acknowledged and mark_as_acknowledged:
group.acknowledge_by_source()

# Create alert
alert = cls(
is_resolve_signal=group_data.is_resolve_signal,
title=title,
Expand All @@ -135,21 +124,36 @@ def create(
raw_request_data=raw_request_data,
is_the_first_alert_in_group=group_created,
)

alert.save()
send_alert_create_signal.apply_async((alert.pk,))

if group_created:
assign_labels(group, alert_receive_channel)
group.log_records.create(type=AlertGroupLogRecord.TYPE_REGISTERED)
group.log_records.create(type=AlertGroupLogRecord.TYPE_ROUTE_ASSIGNED)

if group_created or alert.group.pause_escalation:
# Build escalation snapshot if needed and start escalation
alert.group.start_escalation_if_needed(countdown=TASK_DELAY_SECONDS)

if group_created:
alert_group_escalation_snapshot_built.send(sender=cls.__class__, alert_group=alert.group)

mark_as_acknowledged = group_data.is_acknowledge_signal
if not group.acknowledged and mark_as_acknowledged:
group.acknowledge_by_source()

mark_as_resolved = (
enable_autoresolve and group_data.is_resolve_signal and alert_receive_channel.allow_source_based_resolving
)
if not group.resolved and mark_as_resolved:
group.resolve_by_source()

# Store exact alert which resolved group.
if group.resolved_by == AlertGroup.SOURCE and group.resolved_by_alert is None:
group.resolved_by_alert = alert
group.save(update_fields=["resolved_by_alert"])

if settings.DEBUG:
tasks.distribute_alert(alert.pk)
else:
transaction.on_commit(
partial(tasks.distribute_alert.apply_async, (alert.pk,), countdown=TASK_DELAY_SECONDS)
)

if group_created:
# all code below related to maintenance mode
maintenance_uuid = None
Expand Down
2 changes: 2 additions & 0 deletions engine/apps/alerts/tasks/distribute_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
def distribute_alert(alert_id):
"""
We need this task to make task processing async and to make sure the task is delivered.
This task is not used anymore, but we keep it for the tasks in the queue to be processed.
TODO: remove this task after all the tasks in the queue are processed.
"""
from apps.alerts.models import Alert

Expand Down

0 comments on commit 64fe5e1

Please sign in to comment.