Exemplo n.º 1
0
    def setup_once():
        # type: () -> None

        # This version parsing is absolutely naive but the alternative is to
        # import pkg_resources which slows down the SDK a lot.
        try:
            version = tuple(map(int, FLASK_VERSION.split(".")[:3]))
        except (ValueError, TypeError):
            # It's probably a release candidate, we assume it's fine.
            pass
        else:
            if version < (0, 10):
                raise DidNotEnable("Flask 0.10 or newer is required.")

        before_render_template.connect(_add_sentry_trace)
        request_started.connect(_request_started)
        got_request_exception.connect(_capture_exception)

        old_app = Flask.__call__

        def sentry_patched_wsgi_app(self, environ, start_response):
            # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
            if Hub.current.get_integration(FlaskIntegration) is None:
                return old_app(self, environ, start_response)

            return SentryWsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))(
                environ, start_response
            )

        Flask.__call__ = sentry_patched_wsgi_app  # type: ignore
Exemplo n.º 2
0
    def __init__(
            self,
            # The Flask application to analyze
            app: Flask,
            # The HowFast app ID to use
            app_id: str = None,
            # Endpoints not to monitor
            endpoints_blacklist: List[str] = None,
            # Other configuration parameters passed to the CoreAPM constructor
            **kwargs,
    ):
        super().__init__(**kwargs)

        self.app = app
        self.wsgi_app = app.wsgi_app

        # We need to store thread local information, let's use Werkzeug's context locals
        # (see https://werkzeug.palletsprojects.com/en/1.0.x/local/)
        self.local = local.Local()
        self.local_manager = local.LocalManager([self.local])

        # Overwrite the passed WSGI application
        app.wsgi_app = self.local_manager.make_middleware(self)

        if endpoints_blacklist:
            self.endpoints_blacklist = compile_endpoints(*endpoints_blacklist)
        else:
            self.endpoints_blacklist = []

        # Setup the queue and the background thread
        self.setup(app_id)

        request_started.connect(self._request_started)
Exemplo n.º 3
0
    def do_request(self, *args, **kwargs):
        store = {}
        tear_down_ = partial(tear_down, store)

        request_started.connect(set_up)
        request_finished.connect(tear_down_)
        template_rendered.connect(store_rendered_template)
        if message_flashed:
            message_flashed.connect(store_flashed_message)

        if self.use_session_scopes:
            scope = SessionScope(self.db)
            scope.push()
        try:
            response = super(TestApp, self).do_request(*args, **kwargs)
        finally:
            if self.use_session_scopes:
                scope.pop()
            template_rendered.disconnect(store_rendered_template)
            request_finished.disconnect(tear_down_)
            request_started.disconnect(set_up)
            if message_flashed:
                message_flashed.disconnect(store_flashed_message)

        response.session = store.get('session', {})
        response.flashes = store.get('flashes', [])
        response.contexts = dict(store.get('contexts', []))
        return response
Exemplo n.º 4
0
    def do_request(self, *args, **kwargs):
        store = {}
        tear_down_ = partial(tear_down, store)

        request_started.connect(set_up)
        request_finished.connect(tear_down_)
        template_rendered.connect(store_rendered_template)
        if message_flashed:
            message_flashed.connect(store_flashed_message)

        if self.use_session_scopes:
            scope = SessionScope(self.db)
            scope.push()
        try:
            response = super(TestApp, self).do_request(*args, **kwargs)
        finally:
            if self.use_session_scopes:
                scope.pop()
            template_rendered.disconnect(store_rendered_template)
            request_finished.disconnect(tear_down_)
            request_started.disconnect(set_up)
            if message_flashed:
                message_flashed.disconnect(store_flashed_message)

        response.session = store.get('session', {})
        response.flashes = store.get('flashes', [])
        response.contexts = dict(store.get('contexts', []))
        return response
Exemplo n.º 5
0
    def init_app(self, app):
        if self.name in app.extensions:
            return

        app.extensions[self.name] = self
        request_started.connect(self.reset_request, app)
        got_request_exception.connect(self.reset_request, app)
Exemplo n.º 6
0
    def setup_once():
        # type: () -> None
        try:
            version = tuple(map(int, FLASK_VERSION.split(".")[:3]))
        except (ValueError, TypeError):
            raise DidNotEnable("Unparsable Flask version: {}".format(FLASK_VERSION))

        if version < (0, 10):
            raise DidNotEnable("Flask 0.10 or newer is required.")

        request_started.connect(_request_started)
        got_request_exception.connect(_capture_exception)

        old_app = Flask.__call__

        def sentry_patched_wsgi_app(self, environ, start_response):
            # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
            if Hub.current.get_integration(FlaskIntegration) is None:
                return old_app(self, environ, start_response)

            return SentryWsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))(
                environ, start_response
            )

        Flask.__call__ = sentry_patched_wsgi_app  # type: ignore
Exemplo n.º 7
0
    def init_app(self, app):
        if self.name in app.extensions:
            return

        app.extensions[self.name] = self
        request_started.connect(self.reset_request, app)
        got_request_exception.connect(self.reset_request, app)
Exemplo n.º 8
0
 def init_app(self, app):
     if 'csrf' not in app.extensions:
         raise RuntimeError(
             'Please install flask_wtf.csrf.CsrfProtect() as "csrf" in extensions'
             ' before AbilianCsrf()')
     app.extensions['csrf'].error_handler(self.csrf_error_handler)
     app.extensions['csrf-handler'] = self
     request_started.connect(self.request_started, sender=app)
     app.before_request(self.before_request)
Exemplo n.º 9
0
 def init_app(self, app):
     if "csrf" not in app.extensions:
         raise RuntimeError(
             'Please install flask_wtf.csrf.CsrfProtect() as "csrf" in '
             "extensions before AbilianCsrf()"
         )
     app.extensions["csrf"].error_handler(self.csrf_error_handler)
     app.extensions["csrf-handler"] = self
     request_started.connect(self.request_started, sender=app)
     app.before_request(self.before_request)
Exemplo n.º 10
0
    def init_app(self, app):
        app.extensions["upstream"] = self
        request_started.connect(self.request_started, sender=app)
        request_finished.connect(self.request_finished, sender=app)
        user_loaded.connect(self.user_loaded, sender=app)

        config = app.config
        config.setdefault("ABILIAN_UPSTREAM_INFO_ENABLED", False)
        for key in ("ABILIAN_UPSTREAM_INFO_DISCARD", "ABILIAN_UPSTREAM_INFO_INCLUDE"):
            val = config.get(key, ())
            if val is not None:
                val = frozenset(val)
            config[key] = val
Exemplo n.º 11
0
    def init_app(self, app):
        app.extensions["upstream"] = self
        request_started.connect(self.request_started, sender=app)
        request_finished.connect(self.request_finished, sender=app)
        user_loaded.connect(self.user_loaded, sender=app)

        config = app.config
        config.setdefault("ABILIAN_UPSTREAM_INFO_ENABLED", False)
        for key in ("ABILIAN_UPSTREAM_INFO_DISCARD", "ABILIAN_UPSTREAM_INFO_INCLUDE"):
            val = config.get(key, ())
            if val is not None:
                val = frozenset(val)
            config[key] = val
Exemplo n.º 12
0
    def install(self):
        appcontext_pushed.connect(_push_appctx)
        appcontext_tearing_down.connect(_pop_appctx)
        request_started.connect(_request_started)
        got_request_exception.connect(_capture_exception)

        old_app = Flask.__call__

        def sentry_patched_wsgi_app(self, environ, start_response):
            return run_wsgi_app(lambda *a, **kw: old_app(self, *a, **kw),
                                environ, start_response)

        Flask.__call__ = sentry_patched_wsgi_app
Exemplo n.º 13
0
def add_errormator(app, config=None):
    """
        Adds Errormator to Flask,

        first looks at config var,then tries to read ERRORMATOR from app.config
    """
    if not config and app.config.get('ERRORMATOR'):
        config = app.config.get('ERRORMATOR')
    else:
        config = {}
    app.wsgi_app = make_errormator_middleware(app.wsgi_app, config)
    request_started.connect(populate_post_vars, app)
    got_request_exception.connect(log_exception, app)
    return app
Exemplo n.º 14
0
def add_errormator(app, config=None):
    """
        Adds Errormator to Flask,

        first looks at config var,then tries to read ERRORMATOR from app.config
    """
    if not config:
        config = app.config.get("ERRORMATOR")
    if not config:
        config = get_config()
    app.wsgi_app = make_errormator_middleware(app.wsgi_app, config)
    request_started.connect(populate_post_vars, app)
    got_request_exception.connect(log_exception, app)
    return app
Exemplo n.º 15
0
    def setup_once():
        appcontext_pushed.connect(_push_appctx)
        appcontext_tearing_down.connect(_pop_appctx)
        request_started.connect(_request_started)
        got_request_exception.connect(_capture_exception)

        old_app = Flask.__call__

        def sentry_patched_wsgi_app(self, environ, start_response):
            if Hub.current.get_integration(FlaskIntegration) is None:
                return old_app(self, environ, start_response)

            return run_wsgi_app(lambda *a, **kw: old_app(self, *a, **kw),
                                environ, start_response)

        Flask.__call__ = sentry_patched_wsgi_app
Exemplo n.º 16
0
def add_appenlight(app, config=None):
    """
        Adds Appenlight to Flask,

        first looks at config var,then tries to read APPENLIGHT from app.config
    """
    if not config and app.config.get('APPENLIGHT'):
        config = app.config.get('APPENLIGHT')
    if config:
        pass
    else:
        config = {}
    app.wsgi_app = make_appenlight_middleware(app.wsgi_app, config)
    request_started.connect(populate_post_vars, app)
    got_request_exception.connect(log_exception, app)
    return app
Exemplo n.º 17
0
def add_appenlight(app, config=None):
    """
        Adds Appenlight to Flask,

        first looks at config var,then tries to read APPENLIGHT from app.config
    """
    if not config and app.config.get('APPENLIGHT'):
        config = app.config.get('APPENLIGHT')
    if config:
        pass
    else:
        config = {}
    app.wsgi_app = make_appenlight_middleware(app.wsgi_app, config)
    request_started.connect(populate_post_vars, app)
    got_request_exception.connect(log_exception, app)
    return app
Exemplo n.º 18
0
    def setup_once():
        # type: () -> None
        appcontext_pushed.connect(_push_appctx)
        appcontext_tearing_down.connect(_pop_appctx)
        request_started.connect(_request_started)
        got_request_exception.connect(_capture_exception)

        old_app = Flask.__call__

        def sentry_patched_wsgi_app(self, environ, start_response):
            # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
            if Hub.current.get_integration(FlaskIntegration) is None:
                return old_app(self, environ, start_response)

            return SentryWsgiMiddleware(
                lambda *a, **kw: old_app(self, *a, **kw))(environ,
                                                          start_response)

        Flask.__call__ = sentry_patched_wsgi_app  # type: ignore
Exemplo n.º 19
0
def apply_middlewares(app):
    request_started.connect(_before_request, sender=app)
    request_finished.connect(_after_request, sender=app)
    got_request_exception.connect(_after_exception, sender=app)
Exemplo n.º 20
0
    db_session.expire_all()


request_finished.connect(expire_session, app)
""" Pass account list to template """


def accList_inject(sender):
    if not session.get('accSelect'):
        session['accSelect'] = [
            r.usern for r in db.session.query(eBayAccount.usern).filter_by(
                uid=login.current_user.get_id()).distinct()
        ]


request_started.connect(accList_inject, app)

# @app.after_request
# def add_header(r):
#     """
#     Add headers to both force latest IE rendering engine or Chrome Frame,
#     and also to cache the rendered page for 10 minutes.
#     """
#     r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
#     r.headers["Pragma"] = "no-cache"
#     r.headers["Expires"] = "0"
#     r.headers['Cache-Control'] = 'public, max-age=0'
#     return r

# import abay.views
#db.session.rollback()