Skip to content

Commit

Permalink
WIP support for optional sentry logging in IPython
Browse files Browse the repository at this point in the history
  • Loading branch information
boydgreenfield committed Aug 31, 2017
1 parent d3f1b54 commit 2bbd1f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions onecodex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Api(object):

def __init__(self, api_key=None,
bearer_token=None, cache_schema=False,
base_url=None, telemetry=False,
base_url=None, telemetry=None,
schema_path='/api/v1/schema'):

if base_url is None:
Expand All @@ -42,7 +42,6 @@ def __init__(self, api_key=None,
self._req_args = {}
self._base_url = base_url
self._schema_path = schema_path
self._telemetry = telemetry

# Attempt to automatically fetch API key from
# ~/.onecodex file, API key, or bearer token environment vars
Expand Down Expand Up @@ -73,10 +72,12 @@ def __init__(self, api_key=None,
self._copy_resources()

# Optionally configure Raven
if self._telemetry:
if telemetry is True or (telemetry is None and os.environ.get('ONE_CODEX_AUTO_TELEMETRY', False)):
self._raven_client = get_raven_client(user_context={'email': self._fetch_account_email()})
self._telemetry = True
else:
self._raven_client = None
self._telemetry = False

# Try to import and copy key modules onto the Api object
for module_name in ['onecodex.viz', 'onecodex.distance']:
Expand Down
24 changes: 23 additions & 1 deletion onecodex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ def collapse_user(fp):
return abs_path.replace(home_dir, "~")


def _setup_sentry_for_ipython(client):
from IPython import get_ipython
ip = get_ipython()

def custom_exc(shell, etype, evalue, tb, tb_offset=None):
# Show original error
shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)

# Then send to Sentry
client.captureException()

if ip is not None:
ip.set_custom_exc((Exception,), custom_exc)


def get_raven_client(user_context=None, extra_context=None):
if os.environ.get('ONE_CODEX_NO_TELEMETRY') is None:
key = base64.b64decode(
Expand All @@ -230,7 +245,7 @@ def get_raven_client(user_context=None, extra_context=None):
'https://{}@sentry.onecodex.com/9'.format(key)),
install_sys_hook=install_sys_hook,
raise_send_errors=False,
include_paths=[],
include_paths=[__name__.split('.', 1)[0]],
release=__version__
)

Expand All @@ -239,9 +254,16 @@ def get_raven_client(user_context=None, extra_context=None):
if user_context is None:
user_context = {}

try:
_setup_sentry_for_ipython(client)
extra_context['ipython'] = True
except Exception:
pass

extra_context['platform'] = platform.platform()
client.user_context(user_context)
client.extra_context(extra_context)

return client
except Exception:
return
Expand Down

0 comments on commit 2bbd1f2

Please sign in to comment.