@@ -132,10 +132,22 @@ def _get_address_port(settings):
132132 return address , int (port ) if port is not None else None
133133
134134
135- def patch_caching ():
136- # type: () -> None
135+ def should_enable_cache_spans ():
136+ # type: () -> bool
137137 from sentry_sdk .integrations .django import DjangoIntegration
138138
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
139151 if not hasattr (CacheHandler , "_sentry_patched" ):
140152 if DJANGO_VERSION < (3 , 2 ):
141153 original_get_item = CacheHandler .__getitem__
@@ -145,8 +157,7 @@ def sentry_get_item(self, alias):
145157 # type: (CacheHandler, str) -> Any
146158 cache = original_get_item (self , alias )
147159
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 ():
150161 from django .conf import settings
151162
152163 address , port = _get_address_port (
@@ -168,8 +179,7 @@ def sentry_create_connection(self, alias):
168179 # type: (CacheHandler, str) -> Any
169180 cache = original_create_connection (self , alias )
170181
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 ():
173183 address , port = _get_address_port (self .settings [alias or "default" ])
174184
175185 _patch_cache (cache , address , port )
0 commit comments