Skip to content

Commit 162773c

Browse files
authored
fix(integrations): Do not use convenience decorator (#3022)
1 parent a626f01 commit 162773c

File tree

13 files changed

+65
-35
lines changed

13 files changed

+65
-35
lines changed

sentry_sdk/integrations/aiohttp.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from sentry_sdk.utils import (
2222
capture_internal_exceptions,
2323
ensure_integration_enabled,
24-
ensure_integration_enabled_async,
2524
event_from_exception,
2625
logger,
2726
parse_url,
@@ -98,9 +97,11 @@ def setup_once():
9897

9998
old_handle = Application._handle
10099

101-
@ensure_integration_enabled_async(AioHttpIntegration, old_handle)
102100
async def sentry_app_handle(self, request, *args, **kwargs):
103101
# type: (Any, Request, *Any, **Any) -> Any
102+
if sentry_sdk.get_client().get_integration(AioHttpIntegration) is None:
103+
return await old_handle(self, request, *args, **kwargs)
104+
104105
weak_request = weakref.ref(request)
105106

106107
with sentry_sdk.isolation_scope() as scope:
@@ -190,9 +191,11 @@ def init(*args, **kwargs):
190191
def create_trace_config():
191192
# type: () -> TraceConfig
192193

193-
@ensure_integration_enabled_async(AioHttpIntegration)
194194
async def on_request_start(session, trace_config_ctx, params):
195195
# type: (ClientSession, SimpleNamespace, TraceRequestStartParams) -> None
196+
if sentry_sdk.get_client().get_integration(AioHttpIntegration) is None:
197+
return
198+
196199
method = params.method.upper()
197200

198201
parsed_url = None

sentry_sdk/integrations/arq.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sentry_sdk.utils import (
1111
capture_internal_exceptions,
1212
ensure_integration_enabled,
13-
ensure_integration_enabled_async,
1413
event_from_exception,
1514
SENSITIVE_DATA_SUBSTITUTE,
1615
parse_version,
@@ -71,9 +70,12 @@ def patch_enqueue_job():
7170
# type: () -> None
7271
old_enqueue_job = ArqRedis.enqueue_job
7372

74-
@ensure_integration_enabled_async(ArqIntegration, old_enqueue_job)
7573
async def _sentry_enqueue_job(self, function, *args, **kwargs):
7674
# type: (ArqRedis, str, *Any, **Any) -> Optional[Job]
75+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
76+
if integration is None:
77+
return await old_enqueue_job(self, function, *args, **kwargs)
78+
7779
with sentry_sdk.start_span(op=OP.QUEUE_SUBMIT_ARQ, description=function):
7880
return await old_enqueue_job(self, function, *args, **kwargs)
7981

@@ -84,9 +86,12 @@ def patch_run_job():
8486
# type: () -> None
8587
old_run_job = Worker.run_job
8688

87-
@ensure_integration_enabled_async(ArqIntegration, old_run_job)
8889
async def _sentry_run_job(self, job_id, score):
8990
# type: (Worker, str, int) -> None
91+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
92+
if integration is None:
93+
return await old_run_job(self, job_id, score)
94+
9095
with sentry_sdk.isolation_scope() as scope:
9196
scope._name = "arq"
9297
scope.clear_breadcrumbs()
@@ -157,9 +162,12 @@ def event_processor(event, hint):
157162
def _wrap_coroutine(name, coroutine):
158163
# type: (str, WorkerCoroutine) -> WorkerCoroutine
159164

160-
@ensure_integration_enabled_async(ArqIntegration, coroutine)
161165
async def _sentry_coroutine(ctx, *args, **kwargs):
162166
# type: (Dict[Any, Any], *Any, **Any) -> Any
167+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
168+
if integration is None:
169+
return await coroutine(ctx, *args, **kwargs)
170+
163171
Scope.get_isolation_scope().add_event_processor(
164172
_make_event_processor({**ctx, "job_name": name}, *args, **kwargs)
165173
)

sentry_sdk/integrations/asyncpg.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries
1010
from sentry_sdk.utils import (
1111
ensure_integration_enabled,
12-
ensure_integration_enabled_async,
1312
parse_version,
1413
capture_internal_exceptions,
1514
)
@@ -58,8 +57,10 @@ def setup_once() -> None:
5857

5958

6059
def _wrap_execute(f: Callable[..., Awaitable[T]]) -> Callable[..., Awaitable[T]]:
61-
@ensure_integration_enabled_async(AsyncPGIntegration, f)
6260
async def _inner(*args: Any, **kwargs: Any) -> T:
61+
if sentry_sdk.get_client().get_integration(AsyncPGIntegration) is None:
62+
return await f(*args, **kwargs)
63+
6364
# Avoid recording calls to _execute twice.
6465
# Calls to Connection.execute with args also call
6566
# Connection._execute, which is recorded separately
@@ -110,8 +111,9 @@ def _record(
110111
def _wrap_connection_method(
111112
f: Callable[..., Awaitable[T]], *, executemany: bool = False
112113
) -> Callable[..., Awaitable[T]]:
113-
@ensure_integration_enabled_async(AsyncPGIntegration, f)
114114
async def _inner(*args: Any, **kwargs: Any) -> T:
115+
if sentry_sdk.get_client().get_integration(AsyncPGIntegration) is None:
116+
return await f(*args, **kwargs)
115117
query = args[1]
116118
params_list = args[2] if len(args) > 2 else None
117119
with _record(None, query, params_list, executemany=executemany) as span:
@@ -145,8 +147,10 @@ def _inner(*args: Any, **kwargs: Any) -> T: # noqa: N807
145147

146148

147149
def _wrap_connect_addr(f: Callable[..., Awaitable[T]]) -> Callable[..., Awaitable[T]]:
148-
@ensure_integration_enabled_async(AsyncPGIntegration, f)
149150
async def _inner(*args: Any, **kwargs: Any) -> T:
151+
if sentry_sdk.get_client().get_integration(AsyncPGIntegration) is None:
152+
return await f(*args, **kwargs)
153+
150154
user = kwargs["params"].user
151155
database = kwargs["params"].database
152156

sentry_sdk/integrations/django/asgi.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from sentry_sdk.utils import (
2222
capture_internal_exceptions,
2323
ensure_integration_enabled,
24-
ensure_integration_enabled_async,
2524
)
2625

2726

@@ -72,9 +71,11 @@ def patch_django_asgi_handler_impl(cls):
7271

7372
old_app = cls.__call__
7473

75-
@ensure_integration_enabled_async(DjangoIntegration, old_app)
7674
async def sentry_patched_asgi_handler(self, scope, receive, send):
7775
# type: (Any, Any, Any, Any) -> Any
76+
if sentry_sdk.get_client().get_integration(DjangoIntegration) is None:
77+
return await old_app(self, scope, receive, send)
78+
7879
middleware = SentryAsgiMiddleware(
7980
old_app.__get__(self, cls), unsafe_context_data=True
8081
)._run_asgi3
@@ -120,9 +121,11 @@ def patch_channels_asgi_handler_impl(cls):
120121
if channels.__version__ < "3.0.0":
121122
old_app = cls.__call__
122123

123-
@ensure_integration_enabled_async(DjangoIntegration, old_app)
124124
async def sentry_patched_asgi_handler(self, receive, send):
125125
# type: (Any, Any, Any) -> Any
126+
if sentry_sdk.get_client().get_integration(DjangoIntegration) is None:
127+
return await old_app(self, receive, send)
128+
126129
middleware = SentryAsgiMiddleware(
127130
lambda _scope: old_app.__get__(self, cls), unsafe_context_data=True
128131
)

sentry_sdk/integrations/fastapi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sentry_sdk.utils import (
1111
transaction_from_function,
1212
logger,
13-
ensure_integration_enabled_async,
1413
)
1514

1615
if TYPE_CHECKING:
@@ -97,9 +96,11 @@ def _sentry_call(*args, **kwargs):
9796

9897
old_app = old_get_request_handler(*args, **kwargs)
9998

100-
@ensure_integration_enabled_async(FastApiIntegration, old_app)
10199
async def _sentry_app(*args, **kwargs):
102100
# type: (*Any, **Any) -> Any
101+
if sentry_sdk.get_client().get_integration(FastApiIntegration) is None:
102+
return await old_app(*args, **kwargs)
103+
103104
integration = sentry_sdk.get_client().get_integration(FastApiIntegration)
104105
request = args[0]
105106

sentry_sdk/integrations/graphene.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from sentry_sdk.utils import (
55
capture_internal_exceptions,
66
ensure_integration_enabled,
7-
ensure_integration_enabled_async,
87
event_from_exception,
98
package_version,
109
)
@@ -69,9 +68,11 @@ def _sentry_patched_graphql_sync(schema, source, *args, **kwargs):
6968

7069
return result
7170

72-
@ensure_integration_enabled_async(GrapheneIntegration, old_graphql_async)
7371
async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
7472
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
73+
if sentry_sdk.get_client().get_integration(GrapheneIntegration) is None:
74+
return await old_graphql_async(schema, source, *args, **kwargs)
75+
7576
scope = Scope.get_isolation_scope()
7677
scope.add_event_processor(_event_processor)
7778

sentry_sdk/integrations/httpx.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
SENSITIVE_DATA_SUBSTITUTE,
99
capture_internal_exceptions,
1010
ensure_integration_enabled,
11-
ensure_integration_enabled_async,
1211
logger,
1312
parse_url,
1413
)
@@ -98,9 +97,11 @@ def _install_httpx_async_client():
9897
# type: () -> None
9998
real_send = AsyncClient.send
10099

101-
@ensure_integration_enabled_async(HttpxIntegration, real_send)
102100
async def send(self, request, **kwargs):
103101
# type: (AsyncClient, Request, **Any) -> Response
102+
if sentry_sdk.get_client().get_integration(HttpxIntegration) is None:
103+
return await real_send(self, request, **kwargs)
104+
104105
parsed_url = None
105106
with capture_internal_exceptions():
106107
parsed_url = parse_url(str(request.url), sanitize=False)

sentry_sdk/integrations/quart.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from sentry_sdk.utils import (
1313
capture_internal_exceptions,
1414
ensure_integration_enabled,
15-
ensure_integration_enabled_async,
1615
event_from_exception,
1716
)
1817
from sentry_sdk._types import TYPE_CHECKING
@@ -150,10 +149,11 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
150149
pass
151150

152151

153-
@ensure_integration_enabled_async(QuartIntegration)
154152
async def _request_websocket_started(app, **kwargs):
155153
# type: (Quart, **Any) -> None
156154
integration = sentry_sdk.get_client().get_integration(QuartIntegration)
155+
if integration is None:
156+
return
157157

158158
if has_request_context():
159159
request_websocket = request._get_current_object()
@@ -200,9 +200,12 @@ def inner(event, hint):
200200
return inner
201201

202202

203-
@ensure_integration_enabled_async(QuartIntegration)
204203
async def _capture_exception(sender, exception, **kwargs):
205204
# type: (Quart, Union[ValueError, BaseException], **Any) -> None
205+
integration = sentry_sdk.get_client().get_integration(QuartIntegration)
206+
if integration is None:
207+
return
208+
206209
event, hint = event_from_exception(
207210
exception,
208211
client_options=sentry_sdk.get_client().options,

sentry_sdk/integrations/redis/asyncio.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sentry_sdk.tracing import Span
1111
from sentry_sdk.utils import (
1212
capture_internal_exceptions,
13-
ensure_integration_enabled_async,
1413
)
1514

1615
if TYPE_CHECKING:
@@ -26,9 +25,11 @@ def patch_redis_async_pipeline(
2625
# type: (Union[type[Pipeline[Any]], type[ClusterPipeline[Any]]], bool, Any, Callable[[Span, Any], None]) -> None
2726
old_execute = pipeline_cls.execute
2827

29-
@ensure_integration_enabled_async(RedisIntegration, old_execute)
3028
async def _sentry_execute(self, *args, **kwargs):
3129
# type: (Any, *Any, **Any) -> Any
30+
if sentry_sdk.get_client().get_integration(RedisIntegration) is None:
31+
return await old_execute(self, *args, **kwargs)
32+
3233
with sentry_sdk.start_span(
3334
op=OP.DB_REDIS, description="redis.pipeline.execute"
3435
) as span:
@@ -51,9 +52,11 @@ def patch_redis_async_client(cls, is_cluster, set_db_data_fn):
5152
# type: (Union[type[StrictRedis[Any]], type[RedisCluster[Any]]], bool, Callable[[Span, Any], None]) -> None
5253
old_execute_command = cls.execute_command
5354

54-
@ensure_integration_enabled_async(RedisIntegration, old_execute_command) # type: ignore
5555
async def _sentry_execute_command(self, name, *args, **kwargs):
5656
# type: (Any, str, *Any, **Any) -> Any
57+
if sentry_sdk.get_client().get_integration(RedisIntegration) is None:
58+
return await old_execute_command(self, name, *args, **kwargs)
59+
5760
description = _get_span_description(name, *args)
5861

5962
with sentry_sdk.start_span(op=OP.DB_REDIS, description=description) as span:

sentry_sdk/integrations/sanic.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from sentry_sdk.utils import (
1515
capture_internal_exceptions,
1616
ensure_integration_enabled,
17-
ensure_integration_enabled_async,
1817
event_from_exception,
1918
HAS_REAL_CONTEXTVARS,
2019
CONTEXTVARS_ERROR_MESSAGE,
@@ -274,9 +273,11 @@ async def sentry_wrapped_error_handler(request, exception):
274273
return sentry_wrapped_error_handler
275274

276275

277-
@ensure_integration_enabled_async(SanicIntegration, old_handle_request)
278276
async def _legacy_handle_request(self, request, *args, **kwargs):
279277
# type: (Any, Request, *Any, **Any) -> Any
278+
if sentry_sdk.get_client().get_integration(SanicIntegration) is None:
279+
return await old_handle_request(self, request, *args, **kwargs)
280+
280281
weak_request = weakref.ref(request)
281282

282283
with sentry_sdk.isolation_scope() as scope:

sentry_sdk/integrations/starlette.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
AnnotatedValue,
2222
capture_internal_exceptions,
2323
ensure_integration_enabled,
24-
ensure_integration_enabled_async,
2524
event_from_exception,
2625
logger,
2726
parse_version,
@@ -337,10 +336,11 @@ def patch_asgi_app():
337336
"""
338337
old_app = Starlette.__call__
339338

340-
@ensure_integration_enabled_async(StarletteIntegration, old_app)
341339
async def _sentry_patched_asgi_app(self, scope, receive, send):
342340
# type: (Starlette, StarletteScope, Receive, Send) -> None
343341
integration = sentry_sdk.get_client().get_integration(StarletteIntegration)
342+
if integration is None:
343+
return await old_app(self, scope, receive, send)
344344

345345
middleware = SentryAsgiMiddleware(
346346
lambda *a, **kw: old_app(self, *a, **kw),
@@ -377,12 +377,13 @@ def _sentry_request_response(func):
377377
is_coroutine = _is_async_callable(old_func)
378378
if is_coroutine:
379379

380-
@ensure_integration_enabled_async(StarletteIntegration, old_func)
381380
async def _sentry_async_func(*args, **kwargs):
382381
# type: (*Any, **Any) -> Any
383382
integration = sentry_sdk.get_client().get_integration(
384383
StarletteIntegration
385384
)
385+
if integration is None:
386+
return await old_func(*args, **kwargs)
386387

387388
request = args[0]
388389

sentry_sdk/integrations/starlite.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
99
from sentry_sdk.utils import (
1010
ensure_integration_enabled,
11-
ensure_integration_enabled_async,
1211
event_from_exception,
1312
transaction_from_function,
1413
)
@@ -179,10 +178,11 @@ async def _sentry_send(message: "Message") -> None:
179178
def patch_http_route_handle() -> None:
180179
old_handle = HTTPRoute.handle
181180

182-
@ensure_integration_enabled_async(StarliteIntegration, old_handle)
183181
async def handle_wrapper(
184182
self: "HTTPRoute", scope: "HTTPScope", receive: "Receive", send: "Send"
185183
) -> None:
184+
if sentry_sdk.get_client().get_integration(StarliteIntegration) is None:
185+
return await old_handle(self, scope, receive, send)
186186

187187
sentry_scope = SentryScope.get_isolation_scope()
188188
request: "Request[Any, Any]" = scope["app"].request_class(

sentry_sdk/integrations/strawberry.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from sentry_sdk.utils import (
1010
capture_internal_exceptions,
1111
ensure_integration_enabled,
12-
ensure_integration_enabled_async,
1312
event_from_exception,
1413
logger,
1514
package_version,
@@ -266,11 +265,13 @@ def _patch_execute():
266265
old_execute_async = strawberry_schema.execute
267266
old_execute_sync = strawberry_schema.execute_sync
268267

269-
@ensure_integration_enabled_async(StrawberryIntegration, old_execute_async)
270268
async def _sentry_patched_execute_async(*args, **kwargs):
271269
# type: (Any, Any) -> ExecutionResult
272270
result = await old_execute_async(*args, **kwargs)
273271

272+
if sentry_sdk.get_client().get_integration(StrawberryIntegration) is None:
273+
return result
274+
274275
if "execution_context" in kwargs and result.errors:
275276
scope = Scope.get_isolation_scope()
276277
event_processor = _make_request_event_processor(kwargs["execution_context"])

0 commit comments

Comments
 (0)