diff --git a/c2cwsgiutils/profiler.py b/c2cwsgiutils/profiler.py index 91db1f017..ef98bf6fb 100644 --- a/c2cwsgiutils/profiler.py +++ b/c2cwsgiutils/profiler.py @@ -10,11 +10,15 @@ def filter_wsgi_app(application: Callable) -> Callable: if _PATH != "": - import linesman.middleware - LOG.info("Enable WSGI filter for the profiler on %s", _PATH) - linesman.middleware.ENABLED_FLAG_FILE = os.path.join(gettempdir(), 'linesman-enabled') - return linesman.middleware.ProfilingMiddleware( - app=application, profiler_path=_PATH, chart_packages=_MODULES, - filename=os.path.join(gettempdir(), 'linesman-graph-sessions.db')) + try: + import linesman.middleware + LOG.info("Enable WSGI filter for the profiler on %s", _PATH) + linesman.middleware.ENABLED_FLAG_FILE = os.path.join(gettempdir(), 'linesman-enabled') + return linesman.middleware.ProfilingMiddleware( + app=application, profiler_path=_PATH, chart_packages=_MODULES, + filename=os.path.join(gettempdir(), 'linesman-graph-sessions.db')) + except Exception: + LOG.error("Failed enabling the profiler. Continuing without it.", exc_info=True) + return application else: return application diff --git a/c2cwsgiutils/sentry.py b/c2cwsgiutils/sentry.py index 10c61d6f1..f9b7ed7d8 100644 --- a/c2cwsgiutils/sentry.py +++ b/c2cwsgiutils/sentry.py @@ -63,7 +63,11 @@ def filter_wsgi_app(application: Callable) -> Callable: """ global client if client is not None: - LOG.info("Enable WSGI filter for Sentry") - return middleware.Sentry(application, client) + try: + LOG.info("Enable WSGI filter for Sentry") + return middleware.Sentry(application, client) + except Exception: + LOG.error("Failed enabling sentry. Continuing without it.", exc_info=True) + return application else: return application diff --git a/c2cwsgiutils/wsgi_app.py b/c2cwsgiutils/wsgi_app.py index c0e2282f0..122bbefdd 100755 --- a/c2cwsgiutils/wsgi_app.py +++ b/c2cwsgiutils/wsgi_app.py @@ -7,8 +7,14 @@ def create() -> Callable: # pragma: no cover - from c2cwsgiutils import wsgi, sentry, profiler - return sentry.filter_wsgi_app(profiler.filter_wsgi_app(wsgi.create_application())) + # first import and initialize the wsgi application in order to have the logs setup before importing + # anything else + from c2cwsgiutils import wsgi + main_app = wsgi.create_application() + + # then, we can setup a few filters + from c2cwsgiutils import sentry, profiler + return sentry.filter_wsgi_app(profiler.filter_wsgi_app(main_app)) application = create() # pragma: no cover