-
Notifications
You must be signed in to change notification settings - Fork 299
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
fix IntegrityErrors occuring when creating ResolutionNoteSlackMessage objects #2933
Changes from 4 commits
eea2346
9e8a788
e21d1bc
c6faee3
437f473
742fef6
91faf47
bdc4474
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 3.2.20 on 2023-08-31 14:45 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('alerts', '0030_auto_20230731_0341'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddIndex( | ||
model_name='resolutionnoteslackmessage', | ||
index=models.Index(fields=['ts', 'thread_ts', 'alert_group_id'], name='alerts_reso_ts_08f72c_idx'), | ||
), | ||
migrations.AddIndex( | ||
model_name='resolutionnoteslackmessage', | ||
index=models.Index(fields=['ts', 'thread_ts', 'slack_channel_id'], name='alerts_reso_ts_a9bdf7_idx'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,46 +82,32 @@ def save_thread_message_for_resolution_note( | |
if result["permalink"] is not None: | ||
permalink = result["permalink"] | ||
|
||
try: | ||
slack_thread_message = ResolutionNoteSlackMessage.objects.get( | ||
ts=message_ts, | ||
thread_ts=thread_ts, | ||
alert_group=alert_group, | ||
) | ||
if len(text) > 2900: | ||
if slack_thread_message.added_to_resolution_note: | ||
self._slack_client.api_call( | ||
"chat.postEphemeral", | ||
channel=channel, | ||
user=slack_user_identity.slack_id, | ||
text=":warning: Unable to update the <{}|message> in Resolution Note: the message is too long ({}). " | ||
"Max length - 2900 symbols.".format(permalink, len(text)), | ||
) | ||
return | ||
slack_thread_message.text = text | ||
slack_thread_message.save() | ||
slack_thread_message, created = ResolutionNoteSlackMessage.objects.get_or_create( | ||
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. I think it's better to check 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. 👍 do you happen to know why there is this condition?: if slack_thread_message.added_to_resolution_note: if that can be removed, we can still use 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. alternative solution is to keep the separate 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. ResolutionNoteSlackMessage is a message from alert group thread that potentially can be added to resolution notes. It looks like we post warning message about too long text only either for messages that had already been added to resolution notes or for new messages in thread 🤔 |
||
ts=message_ts, | ||
thread_ts=thread_ts, | ||
alert_group=alert_group, | ||
defaults={ | ||
"user": self.user, | ||
"added_by_user": self.user, | ||
"text": text, | ||
"slack_channel_id": channel, | ||
"permalink": permalink, | ||
}, | ||
) | ||
Comment on lines
+95
to
+106
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. the main issue here seemed to be Slack sending duplicate events to our API endpoint, shortly after one another, which led to this issue. Example from logs (check trace IDs According to Django's docs, the solution to this is to use
|
||
|
||
except ResolutionNoteSlackMessage.DoesNotExist: | ||
if len(text) > 2900: | ||
if len(text) > 2900: | ||
if created or slack_thread_message.added_to_resolution_note: | ||
self._slack_client.api_call( | ||
"chat.postEphemeral", | ||
channel=channel, | ||
user=slack_user_identity.slack_id, | ||
text=":warning: The <{}|message> will not be displayed in Resolution Note: " | ||
"the message is too long ({}). Max length - 2900 symbols.".format(permalink, len(text)), | ||
text=":warning: Unable to update the <{}|message> in Resolution Note: the message is too long ({}). " | ||
"Max length - 2900 symbols.".format(permalink, len(text)), | ||
) | ||
return | ||
|
||
slack_thread_message = ResolutionNoteSlackMessage( | ||
alert_group=alert_group, | ||
user=self.user, | ||
added_by_user=self.user, | ||
text=text, | ||
slack_channel_id=channel, | ||
thread_ts=thread_ts, | ||
ts=message_ts, | ||
permalink=permalink, | ||
) | ||
return | ||
|
||
if not created: | ||
slack_thread_message.text = text | ||
slack_thread_message.save() | ||
|
||
def delete_thread_message_from_resolution_note( | ||
|
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.
added indices to speed up querying these objects. These are the two filter patterns we use when querying these objects.