Last active
November 2, 2021 02:32
-
-
Save Framartin/4e57b57139ed31f36684cfc514037bf6 to your computer and use it in GitHub Desktop.
Simplest scrapy extension to send all errors and exceptions to 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 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 |
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
sentry-sdk |
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
# 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