Skip to content

Commit

Permalink
make capturing of exceptions explicit and lazyload the raven configur…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
jneves committed Mar 15, 2018
1 parent bf63434 commit a170f50
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ Example:

And that's all. Deploy your zappa function and you should see any errors appearing on sentry.

If you want the exception handler to capture the exception and capture it, just replace `zappa_sentry.unhandled_exceptions` by `zappa_sentry.capture_exceptions`. This version won't let the exceptions propagate.

# Sentry raven client

If you need sentry's client to add extra information just do:

`from zappa_sentry import raven_client`
```
from zappa_sentry import get_raven_client
raven_client = get_raven_client()
```

And you'll get an already initialized raven_client. Feel free to use to add context, tags, etc.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
setup(
name = 'zappa_sentry',
packages = ['zappa_sentry'],
version = '0.1.6',
version = '0.2.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.1.4.tar.gz',
download_url = 'https://github.com/jneves/zappa-sentry/archive/0.2.0.tar.gz',
keywords = 'logging zappa sentry',
install_requires=[
'raven'
Expand Down
21 changes: 20 additions & 1 deletion zappa_sentry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from configparser import ConfigParser
import json
import os

from raven import Client
from raven.transport.http import HTTPTransport

raven_client = Client(os.environ['SENTRY_DSN'], transport=HTTPTransport)

raven_config = ConfigParser(os.environ)
_raven_client = None


def get_raven_client():
if not _raven_client:
_raven_client = Client(raven_config.get('DEFAULT', 'SENTRY_DSN'), transport=HTTPTransport)
return _raven_client


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)
Expand Down Expand Up @@ -36,4 +48,11 @@ def unhandled_exceptions(e, event, context):
}})

raven_client.captureException()
return False


def capture_exceptions(e, event, context):
"""Exception handler that makes exceptions disappear after processing them."""

unhandled_exceptions(e, event, context)
return True

0 comments on commit a170f50

Please sign in to comment.