From 4f2f430239d26613f51ebd461ae3d08bd2b0f151 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 24 Oct 2025 14:52:56 +0200 Subject: [PATCH 01/13] fix(django): Improve logic for classifying cache hits or misses --- sentry_sdk/integrations/django/caching.py | 84 ++++++++++++++++++----- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index 7985611761..f21ada111d 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -20,19 +20,72 @@ from typing import Optional -METHODS_TO_INSTRUMENT = [ - "set", - "set_many", - "get", - "get_many", -] - - def _get_span_description(method_name, args, kwargs): # type: (str, tuple[Any], dict[str, Any]) -> str return _key_as_string(_get_safe_key(method_name, args, kwargs)) +def _set_address_and_port(span, address, port): + # type: (sentry_sdk.Span, Optional[str], Optional[int]) -> None + if address is not None: + span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, address) + + if port is not None: + span.set_data(SPANDATA.NETWORK_PEER_PORT, port) + + +def _patch_get_cache(cache, address, port): + # type: (CacheHandler, Optional[str], Optional[int]) -> None + from sentry_sdk.integrations.django import DjangoIntegration + + original_method = cache.get + + @ensure_integration_enabled(DjangoIntegration, original_method) + def _instrument_call(cache, original_method, args, kwargs, address, port): + # type: (CacheHandler, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any + op = OP.CACHE_GET + description = _get_span_description("get", args, kwargs) + + default_value = None + if len(args) >= 2: + default_value = args[1] + elif "default" in kwargs: + default_value = kwargs["default"] + + with sentry_sdk.start_span( + op=op, + name=description, + origin=DjangoIntegration.origin, + ) as span: + value = original_method(*args, **kwargs) + + with capture_internal_exceptions(): + _set_address_and_port(span, address, port) + + key = _get_safe_key("get", args, kwargs) + if key is not None: + span.set_data(SPANDATA.CACHE_KEY, key) + + item_size = None + if value != default_value: + item_size = len(str(value)) + span.set_data(SPANDATA.CACHE_HIT, True) + else: + span.set_data(SPANDATA.CACHE_HIT, False) + + if item_size is not None: + span.set_data(SPANDATA.CACHE_ITEM_SIZE, item_size) + + return value + + @functools.wraps(original_method) + def sentry_method(*args, **kwargs): + # type: (*Any, **Any) -> Any + return _instrument_call(cache, original_method, args, kwargs, address, port) + + setattr(cache, sentry_method) + + def _patch_cache_method(cache, method_name, address, port): # type: (CacheHandler, str, Optional[str], Optional[int]) -> None from sentry_sdk.integrations.django import DjangoIntegration @@ -58,11 +111,7 @@ def _instrument_call( value = original_method(*args, **kwargs) with capture_internal_exceptions(): - if address is not None: - span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, address) - - if port is not None: - span.set_data(SPANDATA.NETWORK_PEER_PORT, port) + _set_address_and_port(span, address, port) key = _get_safe_key(method_name, args, kwargs) if key is not None: @@ -70,7 +119,7 @@ def _instrument_call( item_size = None if is_get_operation: - if value: + if value is not None: item_size = len(str(value)) span.set_data(SPANDATA.CACHE_HIT, True) else: @@ -102,8 +151,11 @@ def sentry_method(*args, **kwargs): def _patch_cache(cache, address=None, port=None): # type: (CacheHandler, Optional[str], Optional[int]) -> None if not hasattr(cache, "_sentry_patched"): - for method_name in METHODS_TO_INSTRUMENT: - _patch_cache_method(cache, method_name, address, port) + _patch_cache_method(cache, "set", address, port) + _patch_cache_method(cache, "set_many", address, port) + # Seperate patch as you can overwrite value on cache-miss + _patch_get_cache(cache, address, port) + _patch_cache_method(cache, "get_many", address, port) cache._sentry_patched = True From 3d1a5394c043163b5e5914b43e4e7068745fdf00 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 10:13:54 +0100 Subject: [PATCH 02/13] . --- sentry_sdk/integrations/django/caching.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index f21ada111d..221183f55d 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -83,7 +83,7 @@ def sentry_method(*args, **kwargs): # type: (*Any, **Any) -> Any return _instrument_call(cache, original_method, args, kwargs, address, port) - setattr(cache, sentry_method) + setattr(cache, "get", sentry_method) def _patch_cache_method(cache, method_name, address, port): From 780a2d384b1780989af9b18d0408889b37fa1193 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 10:34:52 +0100 Subject: [PATCH 03/13] fix existing tests --- tests/integrations/django/test_cache_module.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index bc58dd8471..f3f9ed50ce 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -223,8 +223,8 @@ def test_cache_spans_middleware( assert second_event["spans"][0]["data"]["cache.key"][0].startswith( "views.decorators.cache.cache_header." ) - assert not second_event["spans"][0]["data"]["cache.hit"] - assert "cache.item_size" not in second_event["spans"][0]["data"] + assert second_event["spans"][0]["data"]["cache.hit"] + assert "cache.item_size" in second_event["spans"][0]["data"] # second_event - cache.get 2 assert second_event["spans"][1]["op"] == "cache.get" assert second_event["spans"][1]["description"].startswith( @@ -501,8 +501,8 @@ def test_cache_spans_item_size(sentry_init, client, capture_events, use_django_c assert len(second_event["spans"]) == 2 assert second_event["spans"][0]["op"] == "cache.get" - assert not second_event["spans"][0]["data"]["cache.hit"] - assert "cache.item_size" not in second_event["spans"][0]["data"] + assert second_event["spans"][0]["data"]["cache.hit"] + assert "cache.item_size" in second_event["spans"][0]["data"] assert second_event["spans"][1]["op"] == "cache.get" assert second_event["spans"][1]["data"]["cache.hit"] From 41fe2726260ebe4ede9b78842af571ec71fec9ee Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 10:59:13 +0100 Subject: [PATCH 04/13] test new behaviour --- .../integrations/django/test_cache_module.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index f3f9ed50ce..baaf8551a5 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -509,6 +509,67 @@ def test_cache_spans_item_size(sentry_init, client, capture_events, use_django_c assert second_event["spans"][1]["data"]["cache.item_size"] == 58 +@pytest_mark_django_db_decorator() +def test_cache_spans_get_custom_default( + sentry_init, capture_events, use_django_caching +): + sentry_init( + integrations=[ + DjangoIntegration( + cache_spans=True, + middleware_spans=False, + signals_spans=False, + ) + ], + traces_sample_rate=1.0, + ) + events = capture_events() + + id = os.getpid() + + from django.core.cache import cache + + with sentry_sdk.start_transaction(): + cache.set(f"S{id}", "Sensitive1") + cache.set(f"S{id + 1}", "") + + cache.get(f"S{id}", "null") + cache.get(f"S{id}", default="null") + + cache.get(f"S{id + 1}", "null") + cache.get(f"S{id + 1}", default="null") + + cache.get(f"S{id + 2}", "null") + cache.get(f"S{id + 2}", default="null") + + (transaction,) = events + assert len(transaction["spans"]) == 8 + + assert transaction["spans"][0]["op"] == "cache.put" + assert transaction["spans"][0]["description"] == f"S{id}" + + assert transaction["spans"][1]["op"] == "cache.put" + assert transaction["spans"][1]["description"] == f"S{id + 1}" + + for span in (transaction["spans"][2], transaction["spans"][3]): + assert span["op"] == "cache.get" + assert span["description"] == f"S{id}" + assert span["data"]["cache.hit"] + assert span["data"]["cache.item_size"] == 10 + + for span in (transaction["spans"][4], transaction["spans"][5]): + assert span["op"] == "cache.get" + assert span["description"] == f"S{id + 1}" + assert span["data"]["cache.hit"] + assert span["data"]["cache.item_size"] == 0 + + for span in (transaction["spans"][6], transaction["spans"][7]): + assert span["op"] == "cache.get" + assert span["description"] == f"S{id + 2}" + assert not span["data"]["cache.hit"] + assert "cache.item_size" not in span["data"] + + @pytest.mark.forked @pytest_mark_django_db_decorator() def test_cache_spans_get_many(sentry_init, capture_events, use_django_caching): From 4133b713a2ac10675b5c8e9a249ea681520ce4aa Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 11:05:45 +0100 Subject: [PATCH 05/13] typing --- sentry_sdk/integrations/django/caching.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index 221183f55d..a8dd7cba51 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -26,7 +26,7 @@ def _get_span_description(method_name, args, kwargs): def _set_address_and_port(span, address, port): - # type: (sentry_sdk.Span, Optional[str], Optional[int]) -> None + # type: (sentry_sdk.tracing.Span, Optional[str], Optional[int]) -> None if address is not None: span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, address) From fbfa6b05c96337b20a4ee07bb4bed67dbeb36039 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 11:09:52 +0100 Subject: [PATCH 06/13] add forked --- tests/integrations/django/test_cache_module.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index baaf8551a5..b9c3f84604 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -509,6 +509,7 @@ def test_cache_spans_item_size(sentry_init, client, capture_events, use_django_c assert second_event["spans"][1]["data"]["cache.item_size"] == 58 +@pytest.mark.forked @pytest_mark_django_db_decorator() def test_cache_spans_get_custom_default( sentry_init, capture_events, use_django_caching From 85b5c5503e0c05791b1fa12d6381a98578c26794 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 11:19:14 +0100 Subject: [PATCH 07/13] comment --- sentry_sdk/integrations/django/caching.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index a8dd7cba51..c34284f9f5 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -153,7 +153,7 @@ def _patch_cache(cache, address=None, port=None): if not hasattr(cache, "_sentry_patched"): _patch_cache_method(cache, "set", address, port) _patch_cache_method(cache, "set_many", address, port) - # Seperate patch as you can overwrite value on cache-miss + # Separate patch to account for custom default values on cache misses. _patch_get_cache(cache, address, port) _patch_cache_method(cache, "get_many", address, port) cache._sentry_patched = True From 7805b48ccf18af44687a7aab96167e1bee82b7e2 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 11:34:53 +0100 Subject: [PATCH 08/13] . --- tests/integrations/django/test_cache_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index b9c3f84604..4277d20be7 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -224,7 +224,7 @@ def test_cache_spans_middleware( "views.decorators.cache.cache_header." ) assert second_event["spans"][0]["data"]["cache.hit"] - assert "cache.item_size" in second_event["spans"][0]["data"] + assert second_event["spans"][0]["data"]["cache.item_size"] == 2 # second_event - cache.get 2 assert second_event["spans"][1]["op"] == "cache.get" assert second_event["spans"][1]["description"].startswith( @@ -502,7 +502,7 @@ def test_cache_spans_item_size(sentry_init, client, capture_events, use_django_c assert len(second_event["spans"]) == 2 assert second_event["spans"][0]["op"] == "cache.get" assert second_event["spans"][0]["data"]["cache.hit"] - assert "cache.item_size" in second_event["spans"][0]["data"] + assert second_event["spans"][0]["data"]["cache.item_size"] == 2 assert second_event["spans"][1]["op"] == "cache.get" assert second_event["spans"][1]["data"]["cache.hit"] From dcc112b33814f840c41a5730ab159467641801fe Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 14:03:56 +0100 Subject: [PATCH 09/13] . --- sentry_sdk/integrations/django/caching.py | 2 +- tests/integrations/django/test_cache_module.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index c34284f9f5..abb64a5819 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -119,7 +119,7 @@ def _instrument_call( item_size = None if is_get_operation: - if value is not None: + if value != {}: item_size = len(str(value)) span.set_data(SPANDATA.CACHE_HIT, True) else: diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index 4277d20be7..eb8f5e1acb 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -168,7 +168,6 @@ def test_cache_spans_disabled_templatetag( assert len(second_event["spans"]) == 0 -@pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") def test_cache_spans_middleware( @@ -600,24 +599,30 @@ def test_cache_spans_get_many(sentry_init, capture_events, use_django_caching): assert transaction["spans"][0]["op"] == "cache.get" assert transaction["spans"][0]["description"] == f"S{id}, S{id + 1}" + assert not transaction["spans"][0]["data"]["cache.hit"] assert transaction["spans"][1]["op"] == "cache.get" assert transaction["spans"][1]["description"] == f"S{id}" + assert not transaction["spans"][1]["data"]["cache.hit"] assert transaction["spans"][2]["op"] == "cache.get" assert transaction["spans"][2]["description"] == f"S{id + 1}" + assert not transaction["spans"][2]["data"]["cache.hit"] assert transaction["spans"][3]["op"] == "cache.put" assert transaction["spans"][3]["description"] == f"S{id}" assert transaction["spans"][4]["op"] == "cache.get" assert transaction["spans"][4]["description"] == f"S{id}, S{id + 1}" + assert transaction["spans"][4]["data"]["cache.hit"] assert transaction["spans"][5]["op"] == "cache.get" assert transaction["spans"][5]["description"] == f"S{id}" + assert not transaction["spans"][1]["data"]["cache.hit"] assert transaction["spans"][6]["op"] == "cache.get" assert transaction["spans"][6]["description"] == f"S{id + 1}" + assert not transaction["spans"][1]["data"]["cache.hit"] @pytest.mark.forked From 6eb25ebb60aa61c13b5bc1a4a9cb0b69be5db90d Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 14:20:23 +0100 Subject: [PATCH 10/13] . --- tests/integrations/django/test_cache_module.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index eb8f5e1acb..7778bf9c13 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -168,6 +168,7 @@ def test_cache_spans_disabled_templatetag( assert len(second_event["spans"]) == 0 +@pytest.mark.forked @pytest_mark_django_db_decorator() @pytest.mark.skipif(DJANGO_VERSION < (1, 9), reason="Requires Django >= 1.9") def test_cache_spans_middleware( From 391902bd2c9d3ba4bc37853ecca175520d4d812d Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 14:35:01 +0100 Subject: [PATCH 11/13] test --- tests/integrations/django/test_cache_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index 7778bf9c13..01b97c1302 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -619,11 +619,11 @@ def test_cache_spans_get_many(sentry_init, capture_events, use_django_caching): assert transaction["spans"][5]["op"] == "cache.get" assert transaction["spans"][5]["description"] == f"S{id}" - assert not transaction["spans"][1]["data"]["cache.hit"] + assert transaction["spans"][5]["data"]["cache.hit"] assert transaction["spans"][6]["op"] == "cache.get" assert transaction["spans"][6]["description"] == f"S{id + 1}" - assert not transaction["spans"][1]["data"]["cache.hit"] + assert not transaction["spans"][6]["data"]["cache.hit"] @pytest.mark.forked From 3a4aeeab59689db1d39f9daa0f28908e8476f53a Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 15:28:43 +0100 Subject: [PATCH 12/13] do not break out into own function --- sentry_sdk/integrations/django/caching.py | 84 +++++++---------------- 1 file changed, 25 insertions(+), 59 deletions(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index abb64a5819..1d8a126942 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -20,6 +20,14 @@ from typing import Optional +METHODS_TO_INSTRUMENT = [ + "set", + "set_many", + "get", + "get_many", +] + + def _get_span_description(method_name, args, kwargs): # type: (str, tuple[Any], dict[str, Any]) -> str return _key_as_string(_get_safe_key(method_name, args, kwargs)) @@ -34,58 +42,6 @@ def _set_address_and_port(span, address, port): span.set_data(SPANDATA.NETWORK_PEER_PORT, port) -def _patch_get_cache(cache, address, port): - # type: (CacheHandler, Optional[str], Optional[int]) -> None - from sentry_sdk.integrations.django import DjangoIntegration - - original_method = cache.get - - @ensure_integration_enabled(DjangoIntegration, original_method) - def _instrument_call(cache, original_method, args, kwargs, address, port): - # type: (CacheHandler, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any - op = OP.CACHE_GET - description = _get_span_description("get", args, kwargs) - - default_value = None - if len(args) >= 2: - default_value = args[1] - elif "default" in kwargs: - default_value = kwargs["default"] - - with sentry_sdk.start_span( - op=op, - name=description, - origin=DjangoIntegration.origin, - ) as span: - value = original_method(*args, **kwargs) - - with capture_internal_exceptions(): - _set_address_and_port(span, address, port) - - key = _get_safe_key("get", args, kwargs) - if key is not None: - span.set_data(SPANDATA.CACHE_KEY, key) - - item_size = None - if value != default_value: - item_size = len(str(value)) - span.set_data(SPANDATA.CACHE_HIT, True) - else: - span.set_data(SPANDATA.CACHE_HIT, False) - - if item_size is not None: - span.set_data(SPANDATA.CACHE_ITEM_SIZE, item_size) - - return value - - @functools.wraps(original_method) - def sentry_method(*args, **kwargs): - # type: (*Any, **Any) -> Any - return _instrument_call(cache, original_method, args, kwargs, address, port) - - setattr(cache, "get", sentry_method) - - def _patch_cache_method(cache, method_name, address, port): # type: (CacheHandler, str, Optional[str], Optional[int]) -> None from sentry_sdk.integrations.django import DjangoIntegration @@ -98,7 +54,8 @@ def _instrument_call( ): # type: (CacheHandler, str, Callable[..., Any], tuple[Any, ...], dict[str, Any], Optional[str], Optional[int]) -> Any is_set_operation = method_name.startswith("set") - is_get_operation = not is_set_operation + is_get_method = method_name == "get" + is_get_many_method = method_name == "get_many" op = OP.CACHE_PUT if is_set_operation else OP.CACHE_GET description = _get_span_description(method_name, args, kwargs) @@ -118,12 +75,24 @@ def _instrument_call( span.set_data(SPANDATA.CACHE_KEY, key) item_size = None - if is_get_operation: + if is_get_many_method: if value != {}: item_size = len(str(value)) span.set_data(SPANDATA.CACHE_HIT, True) else: span.set_data(SPANDATA.CACHE_HIT, False) + elif is_get_method: + default_value = None + if len(args) >= 2: + default_value = args[1] + elif "default" in kwargs: + default_value = kwargs["default"] + + if value != default_value: + item_size = len(str(value)) + span.set_data(SPANDATA.CACHE_HIT, True) + else: + span.set_data(SPANDATA.CACHE_HIT, False) else: # TODO: We don't handle `get_or_set` which we should arg_count = len(args) if arg_count >= 2: @@ -151,11 +120,8 @@ def sentry_method(*args, **kwargs): def _patch_cache(cache, address=None, port=None): # type: (CacheHandler, Optional[str], Optional[int]) -> None if not hasattr(cache, "_sentry_patched"): - _patch_cache_method(cache, "set", address, port) - _patch_cache_method(cache, "set_many", address, port) - # Separate patch to account for custom default values on cache misses. - _patch_get_cache(cache, address, port) - _patch_cache_method(cache, "get_many", address, port) + for method_name in METHODS_TO_INSTRUMENT: + _patch_cache_method(cache, method_name, address, port) cache._sentry_patched = True From 626b529efc67f0234e705368687ffa8a660e806d Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 28 Oct 2025 15:29:48 +0100 Subject: [PATCH 13/13] remove method --- sentry_sdk/integrations/django/caching.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index 1d8a126942..82b602f9b5 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -33,15 +33,6 @@ def _get_span_description(method_name, args, kwargs): return _key_as_string(_get_safe_key(method_name, args, kwargs)) -def _set_address_and_port(span, address, port): - # type: (sentry_sdk.tracing.Span, Optional[str], Optional[int]) -> None - if address is not None: - span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, address) - - if port is not None: - span.set_data(SPANDATA.NETWORK_PEER_PORT, port) - - def _patch_cache_method(cache, method_name, address, port): # type: (CacheHandler, str, Optional[str], Optional[int]) -> None from sentry_sdk.integrations.django import DjangoIntegration @@ -68,7 +59,11 @@ def _instrument_call( value = original_method(*args, **kwargs) with capture_internal_exceptions(): - _set_address_and_port(span, address, port) + if address is not None: + span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, address) + + if port is not None: + span.set_data(SPANDATA.NETWORK_PEER_PORT, port) key = _get_safe_key(method_name, args, kwargs) if key is not None: