Skip to content

Commit 119e251

Browse files
Sanic integration initial version
1 parent afc488d commit 119e251

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

sentry_sdk/integrations/sanic.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import weakref
33
from inspect import isawaitable
44

5+
from sentry_sdk import continue_trace
56
from sentry_sdk._compat import urlparse, reraise
67
from sentry_sdk.hub import Hub
78
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
@@ -27,6 +28,7 @@
2728
from typing import Dict
2829

2930
from sanic.request import Request, RequestParameters
31+
from sanic.response import BaseHTTPResponse
3032

3133
from sentry_sdk._types import Event, EventProcessor, Hint
3234
from sanic.router import Route
@@ -133,6 +135,7 @@ def _setup_sanic():
133135
# type: () -> None
134136
Sanic._startup = _startup
135137
ErrorHandler.lookup = _sentry_error_handler_lookup
138+
# _patch_sanic_asgi()
136139

137140

138141
def _setup_legacy_sanic():
@@ -142,6 +145,40 @@ def _setup_legacy_sanic():
142145
ErrorHandler.lookup = _sentry_error_handler_lookup
143146

144147

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+
145182
async def _startup(self):
146183
# type: (Sanic) -> None
147184
# This happens about as early in the lifecycle as possible, just after the
@@ -164,6 +201,7 @@ async def _startup(self):
164201

165202
async def _hub_enter(request):
166203
# type: (Request) -> None
204+
# breakpoint()
167205
hub = Hub.current
168206
request.ctx._sentry_do_integration = (
169207
hub.get_integration(SanicIntegration) is not None
@@ -180,9 +218,20 @@ async def _hub_enter(request):
180218
scope.clear_breadcrumbs()
181219
scope.add_event_processor(_make_request_processor(weak_request))
182220

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+
183228

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()
186235
request.ctx._sentry_hub.__exit__(None, None, None)
187236

188237

@@ -192,6 +241,7 @@ async def _set_transaction(request, route, **kwargs):
192241
if hub.get_integration(SanicIntegration) is not None:
193242
with capture_internal_exceptions():
194243
with hub.configure_scope() as scope:
244+
breakpoint()
195245
route_name = route.name.replace(request.app.name, "").strip(".")
196246
scope.set_transaction_name(
197247
route_name, source=TRANSACTION_SOURCE_COMPONENT
@@ -285,6 +335,7 @@ def _legacy_router_get(self, *args):
285335

286336
def _capture_exception(exception):
287337
# type: (Union[Tuple[Optional[type], Optional[BaseException], Any], BaseException]) -> None
338+
breakpoint()
288339
hub = Hub.current
289340
integration = hub.get_integration(SanicIntegration)
290341
if integration is None:

0 commit comments

Comments
 (0)