Skip to content

Commit

Permalink
Add a context manager to capture exceptions in sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Aug 21, 2017
1 parent 0f1494b commit 3d08df8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions acceptance_tests/app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pyramid.httpexceptions import HTTPForbidden

from c2cwsgiutils import services
from c2cwsgiutils import sentry
from c2cwsgiutils.stats import timer_context, increment_counter, set_gauge
from c2cwsgiutils_app import models

Expand Down Expand Up @@ -34,8 +35,9 @@ def hello_put(request):
"""
Will use the master
"""
hello = models.DBSession.query(models.Hello).first()
return {'value': hello.value}
with sentry.capture_exceptions():
hello = models.DBSession.query(models.Hello).first()
return {'value': hello.value}


@hello_service.post()
Expand Down
16 changes: 16 additions & 0 deletions c2cwsgiutils/sentry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import contextlib
import logging
import os
from raven import Client
from raven.handlers.logging import SentryHandler
from raven.conf import setup_logging

LOG = logging.getLogger(__name__)
client = None


def init():
global client
if 'SENTRY_URL' in os.environ:
client_info = {key[14:].lower(): value
for key, value in os.environ.items() if key.startswith('SENTRY_CLIENT_')}
Expand All @@ -21,3 +24,16 @@ def init():

setup_logging(handler, exclude=('raven', ))
LOG.info("Configured sentry reporting with client=%s", repr(client_info))


@contextlib.contextmanager
def capture_exceptions():
global client
if client is not None:
try:
yield
except:
client.captureException()
raise
else:
yield
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = '0.20.1'
VERSION = '0.21.0'
HERE = os.path.abspath(os.path.dirname(__file__))
INSTALL_REQUIRES = open(os.path.join(HERE, 'requirements.txt')).read().splitlines()

Expand Down

0 comments on commit 3d08df8

Please sign in to comment.