Skip to content

Instantly share code, notes, and snippets.

@jurrian
Created September 30, 2021 10:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jurrian/e22f8e724b8499a29c5537e956f0dc7f to your computer and use it in GitHub Desktop.
Save jurrian/e22f8e724b8499a29c5537e956f0dc7f to your computer and use it in GitHub Desktop.
Client-side rate limiting for Sentry
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],
)
@dineshgowda24
Copy link

Hey @jurrian
Does this rate limit apply per error class/message or on the entire sentry errors?

@leftys
Copy link

leftys commented Jul 22, 2022

sentry client seems to send no issues with this configuration. deleting the _breadcrumb_handler filter line seems to help.

@jurrian
Copy link
Author

jurrian commented Jan 11, 2023

@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