|
7 | 7 | from django import VERSION as DJANGO_VERSION
|
8 | 8 |
|
9 | 9 | from sentry_sdk import Hub
|
10 |
| -from sentry_sdk.utils import ContextVar, transaction_from_function |
| 10 | +from sentry_sdk.utils import ( |
| 11 | + ContextVar, |
| 12 | + transaction_from_function, |
| 13 | + capture_internal_exceptions, |
| 14 | +) |
11 | 15 |
|
12 | 16 | from sentry_sdk._types import MYPY
|
13 | 17 |
|
@@ -64,29 +68,36 @@ def _wrap_middleware(middleware, middleware_name):
|
64 | 68 |
|
65 | 69 | def _get_wrapped_method(old_method):
|
66 | 70 | # type: (F) -> F
|
67 |
| - @wraps(old_method) |
68 |
| - def sentry_wrapped_method(*args, **kwargs): |
69 |
| - # type: (*Any, **Any) -> Any |
70 |
| - hub = Hub.current |
71 |
| - integration = hub.get_integration(DjangoIntegration) |
72 |
| - if integration is None or not integration.middleware_spans: |
73 |
| - return old_method(*args, **kwargs) |
74 |
| - |
75 |
| - function_name = transaction_from_function(old_method) |
76 |
| - |
77 |
| - description = middleware_name |
78 |
| - function_basename = getattr(old_method, "__name__", None) |
79 |
| - if function_basename: |
80 |
| - description = "{}.{}".format(description, function_basename) |
81 |
| - |
82 |
| - with hub.start_span( |
83 |
| - op="django.middleware", description=description |
84 |
| - ) as span: |
85 |
| - span.set_tag("django.function_name", function_name) |
86 |
| - span.set_tag("django.middleware_name", middleware_name) |
87 |
| - return old_method(*args, **kwargs) |
88 |
| - |
89 |
| - return sentry_wrapped_method # type: ignore |
| 71 | + with capture_internal_exceptions(): |
| 72 | + |
| 73 | + def sentry_wrapped_method(*args, **kwargs): |
| 74 | + # type: (*Any, **Any) -> Any |
| 75 | + hub = Hub.current |
| 76 | + integration = hub.get_integration(DjangoIntegration) |
| 77 | + if integration is None or not integration.middleware_spans: |
| 78 | + return old_method(*args, **kwargs) |
| 79 | + |
| 80 | + function_name = transaction_from_function(old_method) |
| 81 | + |
| 82 | + description = middleware_name |
| 83 | + function_basename = getattr(old_method, "__name__", None) |
| 84 | + if function_basename: |
| 85 | + description = "{}.{}".format(description, function_basename) |
| 86 | + |
| 87 | + with hub.start_span( |
| 88 | + op="django.middleware", description=description |
| 89 | + ) as span: |
| 90 | + span.set_tag("django.function_name", function_name) |
| 91 | + span.set_tag("django.middleware_name", middleware_name) |
| 92 | + return old_method(*args, **kwargs) |
| 93 | + |
| 94 | + try: |
| 95 | + # fails for __call__ of function on Python 2 (see py2.7-django-1.11) |
| 96 | + return wraps(old_method)(sentry_wrapped_method) # type: ignore |
| 97 | + except Exception: |
| 98 | + return sentry_wrapped_method # type: ignore |
| 99 | + |
| 100 | + return old_method |
90 | 101 |
|
91 | 102 | class SentryWrappingMiddleware(object):
|
92 | 103 | def __init__(self, *args, **kwargs):
|
|
0 commit comments