|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from sentry_sdk import capture_exception, configure_scope, get_current_hub |
| 4 | +from ._wsgi import get_environ |
| 5 | + |
| 6 | +try: |
| 7 | + from flask_login import current_user |
| 8 | +except ImportError: |
| 9 | + current_user = None |
| 10 | + |
| 11 | +from flask import current_app, request, _app_ctx_stack |
| 12 | +from flask.signals import appcontext_pushed, appcontext_popped, got_request_exception |
| 13 | + |
| 14 | +class FlaskSentry(object): |
| 15 | + def __init__(self, app=None): |
| 16 | + self.app = app |
| 17 | + if app is not None: |
| 18 | + self.init_app(app) |
| 19 | + |
| 20 | + def init_app(self, app): |
| 21 | + if not hasattr(app, 'extensions'): |
| 22 | + app.extensions = {} |
| 23 | + elif app.extensions.get(__name__, None): |
| 24 | + raise RuntimeError('Sentry registration is already registered') |
| 25 | + app.extensions[__name__] = True |
| 26 | + |
| 27 | + appcontext_pushed.connect(_push_appctx, sender=app) |
| 28 | + app.teardown_appcontext(_pop_appctx) |
| 29 | + got_request_exception.connect(_capture_exception, sender=app) |
| 30 | + app.before_request(_before_request) |
| 31 | + |
| 32 | + |
| 33 | +def _push_appctx(*args, **kwargs): |
| 34 | + assert getattr( |
| 35 | + _app_ctx_stack.top, |
| 36 | + '_sentry_app_scope_manager', |
| 37 | + None |
| 38 | + ) is None, 'race condition' |
| 39 | + _app_ctx_stack.top._sentry_app_scope_manager = \ |
| 40 | + get_current_hub().push_scope().__enter__() |
| 41 | + |
| 42 | + |
| 43 | +def _pop_appctx(exception): |
| 44 | + assert getattr( |
| 45 | + _app_ctx_stack.top, |
| 46 | + '_sentry_app_scope_manager', |
| 47 | + None |
| 48 | + ) is not None, 'race condition' |
| 49 | + _app_ctx_stack.top._sentry_app_scope_manager.__exit__(None, None, None) |
| 50 | + _app_ctx_stack.top._sentry_app_scope_manager = None |
| 51 | + |
| 52 | + |
| 53 | +def _capture_exception(sender, exception, **kwargs): |
| 54 | + capture_exception(exception) |
| 55 | + |
| 56 | + |
| 57 | +def _before_request(*args, **kwargs): |
| 58 | + assert getattr( |
| 59 | + _app_ctx_stack.top, |
| 60 | + '_sentry_app_scope_manager', |
| 61 | + None |
| 62 | + ) is not None, 'scope push failed' |
| 63 | + |
| 64 | + with configure_scope() as scope: |
| 65 | + if request.url_rule: |
| 66 | + scope.transaction = request.url_rule.endpoint |
| 67 | + |
| 68 | + try: |
| 69 | + scope.request = _get_request_info() |
| 70 | + except Exception: |
| 71 | + get_current_hub().capture_internal_exception() |
| 72 | + |
| 73 | + try: |
| 74 | + scope.user = _get_user_info() |
| 75 | + except Exception: |
| 76 | + raise |
| 77 | + get_current_hub().capture_internal_exception() |
| 78 | + |
| 79 | +def _get_request_info(): |
| 80 | + { |
| 81 | + 'url': '%s://%s%s' % (request.scheme, urlparts.host, request.path), |
| 82 | + 'query_string': urlparts.query, |
| 83 | + 'method': request.method, |
| 84 | + 'data': request.get_data(cache=True, as_text=True, parse_form_data=True), |
| 85 | + 'headers': dict(request.headers), |
| 86 | + 'env': get_environ(request.environ) |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +def _get_user_info(): |
| 91 | + try: |
| 92 | + ip_address = request.access_route[0] |
| 93 | + except IndexError: |
| 94 | + ip_address = request.remote_addr |
| 95 | + |
| 96 | + user_info = { |
| 97 | + 'id': None, |
| 98 | + 'ip_address': ip_address |
| 99 | + } |
| 100 | + |
| 101 | + try: |
| 102 | + user_info['id'] = current_user.get_id() |
| 103 | + # TODO: more configurable user attrs here |
| 104 | + except AttributeError: |
| 105 | + # might happen if: |
| 106 | + # - flask_login could not be imported |
| 107 | + # - flask_login is not configured |
| 108 | + # - no user is logged in |
| 109 | + pass |
| 110 | + |
| 111 | + return user_info |
0 commit comments