Skip to content

Commit 0b1f358

Browse files
committed
feat(spotlight): Auto enable cache_spans for Spotlight on DEBUG
This patch enables `cache_spans` in Django integration automatically when Spotlight is enabled and `DEBUG` is set in Django settings.
1 parent da0b086 commit 0b1f358

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

sentry_sdk/integrations/django/caching.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)