-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
🔧 chore(discord): change rate limits to halt for SLOs #83656
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from unittest.mock import patch | ||
|
||
import orjson | ||
import responses | ||
|
||
from sentry.incidents.models.alert_rule import AlertRuleTriggerAction | ||
from sentry.incidents.models.incident import IncidentStatus | ||
from sentry.integrations.discord.client import CHANNEL_URL, DISCORD_BASE_URL, MESSAGE_URL | ||
from sentry.integrations.discord.spec import DiscordMessagingSpec | ||
from sentry.integrations.messaging.spec import MessagingActionHandler | ||
from sentry.integrations.types import EventLifecycleOutcome | ||
from sentry.shared_integrations.exceptions import ApiRateLimitedError | ||
from sentry.testutils.asserts import assert_slo_metric | ||
from sentry.testutils.helpers.datetime import freeze_time | ||
|
||
from . import FireTest | ||
|
||
|
||
@freeze_time() | ||
class DiscordActionHandlerTest(FireTest): | ||
@responses.activate | ||
def setUp(self): | ||
self.spec = DiscordMessagingSpec() | ||
|
||
self.guild_id = "guild-id" | ||
self.channel_id = "12345678910" | ||
self.discord_user_id = "user1234" | ||
|
||
responses.add( | ||
method=responses.GET, | ||
url=f"{DISCORD_BASE_URL}{CHANNEL_URL.format(channel_id=self.channel_id)}", | ||
json={"type": 0, "guild_id": self.guild_id}, | ||
status=200, | ||
) | ||
self.discord_integration = self.create_integration( | ||
provider="discord", | ||
name="Cool server", | ||
external_id=self.guild_id, | ||
organization=self.organization, | ||
) | ||
self.provider = self.create_identity_provider(integration=self.discord_integration) | ||
self.identity = self.create_identity( | ||
user=self.user, identity_provider=self.provider, external_id=self.discord_user_id | ||
) | ||
|
||
self.action = self.create_alert_rule_trigger_action( | ||
target_identifier=self.channel_id, | ||
type=AlertRuleTriggerAction.Type.DISCORD, | ||
target_type=AlertRuleTriggerAction.TargetType.SPECIFIC, | ||
integration=self.discord_integration, | ||
) | ||
|
||
@responses.activate | ||
def run_test(self, incident, method): | ||
responses.add( | ||
method=responses.POST, | ||
url=f"{DISCORD_BASE_URL}{MESSAGE_URL.format(channel_id=self.channel_id)}", | ||
json={}, | ||
status=200, | ||
) | ||
|
||
handler = MessagingActionHandler(self.action, incident, self.project, self.spec) | ||
metric_value = 1000 | ||
with self.tasks(): | ||
getattr(handler, method)(metric_value, IncidentStatus(incident.status)) | ||
|
||
data = orjson.loads(responses.calls[0].request.body) | ||
return data | ||
|
||
def test_fire_metric_alert(self): | ||
self.run_fire_test() | ||
|
||
def test_resolve_metric_alert(self): | ||
self.run_fire_test("resolve") | ||
|
||
@responses.activate | ||
def test_rule_snoozed(self): | ||
alert_rule = self.create_alert_rule() | ||
incident = self.create_incident(alert_rule=alert_rule, status=IncidentStatus.CLOSED.value) | ||
self.snooze_rule(alert_rule=alert_rule) | ||
|
||
responses.add( | ||
method=responses.POST, | ||
url=f"{DISCORD_BASE_URL}{MESSAGE_URL.format(channel_id=self.channel_id)}", | ||
json={}, | ||
status=200, | ||
) | ||
|
||
handler = MessagingActionHandler(self.action, incident, self.project, self.spec) | ||
metric_value = 1000 | ||
with self.tasks(): | ||
handler.fire(metric_value, IncidentStatus(incident.status)) | ||
|
||
assert len(responses.calls) == 0 | ||
|
||
@responses.activate | ||
@patch("sentry.integrations.discord.client.DiscordClient.send_message", side_effect=Exception) | ||
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") | ||
def test_metric_alert_failure(self, mock_record_event, mock_send_message): | ||
alert_rule = self.create_alert_rule() | ||
incident = self.create_incident(alert_rule=alert_rule, status=IncidentStatus.CLOSED.value) | ||
handler = MessagingActionHandler(self.action, incident, self.project, self.spec) | ||
metric_value = 1000 | ||
with self.tasks(): | ||
handler.fire(metric_value, IncidentStatus.OPEN) | ||
|
||
assert_slo_metric(mock_record_event, EventLifecycleOutcome.FAILURE) | ||
|
||
@patch( | ||
"sentry.integrations.discord.client.DiscordClient.send_message", | ||
side_effect=ApiRateLimitedError(text="Rate limited"), | ||
) | ||
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") | ||
def test_metric_alert_halt_for_rate_limited(self, mock_record_event, mock_send_message): | ||
alert_rule = self.create_alert_rule() | ||
incident = self.create_incident(alert_rule=alert_rule, status=IncidentStatus.CLOSED.value) | ||
handler = MessagingActionHandler(self.action, incident, self.project, self.spec) | ||
metric_value = 1000 | ||
with self.tasks(): | ||
handler.fire(metric_value, IncidentStatus.OPEN) | ||
|
||
assert_slo_metric(mock_record_event, EventLifecycleOutcome.HALTED) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need this if the setup is already setting a mock response, and we're asserting that it's not called?
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.
fixed!