2
2
import weakref
3
3
from inspect import isawaitable
4
4
5
+ from sentry_sdk import continue_trace
5
6
from sentry_sdk ._compat import urlparse , reraise
6
7
from sentry_sdk .hub import Hub
7
8
from sentry_sdk .tracing import TRANSACTION_SOURCE_COMPONENT
27
28
from typing import Dict
28
29
29
30
from sanic .request import Request , RequestParameters
31
+ from sanic .response import BaseHTTPResponse
30
32
31
33
from sentry_sdk ._types import Event , EventProcessor , Hint
32
34
from sanic .router import Route
@@ -133,6 +135,7 @@ def _setup_sanic():
133
135
# type: () -> None
134
136
Sanic ._startup = _startup
135
137
ErrorHandler .lookup = _sentry_error_handler_lookup
138
+ # _patch_sanic_asgi()
136
139
137
140
138
141
def _setup_legacy_sanic ():
@@ -142,6 +145,40 @@ def _setup_legacy_sanic():
142
145
ErrorHandler .lookup = _sentry_error_handler_lookup
143
146
144
147
148
+ # def _patch_sanic_asgi():
149
+ # # type: () -> None
150
+ # old_app = Sanic.__call__
151
+
152
+ # print("Patching")
153
+ # breakpoint()
154
+
155
+ # @integration_patched_async(old_app, SanicIntegration)
156
+ # async def _sentry_patched_sanic(self, *args, **kwargs):
157
+ # # type: (Sanic, *object, **object) -> object
158
+ # print("Patched")
159
+ # # middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, *kw))
160
+
161
+ # middleware = SentryAsgiMiddleware(old_app)
162
+ # middleware.__call__ = middleware._run_asgi2
163
+
164
+ # breakpoint()
165
+
166
+ # return await middleware(self, *args, **kwargs)
167
+
168
+ # Sanic.__call__ = _sentry_patched_sanic
169
+
170
+
171
+ # def _patch_sanic_asgi():
172
+ # old_init = Sanic.__init__
173
+
174
+ # @wraps(Sanic.__init__)
175
+ # def _sentry_patched_init(self, *args, **kwargs):
176
+ # old_init(self, *args, **kwargs)
177
+ # self.register_middleware(SentryAsgiMiddleware(self), "request")
178
+
179
+ # Sanic.__init__ = _sentry_patched_init
180
+
181
+
145
182
async def _startup (self ):
146
183
# type: (Sanic) -> None
147
184
# This happens about as early in the lifecycle as possible, just after the
@@ -164,6 +201,7 @@ async def _startup(self):
164
201
165
202
async def _hub_enter (request ):
166
203
# type: (Request) -> None
204
+ # breakpoint()
167
205
hub = Hub .current
168
206
request .ctx ._sentry_do_integration = (
169
207
hub .get_integration (SanicIntegration ) is not None
@@ -180,9 +218,20 @@ async def _hub_enter(request):
180
218
scope .clear_breadcrumbs ()
181
219
scope .add_event_processor (_make_request_processor (weak_request ))
182
220
221
+ transaction = continue_trace (
222
+ dict (request .headers ), op = "http.server" , name = request .server_path
223
+ )
224
+ request .ctx ._sentry_transaction = request .ctx ._sentry_hub .start_transaction (
225
+ transaction
226
+ )
227
+
183
228
184
- async def _hub_exit (request , ** _ ):
185
- # type: (Request, **Any) -> None
229
+ async def _hub_exit (request , response ):
230
+ # type: (Request, BaseHTTPResponse) -> None
231
+ # TODO: Should these lines go in a try block in case the context got modified?
232
+ breakpoint ()
233
+ request .ctx ._sentry_transaction .set_http_status (response .status )
234
+ request .ctx ._sentry_transaction .finish ()
186
235
request .ctx ._sentry_hub .__exit__ (None , None , None )
187
236
188
237
@@ -192,6 +241,7 @@ async def _set_transaction(request, route, **kwargs):
192
241
if hub .get_integration (SanicIntegration ) is not None :
193
242
with capture_internal_exceptions ():
194
243
with hub .configure_scope () as scope :
244
+ breakpoint ()
195
245
route_name = route .name .replace (request .app .name , "" ).strip ("." )
196
246
scope .set_transaction_name (
197
247
route_name , source = TRANSACTION_SOURCE_COMPONENT
@@ -285,6 +335,7 @@ def _legacy_router_get(self, *args):
285
335
286
336
def _capture_exception (exception ):
287
337
# type: (Union[Tuple[Optional[type], Optional[BaseException], Any], BaseException]) -> None
338
+ breakpoint ()
288
339
hub = Hub .current
289
340
integration = hub .get_integration (SanicIntegration )
290
341
if integration is None :
0 commit comments