Skip to content

Instantly share code, notes, and snippets.

@Framartin
Last active November 2, 2021 02:32
Show Gist options
  • Save Framartin/4e57b57139ed31f36684cfc514037bf6 to your computer and use it in GitHub Desktop.
Save Framartin/4e57b57139ed31f36684cfc514037bf6 to your computer and use it in GitHub Desktop.
Simplest scrapy extension to send all errors and exceptions to Sentry
import sentry_sdk
from scrapy.exceptions import NotConfigured
class SentryLogging(object):
"""
Send exceptions and errors to Sentry.
"""
@classmethod
def from_crawler(cls, crawler):
sentry_dsn = crawler.settings.get('SENTRY_DSN', None)
if sentry_dsn is None:
raise NotConfigured
# instantiate the extension object
ext = cls()
# instantiate
sentry_sdk.init(sentry_dsn)
# return the extension object
return ext
# Add the following lines to your settings.py file:
# ...
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
EXTENSIONS = {
'myproject.extensions.SentryLogging': -1, # Load SentryLogging extension before others
}
# Send exceptions to Sentry
# replace SENTRY_DSN by you own DSN
SENTRY_DSN = "XXXXXXXXXX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment