-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor create_alert task #3759
Changes from all commits
64fe5e1
d21a7dd
da05316
3dd5ebc
5dd5b4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -102,28 +104,16 @@ 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, | ||
) | ||
logger.debug(f"alert group {group.pk} created={group_created}") | ||
|
||
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, | ||
|
@@ -135,21 +125,39 @@ def create( | |
raw_request_data=raw_request_data, | ||
is_the_first_alert_in_group=group_created, | ||
) | ||
|
||
alert.save() | ||
logger.debug(f"alert {alert.pk} created") | ||
|
||
transaction.on_commit(partial(send_alert_create_signal.apply_async, (alert.pk,))) | ||
|
||
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) | ||
|
||
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) | ||
Comment on lines
+138
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Start escalation here |
||
|
||
if group_created: | ||
# TODO: consider moving to start_escalation_if_needed | ||
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) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need this task anymore |
||
|
||
if group_created: | ||
# all code below related to maintenance mode | ||
maintenance_uuid = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved alert creation and group creation together
TODO: consider adding transaction