Created
February 1, 2019 16:38
-
-
Save toast38coza/fc590c12fcc1c9c8bd900931f58c6bba to your computer and use it in GitHub Desktop.
Fun times with Sentry (and Django)
This file contains hidden or 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
[bumpversion] | |
current_version = 1.0.0 | |
commit = False | |
tag = False | |
[bumpversion:file:api/custom_settings.py] |
This file contains hidden or 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
from sentry_sdk import push_scope, capture_exception | |
import requests | |
fail_silently = True | |
response = requests.get(url, data) | |
if not response.ok: | |
with push_scope() as local_scope: | |
req = response.request | |
# request: | |
scope.set_extra('request.url', req.url) | |
scope.set_extra('request.method', req.method) | |
scope.set_extra('request.headers', str(req.headers)) | |
scope.set_extra('request.body', req.body) | |
# response: | |
scope.set_extra('response.code', response.status_code) | |
scope.set_extra('response.content', response.content) | |
capture_exception( | |
Exception("API request failed") | |
) |
This file contains hidden or 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
from sentry_sdk import configure_scope | |
class SentryMiddleware: | |
def __init__(self, get_response): | |
self.get_response = get_response | |
def __call__(self, request): | |
with configure_scope() as scope: | |
scope.user = { "id": request.user.id } | |
# .. configure any other context info you would like Sentry to | |
# attached to Exceptions | |
return response |
This file contains hidden or 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 os | |
APP_NAME = '..' | |
APP_VERSION = '1.0.0' | |
IS_PRODUCTION = ENVIRONMENT.lower() == 'production' | |
if IS_PRODUCTION: | |
dsn = "https://{}@sentry.io/{}".format( | |
os.environ.get("SENTRY_PUBLIC_KEY"), | |
os.environ.get("SENTRY_PROJECT_ID"), | |
) | |
sentry_sdk.init( | |
dsn=dsn, | |
environment=ENVIRONMENT, | |
release="{}@{}".format(APP_NAME, APP_VERSION), | |
integrations=[DjangoIntegration()] | |
) |
This file contains hidden or 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
# basic: | |
bumpversion {major|minor|patch} | |
# create a tag: | |
bumpversion major --tag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment