From f71140bd0fa15e9c1630fbb1bfd950361aea3310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Miguel=20Neves?= Date: Sat, 10 Nov 2018 08:40:01 +0000 Subject: [PATCH] use new sentry-sdk instead of raven --- requirements.in | 2 +- requirements.txt | 8 +++-- setup.py | 4 +-- zappa_sentry/__init__.py | 72 ++++++++++++++++------------------------ 4 files changed, 36 insertions(+), 50 deletions(-) diff --git a/requirements.in b/requirements.in index d74aa87..8970ed6 100644 --- a/requirements.in +++ b/requirements.in @@ -1,4 +1,4 @@ pip-tools -raven +sentry-sdk twine flake8 diff --git a/requirements.txt b/requirements.txt index 9d559c4..cc36f1d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,9 +4,11 @@ # # pip-compile --output-file requirements.txt requirements.in # -certifi==2018.1.18 # via requests +certifi==2018.1.18 # via requests, sentry-sdk chardet==3.0.4 # via requests click==6.7 # via pip-tools +configparser==3.5.0 # via flake8 +enum34==1.1.6 # via flake8 first==2.0.1 # via pip-tools flake8==3.5.0 idna==2.6 # via requests @@ -15,10 +17,10 @@ pip-tools==1.11.0 pkginfo==1.4.2 # via twine pycodestyle==2.3.1 # via flake8 pyflakes==1.6.0 # via flake8 -raven==6.5.0 requests-toolbelt==0.8.0 # via twine requests==2.18.4 # via requests-toolbelt, twine +sentry-sdk==0.5.4 six==1.11.0 # via pip-tools tqdm==4.19.8 # via twine twine==1.10.0 -urllib3==1.22 # via requests +urllib3==1.22 # via requests, sentry-sdk diff --git a/setup.py b/setup.py index e4cfc04..85c0fa1 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name = 'zappa_sentry', packages = ['zappa_sentry'], - version = '0.2.3', + version = '0.3.0', description = 'Easy integration with sentry for zappa apps', author = 'João Miguel Neves', author_email = 'joao@silvaneves.org', @@ -11,7 +11,7 @@ download_url = 'https://github.com/jneves/zappa-sentry/archive/0.2.3.tar.gz', keywords = 'logging zappa sentry', install_requires=[ - 'raven' + 'sentry-sdk' ], classifiers = [ 'Development Status :: 3 - Alpha', diff --git a/zappa_sentry/__init__.py b/zappa_sentry/__init__.py index bff8a8f..6c73050 100644 --- a/zappa_sentry/__init__.py +++ b/zappa_sentry/__init__.py @@ -1,63 +1,47 @@ +# -*- coding: utf-8 -*- from configparser import ConfigParser import json import os -import sys -from raven import Client -from raven.transport.http import HTTPTransport - - -# Cache this value and clients in the module to reduce overhead -this = sys.modules[__name__] - -this.raven_config = ConfigParser(os.environ) -this._raven_client = None - - -def get_raven_client(): - if not this._raven_client: - this._raven_client = Client(this.raven_config.get('DEFAULT', 'SENTRY_DSN'), - transport=HTTPTransport) - return this._raven_client +import sentry_sdk +from sentry_sdk import capture_exception, configure_scope def unhandled_exceptions(e, event, context): - """Exception handler reports exceptions to sentry but does not capture them.""" - raven_client = get_raven_client() - - try: - package_info_file = open('package_info.json', 'r') - package_info = json.load(package_info_file) - package_info_file.close() - - raven_client.context.merge({'tags': package_info}) - except OSError: - # not deployed, probably a test - pass - - if 'httpMethod' in event: - extra_tags = { - 'http_method': event['httpMethod'], - 'path': event['path'] - } + "Exception handler reports exceptions to sentry but does not capture them." + sentry_config = ConfigParser(os.environ) + sentry_sdk.init(sentry_config.get('DEFAULT', 'SENTRY_DSN')) + + with configure_scope() as scope: + try: + package_info_file = open('package_info.json', 'r') + package_info = json.load(package_info_file) + package_info_file.close() + + for key, value in package_info.items(): + scope.set_tag(key, value) + except OSError: + # not deployed, probably a test + pass + + if 'httpMethod' in event: + scope.set_tag('http_method', event['httpMethod']) + scope.set_tag('path', event['path']) if 'Host' in event['headers']: - extra_tags['host'] = event['headers']['Host'] + scope.set_tag('host', event['headers']['Host']) if 'User-Agent' in event['headers']: - extra_tags['user_agent'] = event['headers']['User-Agent'] + scope.set_tag('user_agent', event['headers']['User-Agent']) if 'requestContext' in event and 'stage' in event['requestContext']: - extra_tags['stage'] = event['requestContext']['stage'] - raven_client.context.merge({'tags': extra_tags}) + scope.set_tag('stage', event['requestContext']['stage']) - raven_client.context.merge({'extra': { - 'event': event - }}) + scope.set_extra('event', event) - raven_client.captureException() + capture_exception(e) return False def capture_exceptions(e, event, context): - """Exception handler that makes exceptions disappear after processing them.""" + "Exception handler that makes exceptions disappear after processing them." unhandled_exceptions(e, event, context) return True