@@ -132,10 +132,21 @@ 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+ def patch_caching ():
149+ # type: () -> None
139150 if not hasattr (CacheHandler , "_sentry_patched" ):
140151 if DJANGO_VERSION < (3 , 2 ):
141152 original_get_item = CacheHandler .__getitem__
@@ -145,8 +156,7 @@ def sentry_get_item(self, alias):
145156 # type: (CacheHandler, str) -> Any
146157 cache = original_get_item (self , alias )
147158
148- integration = sentry_sdk .get_client ().get_integration (DjangoIntegration )
149- if integration is not None and integration .cache_spans :
159+ if should_enable_cache_spans ():
150160 from django .conf import settings
151161
152162 address , port = _get_address_port (
@@ -168,8 +178,7 @@ def sentry_create_connection(self, alias):
168178 # type: (CacheHandler, str) -> Any
169179 cache = original_create_connection (self , alias )
170180
171- integration = sentry_sdk .get_client ().get_integration (DjangoIntegration )
172- if integration is not None and integration .cache_spans :
181+ if should_enable_cache_spans ():
173182 address , port = _get_address_port (self .settings [alias or "default" ])
174183
175184 _patch_cache (cache , address , port )
0 commit comments