Skip to content

Commit

Permalink
Add user email to Sentry context for better support
Browse files Browse the repository at this point in the history
  • Loading branch information
boydgreenfield committed Aug 31, 2017
1 parent e1db38d commit 9bb6d75
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
9 changes: 8 additions & 1 deletion onecodex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, api_key=None,

# Optionally configure Raven
if self._telemetry:
self._raven_client = get_raven_client()
self._raven_client = get_raven_client(user_context={'email': self._fetch_account_email()})
else:
self._raven_client = None

Expand All @@ -84,6 +84,13 @@ def __init__(self, api_key=None,
if module._imported:
setattr(self, module._name, module)

def _fetch_account_email(self):
creds_fp = os.path.expanduser('~/.onecodex')
if os.path.exists(creds_fp):
with open(creds_fp) as f:
creds = json.load(f)
return creds.get('email')

def _copy_resources(self):
"""
Copy all of the resources over to the toplevel client
Expand Down
13 changes: 10 additions & 3 deletions onecodex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def collapse_user(fp):
return abs_path.replace(home_dir, "~")


def get_raven_client(**extra):
def get_raven_client(user_context=None, extra_context=None):
if os.environ.get('ONE_CODEX_NO_TELEMETRY') is None:
key = base64.b64decode(
b'NmFlMjMwYWY4NjI5NDg3NmEyYzYwYjZjNDhhZDJiYzI6ZTMyZmYwZTVhNjUwNGQ5NGJhODc0NWZlMmU1ZjNmZjA='
Expand All @@ -233,8 +233,15 @@ def get_raven_client(**extra):
include_paths=[],
release=__version__
)
extra['platform'] = platform.platform()
client.extra_context(extra)

if extra_context is None:
extra_context = {}
if user_context is None:
user_context = {}

extra_context['platform'] = platform.platform()
client.user_context(user_context)
client.extra_context(extra_context)
return client
except Exception:
return
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def mockreturn(path):
def mocked_creds_file(mocked_creds_path):
with open(os.path.expanduser('~/.onecodex'), mode='w') as f:
f.write(json.dumps({
'email': 'test@onecodex.com',
'api_key': None,
'saved_at': datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
}))
3 changes: 2 additions & 1 deletion tests/test_raven.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def cli_exception(cli_args):
assert grc.call_count == call_count


def test_ocx_with_raven(ocx_w_raven):
def test_ocx_with_raven(ocx_w_raven, mocked_creds_file):
assert ocx_w_raven._telemetry is True
assert isinstance(ocx_w_raven._raven_client, RavenClient)
assert ocx_w_raven._raven_client.remote.base_url == 'https://sentry.example.com'
assert ocx_w_raven._raven_client.is_enabled() is True
assert 'email' in ocx_w_raven._raven_client.context['user']


def test_good_sentry_dsn():
Expand Down

0 comments on commit 9bb6d75

Please sign in to comment.