Created
September 30, 2021 10:20
-
-
Save jurrian/e22f8e724b8499a29c5537e956f0dc7f to your computer and use it in GitHub Desktop.
Client-side rate limiting for Sentry
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
import logging | |
import sentry_sdk | |
from ratelimitingfilter import RateLimitingFilter | |
from sentry_sdk.integrations.django import DjangoIntegration | |
from sentry_sdk.integrations.logging import LoggingIntegration | |
# Sentry | |
sentry_logging = LoggingIntegration( | |
level=logging.INFO, # Capture info and above as breadcrumbs | |
event_level=logging.ERROR # Send errors as events | |
) | |
# Allow the first 1000 events, then limit 1 per 10 minutes = max 4320 events per month. | |
# Should prevent spammy loggers from depleting Sentry quota, doesn't affect capture_message() and capture_exception(). | |
rate_limit = RateLimitingFilter(rate=1, per=600, burst=1000) | |
sentry_logging._handler.addFilter(rate_limit) | |
sentry_logging._breadcrumb_handler.addFilter(rate_limit) | |
sentry_sdk.init( | |
integrations=[DjangoIntegration(), sentry_logging], | |
) |
sentry client seems to send no issues with this configuration. deleting the _breadcrumb_handler filter line seems to help.
@dineshgowda24 this applies rate limiting to all sentry errors send using the logging facility, so capture_exception()
and capture_message()
are not included here.
@leftys interesting remark, for us this has been working all this time now. I cannot tell what is different in your case, but it's good to take in consideration for others.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @jurrian
Does this rate limit apply per error class/message or on the entire sentry errors?