|
27 | 27 |
|
28 | 28 | try: |
29 | 29 | from tornado import version_info as TORNADO_VERSION |
30 | | - from tornado.web import RequestHandler, HTTPError |
31 | 30 | from tornado.gen import coroutine |
| 31 | + from tornado.httputil import HTTPServerRequest |
| 32 | + from tornado.web import RequestHandler, HTTPError |
32 | 33 | except ImportError: |
33 | 34 | raise DidNotEnable("Tornado not installed") |
34 | 35 |
|
|
44 | 45 | from sentry_sdk._types import Event, EventProcessor |
45 | 46 |
|
46 | 47 |
|
| 48 | +REQUEST_PROPERTY_TO_ATTRIBUTE = { |
| 49 | + "method": "http.request.method", |
| 50 | + "path": "url.path", |
| 51 | + "query": "url.query", |
| 52 | + "protocol": "url.scheme", |
| 53 | +} |
| 54 | + |
| 55 | + |
47 | 56 | class TornadoIntegration(Integration): |
48 | 57 | identifier = "tornado" |
49 | 58 | origin = f"auto.http.{identifier}" |
@@ -124,7 +133,7 @@ def _handle_request_impl(self): |
124 | 133 | name="generic Tornado request", |
125 | 134 | source=TRANSACTION_SOURCE_ROUTE, |
126 | 135 | origin=TornadoIntegration.origin, |
127 | | - custom_sampling_context={"tornado_request": self.request}, |
| 136 | + attributes=_prepopulate_attributes(self.request), |
128 | 137 | ): |
129 | 138 | yield |
130 | 139 |
|
@@ -218,3 +227,36 @@ def files(self): |
218 | 227 | def size_of_file(self, file): |
219 | 228 | # type: (Any) -> int |
220 | 229 | return len(file.body or ()) |
| 230 | + |
| 231 | + |
| 232 | +def _prepopulate_attributes(request): |
| 233 | + # type: (HTTPServerRequest) -> dict[str, Any] |
| 234 | + # https://www.tornadoweb.org/en/stable/httputil.html#tornado.httputil.HTTPServerRequest |
| 235 | + attributes = {} |
| 236 | + |
| 237 | + for prop, attr in REQUEST_PROPERTY_TO_ATTRIBUTE.items(): |
| 238 | + if getattr(request, prop, None) is not None: |
| 239 | + attributes[attr] = getattr(request, prop) |
| 240 | + |
| 241 | + if getattr(request, "version", None): |
| 242 | + try: |
| 243 | + proto, version = request.version.split("/") |
| 244 | + attributes["network.protocol.name"] = proto |
| 245 | + attributes["network.protocol.version"] = version |
| 246 | + except ValueError: |
| 247 | + attributes["network.protocol.name"] = request.version |
| 248 | + |
| 249 | + if getattr(request, "host", None) is not None: |
| 250 | + try: |
| 251 | + address, port = request.host.split(":") |
| 252 | + attributes["server.address"] = address |
| 253 | + attributes["server.port"] = port |
| 254 | + except ValueError: |
| 255 | + attributes["server.address"] = request.host |
| 256 | + |
| 257 | + try: |
| 258 | + attributes["url.full"] = request.full_url() |
| 259 | + except Exception: |
| 260 | + pass |
| 261 | + |
| 262 | + return attributes |
0 commit comments