|
| 1 | +import sys |
| 2 | +import weakref |
| 3 | + |
| 4 | +from sentry_sdk.hub import Hub, _should_send_default_pii |
| 5 | +from sentry_sdk.utils import ( |
| 6 | + event_from_exception, |
| 7 | + capture_internal_exceptions, |
| 8 | + transaction_from_function, |
| 9 | +) |
| 10 | +from sentry_sdk.integrations import Integration |
| 11 | +from sentry_sdk.integrations._wsgi_common import ( |
| 12 | + RequestExtractor, |
| 13 | + _filter_headers, |
| 14 | + _is_json_content_type, |
| 15 | +) |
| 16 | +from sentry_sdk.integrations.logging import ignore_logger |
| 17 | + |
| 18 | +from tornado.web import RequestHandler, HTTPError |
| 19 | +from tornado.gen import coroutine |
| 20 | + |
| 21 | + |
| 22 | +class TornadoIntegration(Integration): |
| 23 | + identifier = "tornado" |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def setup_once(): |
| 27 | + import tornado |
| 28 | + |
| 29 | + tornado_version = getattr(tornado, "version_info", None) |
| 30 | + if tornado_version is None or tornado_version < (5, 0): |
| 31 | + raise RuntimeError("Tornado 5+ required") |
| 32 | + |
| 33 | + if sys.version_info < (3, 7): |
| 34 | + # Tornado is async. We better have contextvars or we're going to leak |
| 35 | + # state between requests. |
| 36 | + raise RuntimeError( |
| 37 | + "The tornado integration for Sentry requires Python 3.7+" |
| 38 | + ) |
| 39 | + |
| 40 | + ignore_logger("tornado.application") |
| 41 | + ignore_logger("tornado.access") |
| 42 | + |
| 43 | + old_execute = RequestHandler._execute |
| 44 | + |
| 45 | + @coroutine |
| 46 | + def sentry_execute_request_handler(self, *args, **kwargs): |
| 47 | + hub = Hub.current |
| 48 | + integration = hub.get_integration(TornadoIntegration) |
| 49 | + if integration is None: |
| 50 | + return old_execute(self, *args, **kwargs) |
| 51 | + |
| 52 | + weak_handler = weakref.ref(self) |
| 53 | + |
| 54 | + with Hub(hub) as hub: |
| 55 | + with hub.configure_scope() as scope: |
| 56 | + scope.add_event_processor(_make_event_processor(weak_handler)) |
| 57 | + result = yield from old_execute(self, *args, **kwargs) |
| 58 | + return result |
| 59 | + |
| 60 | + RequestHandler._execute = sentry_execute_request_handler |
| 61 | + |
| 62 | + old_log_exception = RequestHandler.log_exception |
| 63 | + |
| 64 | + def sentry_log_exception(self, ty, value, tb, *args, **kwargs): |
| 65 | + _capture_exception(ty, value, tb) |
| 66 | + return old_log_exception(self, ty, value, tb, *args, **kwargs) |
| 67 | + |
| 68 | + RequestHandler.log_exception = sentry_log_exception |
| 69 | + |
| 70 | + |
| 71 | +def _capture_exception(ty, value, tb): |
| 72 | + hub = Hub.current |
| 73 | + if hub.get_integration(TornadoIntegration) is None: |
| 74 | + return |
| 75 | + if isinstance(value, HTTPError): |
| 76 | + return |
| 77 | + |
| 78 | + event, hint = event_from_exception( |
| 79 | + (ty, value, tb), |
| 80 | + client_options=hub.client.options, |
| 81 | + mechanism={"type": "tornado", "handled": False}, |
| 82 | + ) |
| 83 | + |
| 84 | + hub.capture_event(event, hint=hint) |
| 85 | + |
| 86 | + |
| 87 | +def _make_event_processor(weak_handler): |
| 88 | + def tornado_processor(event, hint): |
| 89 | + handler = weak_handler() |
| 90 | + if handler is None: |
| 91 | + return event |
| 92 | + |
| 93 | + request = handler.request |
| 94 | + |
| 95 | + if "transaction" not in event: |
| 96 | + with capture_internal_exceptions(): |
| 97 | + method = getattr(handler, handler.request.method.lower()) |
| 98 | + event["transaction"] = transaction_from_function(method) |
| 99 | + |
| 100 | + with capture_internal_exceptions(): |
| 101 | + extractor = TornadoRequestExtractor(request) |
| 102 | + extractor.extract_into_event(event) |
| 103 | + |
| 104 | + request_info = event["request"] |
| 105 | + |
| 106 | + if "url" not in request_info: |
| 107 | + request_info["url"] = "%s://%s%s" % ( |
| 108 | + request.protocol, |
| 109 | + request.host, |
| 110 | + request.path, |
| 111 | + ) |
| 112 | + |
| 113 | + if "query_string" not in request_info: |
| 114 | + request_info["query_string"] = request.query |
| 115 | + |
| 116 | + if "method" not in request_info: |
| 117 | + request_info["method"] = request.method |
| 118 | + |
| 119 | + if "env" not in request_info: |
| 120 | + request_info["env"] = {"REMOTE_ADDR": request.remote_ip} |
| 121 | + |
| 122 | + if "headers" not in request_info: |
| 123 | + request_info["headers"] = _filter_headers(dict(request.headers)) |
| 124 | + |
| 125 | + with capture_internal_exceptions(): |
| 126 | + if handler.current_user and _should_send_default_pii(): |
| 127 | + event.setdefault("user", {})["is_authenticated"] = True |
| 128 | + |
| 129 | + return event |
| 130 | + |
| 131 | + return tornado_processor |
| 132 | + |
| 133 | + |
| 134 | +class TornadoRequestExtractor(RequestExtractor): |
| 135 | + def content_length(self): |
| 136 | + if self.request.body is None: |
| 137 | + return 0 |
| 138 | + return len(self.request.body) |
| 139 | + |
| 140 | + def cookies(self): |
| 141 | + return dict(self.request.cookies) |
| 142 | + |
| 143 | + def raw_data(self): |
| 144 | + return self.request.body |
| 145 | + |
| 146 | + def form(self): |
| 147 | + # TODO: Where to get formdata and nothing else? |
| 148 | + return None |
| 149 | + |
| 150 | + def is_json(self): |
| 151 | + return _is_json_content_type(self.request.headers.get("content-type")) |
| 152 | + |
| 153 | + def files(self): |
| 154 | + return {k: v[0] for k, v in self.request.files.items() if v} |
| 155 | + |
| 156 | + def size_of_file(self, file): |
| 157 | + return len(file.body or ()) |
0 commit comments