Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sentry_sdk/integrations/django/views.py: 76%
35 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from sentry_sdk.hub import Hub
2from sentry_sdk._types import MYPY
3from sentry_sdk import _functools
5if MYPY: 5 ↛ 6line 5 didn't jump to line 6, because the condition on line 5 was never true
6 from typing import Any
9try:
10 from asyncio import iscoroutinefunction
11except ImportError:
12 iscoroutinefunction = None # type: ignore
15try:
16 from sentry_sdk.integrations.django.asgi import wrap_async_view
17except (ImportError, SyntaxError):
18 wrap_async_view = None # type: ignore
21def patch_views():
22 # type: () -> None
24 from django.core.handlers.base import BaseHandler
25 from sentry_sdk.integrations.django import DjangoIntegration
27 old_make_view_atomic = BaseHandler.make_view_atomic
29 @_functools.wraps(old_make_view_atomic)
30 def sentry_patched_make_view_atomic(self, *args, **kwargs):
31 # type: (Any, *Any, **Any) -> Any
32 callback = old_make_view_atomic(self, *args, **kwargs)
34 # XXX: The wrapper function is created for every request. Find more
35 # efficient way to wrap views (or build a cache?)
37 hub = Hub.current
38 integration = hub.get_integration(DjangoIntegration)
40 if integration is not None and integration.middleware_spans: 40 ↛ 52line 40 didn't jump to line 52, because the condition on line 40 was never false
42 if ( 42 ↛ 47line 42 didn't jump to line 47
43 iscoroutinefunction is not None
44 and wrap_async_view is not None
45 and iscoroutinefunction(callback)
46 ):
47 sentry_wrapped_callback = wrap_async_view(hub, callback)
48 else:
49 sentry_wrapped_callback = _wrap_sync_view(hub, callback)
51 else:
52 sentry_wrapped_callback = callback
54 return sentry_wrapped_callback
56 BaseHandler.make_view_atomic = sentry_patched_make_view_atomic
59def _wrap_sync_view(hub, callback):
60 # type: (Hub, Any) -> Any
61 @_functools.wraps(callback)
62 def sentry_wrapped_callback(request, *args, **kwargs):
63 # type: (Any, *Any, **Any) -> Any
64 with hub.start_span(
65 op="django.view", description=request.resolver_match.view_name
66 ):
67 return callback(request, *args, **kwargs)
69 return sentry_wrapped_callback