@@ -132,10 +132,22 @@ def _get_address_port(settings):
132
132
return address , int (port ) if port is not None else None
133
133
134
134
135
- def patch_caching ():
136
- # type: () -> None
135
+ def should_enable_cache_spans ():
136
+ # type: () -> bool
137
137
from sentry_sdk .integrations .django import DjangoIntegration
138
138
139
+ client = sentry_sdk .get_client ()
140
+ integration = client .get_integration (DjangoIntegration )
141
+ from django .conf import settings
142
+
143
+ return integration is not None and (
144
+ (client .spotlight is not None and settings .DEBUG is True )
145
+ or integration .cache_spans is True
146
+ )
147
+
148
+
149
+ def patch_caching ():
150
+ # type: () -> None
139
151
if not hasattr (CacheHandler , "_sentry_patched" ):
140
152
if DJANGO_VERSION < (3 , 2 ):
141
153
original_get_item = CacheHandler .__getitem__
@@ -145,8 +157,7 @@ def sentry_get_item(self, alias):
145
157
# type: (CacheHandler, str) -> Any
146
158
cache = original_get_item (self , alias )
147
159
148
- integration = sentry_sdk .get_client ().get_integration (DjangoIntegration )
149
- if integration is not None and integration .cache_spans :
160
+ if should_enable_cache_spans ():
150
161
from django .conf import settings
151
162
152
163
address , port = _get_address_port (
@@ -168,8 +179,7 @@ def sentry_create_connection(self, alias):
168
179
# type: (CacheHandler, str) -> Any
169
180
cache = original_create_connection (self , alias )
170
181
171
- integration = sentry_sdk .get_client ().get_integration (DjangoIntegration )
172
- if integration is not None and integration .cache_spans :
182
+ if should_enable_cache_spans ():
173
183
address , port = _get_address_port (self .settings [alias or "default" ])
174
184
175
185
_patch_cache (cache , address , port )
0 commit comments