Repository: getsentry/sentry-python

Issue:
Why is tornado.application logger ignored?
It appears Sentry do not report any logs from tornado.application logger

https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/tornado.py#L53
    ignore_logger("tornado.application")

However, it seems the default logger used by Tornado to report unhandled exceptions:

https://github.com/tornadoweb/tornado/blob/682ff8dec2d095b339b62d875252be932ae4a5f2/tornado/web.py#L1762

    def log_exception(
        self,
        typ: "Optional[Type[BaseException]]",
        value: Optional[BaseException],
        tb: Optional[TracebackType],
    ) -> None:
        """Override to customize logging of uncaught exceptions.
        By default logs instances of `HTTPError` as warnings without
        stack traces (on the ``tornado.general`` logger), and all
        other exceptions as errors with stack traces (on the
        ``tornado.application`` logger).
        .. versionadded:: 3.1
        """
        if isinstance(value, HTTPError):
            if value.log_message:
                format = "%d %s: " + value.log_message
                args = [value.status_code, self._request_summary()] + list(value.args)
                gen_log.warning(format, *args)
        else:
            app_log.error(  # type: ignore
                "Uncaught exception %s\n%r",
                self._request_summary(),
                self.request,
                exc_info=(typ, value, tb),
            )

How are we supposed to propagate uncaught exceptions in a Tornado based application to Sentry with Tornado integration?

Thanks


Discussion:
The Tornado integration hooks into tornado via other means to handle uncaught exceptions. Specifically we monkeypatch `log_exception` with our own version that reports the exception and calls the old version.

Generally we ignore loggers that have stringified stacktraces logged on them, but this does not seem to be the case here. I can't remember the reason in this specific instance.

I believe we could change the tornado integration to remove this `ignore_logger` call, and perhaps even rely on the logger for crash reporting instead of the monkeypatching.

Is this currently a problem for you?
Our Python developper suggests to drop Sentry TornadoIntegration ; I would rather keep it but I would expect it to capture uncaught exceptions without additional coding on our part.

 -> The workaround is easy (dropping TornadoIntegration) but not satisfying.
The TornadoIntegration is supposed to capture uncaught exceptions, just not via the logging mechanism. Is your problem that you don't see crash reports in Sentry when enabling the integration?

Resolving patch:
diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py
--- a/sentry_sdk/integrations/tornado.py
+++ b/sentry_sdk/integrations/tornado.py
@@ -50,7 +50,6 @@ def setup_once():
                 "The tornado integration for Sentry requires Python 3.6+ or the aiocontextvars package"
             )
 
-        ignore_logger("tornado.application")
         ignore_logger("tornado.access")
 
         old_execute = RequestHandler._execute


Test patch:
diff --git a/tests/integrations/tornado/test_tornado.py b/tests/integrations/tornado/test_tornado.py
--- a/tests/integrations/tornado/test_tornado.py
+++ b/tests/integrations/tornado/test_tornado.py
@@ -54,6 +54,7 @@ def test_basic(tornado_testcase, sentry_init, capture_events):
     event, = events
     exception, = event["exception"]["values"]
     assert exception["type"] == "ZeroDivisionError"
+    assert exception["mechanism"]["type"] == "tornado"
 
     request = event["request"]
     host = request["headers"]["Host"]