Skip to content
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

Raise a sentry warning when ratelimitted by Discord #1170

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pydis_site/apps/api/tests/test_github_webhook_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest import mock
from urllib.error import HTTPError

from django.urls import reverse
from rest_framework.test import APITestCase

from pydis_site.apps.api.views import GitHubWebhookFilterView

class GitHubWebhookFilterAPITests(APITestCase):
def test_ignores_bot_sender(self):
Expand Down Expand Up @@ -44,3 +46,17 @@ def test_accepts_interesting_events(self):
response = self.client.post(url, data=payload, headers=headers)
self.assertEqual(response.status_code, context_mock.status)
self.assertEqual(response.headers.get('X-Clacks-Overhead'), 'Joe Armstrong')

def test_rate_limit_is_logged_to_sentry(self):
url = reverse('api:github-webhook-filter', args=('id', 'token'))
payload = {}
headers = {'X-GitHub-Event': 'pull_request_review'}
with (
mock.patch('urllib.request.urlopen') as urlopen,
mock.patch.object(GitHubWebhookFilterView, "logger") as logger,
):
urlopen.side_effect = HTTPError(None, 429, 'Too Many Requests', {}, None)
logger.warning = mock.PropertyMock()
self.client.post(url, data=payload, headers=headers)

logger.warning.assert_called_once()
9 changes: 9 additions & 0 deletions pydis_site/apps/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import logging
import urllib.request
from collections.abc import Mapping
from http import HTTPStatus

from rest_framework import status
from rest_framework.exceptions import ParseError
Expand Down Expand Up @@ -254,6 +256,7 @@ class GitHubWebhookFilterView(APIView):

authentication_classes = ()
permission_classes = ()
logger = logging.getLogger(__name__ + ".GitHubWebhookFilterView")

def post(self, request: Request, *, webhook_id: str, webhook_token: str) -> Response:
"""Filter a webhook POST from GitHub before sending it to Discord."""
Expand Down Expand Up @@ -329,4 +332,10 @@ def send_webhook(
with urllib.request.urlopen(request) as response: # noqa: S310
return (response.status, dict(response.getheaders()), response.read())
except urllib.error.HTTPError as err: # pragma: no cover
if err.code == HTTPStatus.TOO_MANY_REQUESTS:
self.logger.warning(
"We are being rate limited by Discord! Scope: %s, reset-after: %s",
headers.get("X-RateLimit-Scope"),
headers.get("X-RateLimit-Reset-After"),
)
return (err.code, dict(err.headers), err.fp.read())
7 changes: 5 additions & 2 deletions pydis_site/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import logging
import os
import secrets
import sys
Expand All @@ -19,6 +20,7 @@

import environ
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.django import DjangoIntegration

env = environ.Env(
Expand Down Expand Up @@ -50,9 +52,10 @@
if not STATIC_BUILD:
sentry_sdk.init(
dsn=env('SITE_DSN'),
integrations=[DjangoIntegration()],
integrations=[DjangoIntegration(), LoggingIntegration(level=logging.DEBUG, event_level=logging.ERROR)],
send_default_pii=True,
release=f"site@{GIT_SHA}"
release=f"site@{GIT_SHA}",
profiles_sample_rate=0.5,
)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand Down
Loading