Remove excessive usage of attachments

This commit is contained in:
Ildar Iskhakov 2022-08-09 21:50:22 +03:00
parent 3933c225d9
commit 2ddc20b1f3
6 changed files with 28 additions and 65 deletions

View file

@ -481,10 +481,8 @@ class AttachGroupStep(
alert_group = log_record.alert_group
if log_record.type == AlertGroupLogRecord.TYPE_ATTACHED and log_record.alert_group.is_maintenance_incident:
attachments = [
{"callback_id": "alert", "text": "{}".format(log_record.rendered_log_line_action(for_slack=True))},
]
self._publish_message_to_thread(alert_group, attachments)
text = f"{log_record.rendered_log_line_action(for_slack=True)}"
self.publish_message_to_thread(alert_group, text=text)
if log_record.type == AlertGroupLogRecord.TYPE_FAILED_ATTACHMENT:
ephemeral_text = log_record.rendered_log_line_action(for_slack=True)
@ -630,9 +628,9 @@ class CustomButtonProcessStep(
f"according to escalation policy with the result `{result_message}`"
)
attachments = [
{"callback_id": "alert", "text": debug_message, "footer": text},
{"callback_id": "alert", "text": debug_message},
]
self._publish_message_to_thread(alert_group, attachments)
self.publish_message_to_thread(alert_group, attachments=attachments, text=text)
class ResolveGroupStep(
@ -764,23 +762,27 @@ class UnAcknowledgeGroupStep(
message_attachments = [
{
"callback_id": "alert",
"text": f"{user_verbal} hasn't responded to an acknowledge timeout reminder."
f" Incident is unacknowledged automatically",
"text": "",
"footer": "Escalation started again...",
},
]
text = (
f"{user_verbal} hasn't responded to an acknowledge timeout reminder."
f" Incident is unacknowledged automatically"
)
if alert_group.slack_message.ack_reminder_message_ts:
try:
self._slack_client.api_call(
"chat.update",
channel=channel_id,
ts=alert_group.slack_message.ack_reminder_message_ts,
text=text,
attachments=message_attachments,
)
except SlackAPIException as e:
# post to thread if ack reminder message was deleted in Slack
if e.response["error"] == "message_not_found":
self._publish_message_to_thread(alert_group, message_attachments)
self.publish_message_to_thread(alert_group, attachments=message_attachments, text=text)
elif e.response["error"] == "account_inactive":
logger.info(
f"Skip unacknowledge slack message for alert_group {alert_group.pk} due to account_inactive"
@ -788,7 +790,7 @@ class UnAcknowledgeGroupStep(
else:
raise
else:
self._publish_message_to_thread(alert_group, message_attachments)
self.publish_message_to_thread(alert_group, attachments=message_attachments, text=text)
self._update_slack_message(alert_group)
logger.debug(f"Finished process_signal in UnAcknowledgeGroupStep for alert_group {alert_group.pk}")
@ -807,18 +809,12 @@ class AcknowledgeConfirmationStep(AcknowledgeGroupStep):
if alert_group.acknowledged_by == AlertGroup.USER:
if self.user == alert_group.acknowledged_by_user:
user_verbal = alert_group.acknowledged_by_user.get_user_verbal_for_team_for_slack()
attachments = [
{
"color": "#c6c000",
"callback_id": "alert",
"text": f"{user_verbal} is confirmed to be working on this incident",
},
]
text = f"{user_verbal} confirmed that the incident is still acknowledged"
self._slack_client.api_call(
"chat.update",
channel=channel,
ts=message_ts,
attachments=attachments,
text=text,
)
alert_group.acknowledged_by_confirmed = datetime.utcnow()
alert_group.save(update_fields=["acknowledged_by_confirmed"])
@ -831,18 +827,12 @@ class AcknowledgeConfirmationStep(AcknowledgeGroupStep):
)
elif alert_group.acknowledged_by == AlertGroup.SOURCE:
user_verbal = self.user.get_user_verbal_for_team_for_slack()
attachments = [
{
"color": "#c6c000",
"callback_id": "alert",
"text": f"{user_verbal} is confirmed to be working on this incident",
},
]
text = f"{user_verbal} confirmed that the incident is still acknowledged"
self._slack_client.api_call(
"chat.update",
channel=channel,
ts=message_ts,
attachments=attachments,
text=text,
)
alert_group.acknowledged_by_confirmed = datetime.utcnow()
alert_group.save(update_fields=["acknowledged_by_confirmed"])
@ -935,14 +925,8 @@ class AcknowledgeConfirmationStep(AcknowledgeGroupStep):
alert_group.slack_message.ack_reminder_message_ts = response["ts"]
alert_group.slack_message.save(update_fields=["ack_reminder_message_ts"])
else:
attachments = [
{
"callback_id": "alert",
"text": f"This is a reminder that the incident is still acknowledged by {user_verbal}"
f" and not resolved.",
},
]
self._publish_message_to_thread(alert_group, attachments)
text = f"This is a reminder that the incident is still acknowledged by {user_verbal}"
self.publish_message_to_thread(alert_group, text=text)
class WipeGroupStep(scenario_step.ScenarioStep):
@ -956,15 +940,8 @@ class WipeGroupStep(scenario_step.ScenarioStep):
def process_signal(self, log_record):
alert_group = log_record.alert_group
user_verbal = log_record.author.get_user_verbal_for_team_for_slack()
attachments = [
{
"color": "warning",
"callback_id": "alert",
"footer": "Incident wiped",
"text": "Wiped by {}.".format(user_verbal),
},
]
self._publish_message_to_thread(alert_group, attachments)
text = f"Wiped by {user_verbal}"
self.publish_message_to_thread(alert_group, text=text)
self._update_slack_message(alert_group)
@ -1145,6 +1122,7 @@ class UpdateLogReportMessageStep(scenario_step.ScenarioStep):
self._slack_client.api_call(
"chat.update",
channel=slack_message.channel_id,
text="Alert Group log",
ts=slack_log_message.slack_id,
attachments=attachments,
)

View file

@ -34,14 +34,3 @@ class EscalationDeliveryStep(scenario_step.ScenarioStep):
user_mention_as = user_verbal
notify_by = " by {}".format(UserNotificationPolicy.NotificationChannel(notification_channel).label)
return "Inviting {}{} to look at incident.".format(user_mention_as, notify_by)
def notify_thread_about_action(self, alert_group, text, footer=None, color=None):
attachments = [
{
"callback_id": "alert",
"footer": footer,
"text": text,
"color": color,
},
]
self._publish_message_to_thread(alert_group, attachments)

View file

@ -287,7 +287,7 @@ class ScenarioStep(object):
raise e
logger.info(f"Finished _update_slack_message for alert_group {alert_group.pk}")
def _publish_message_to_thread(self, alert_group, attachments, mrkdwn=True, unfurl_links=True):
def publish_message_to_thread(self, alert_group, attachments=[], mrkdwn=True, unfurl_links=True, text=None):
# TODO: refactor checking the possibility of sending message to slack
# do not try to post message to slack if integration is rate limited
if alert_group.channel.is_rate_limited_in_slack:
@ -300,6 +300,7 @@ class ScenarioStep(object):
result = self._slack_client.api_call(
"chat.postMessage",
channel=channel_id,
text=text,
attachments=attachments,
thread_ts=slack_message.slack_id,
mrkdwn=mrkdwn,

View file

@ -16,7 +16,7 @@ class AlertGroupLogSlackRenderer:
attachments = []
# get rendered logs
result = "Alert Group log:\n\n"
result = ""
for log_record in all_log_records: # list of AlertGroupLogRecord and UserNotificationPolicyLogRecord logs
if type(log_record) == AlertGroupLogRecord:
result += f"{log_record.rendered_incident_log_line(for_slack=True)}\n"

View file

@ -98,9 +98,10 @@ def check_slack_message_exists_before_post_message_to_thread(
slack_message = alert_group.get_slack_message()
if slack_message is not None:
EscalationDeliveryStep(slack_team_identity, alert_group.channel.organization).notify_thread_about_action(
alert_group, text
EscalationDeliveryStep(slack_team_identity, alert_group.channel.organization).publish_message_to_thread(
alert_group, text=text
)
# check how much time has passed since alert group was created
# to prevent eternal loop of restarting check_slack_message_before_post_message_to_thread
elif timezone.now() < alert_group.started_at + timezone.timedelta(hours=retry_timeout_hours):
@ -239,12 +240,7 @@ def send_message_to_thread_if_bot_not_in_channel(alert_group_pk, slack_team_iden
members = slack_team_identity.get_conversation_members(sc, channel_id)
if bot_user_id not in members:
text = f"Please invite <@{bot_user_id}> to this channel to make all features " f"available :wink:"
attachments = [
{
"text": text,
}
]
ScenarioStep(slack_team_identity)._publish_message_to_thread(alert_group, attachments)
ScenarioStep(slack_team_identity).publish_message_to_thread(alert_group, text=text)
@shared_dedicated_queue_retry_task(autoretry_for=(Exception,), retry_backoff=True, max_retries=1)

View file

@ -286,7 +286,6 @@ class SlackEventApiEndpointView(APIView):
or payload["event"]["subtype"] == EVENT_SUBTYPE_MESSAGE_DELETED
)
):
print("Inside channel.messages event")
for route in SCENARIOS_ROUTES:
if (
"message_channel_type" in route