Skip to content

Commit

Permalink
use new sentry-sdk instead of raven
Browse files Browse the repository at this point in the history
  • Loading branch information
jneves committed Nov 10, 2018
1 parent e2ac6c5 commit f71140b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 50 deletions.
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pip-tools
raven
sentry-sdk
twine
flake8
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
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',
url = 'https://github.com/jneves/zappa-sentry',
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',
Expand Down
72 changes: 28 additions & 44 deletions zappa_sentry/__init__.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f71140b

Please sign in to comment.