Skip to content

Commit 01bfc73

Browse files
committed
fix(django): trace cache.add operations
1 parent c686455 commit 01bfc73

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

sentry_sdk/integrations/django/caching.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
METHODS_TO_INSTRUMENT = [
22+
"add",
2223
"set",
2324
"set_many",
2425
"get",
@@ -52,7 +53,7 @@ def _instrument_call(
5253
address: "Optional[str]",
5354
port: "Optional[int]",
5455
) -> "Any":
55-
is_set_operation = method_name.startswith("set")
56+
is_set_operation = method_name.startswith("set") or method_name == "add"
5657
is_get_method = method_name == "get"
5758
is_get_many_method = method_name == "get_many"
5859

tests/integrations/django/test_cache_module.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from django import VERSION as DJANGO_VERSION
7+
from django.core.cache import cache
78
from werkzeug.test import Client
89

910
try:
@@ -542,6 +543,56 @@ def test_cache_spans_templatetag(
542543
assert second_event["spans"][0]["data"]["cache.item_size"] == 51
543544

544545

546+
@pytest.mark.forked
547+
@pytest.mark.parametrize("span_streaming", [True, False])
548+
def test_cache_spans_add(
549+
sentry_init,
550+
capture_events,
551+
capture_items,
552+
use_django_caching,
553+
span_streaming,
554+
):
555+
sentry_init(
556+
integrations=[
557+
DjangoIntegration(
558+
cache_spans=True,
559+
middleware_spans=False,
560+
signals_spans=False,
561+
)
562+
],
563+
traces_sample_rate=1.0,
564+
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
565+
)
566+
567+
if span_streaming:
568+
items = capture_items("span")
569+
570+
with sentry_sdk.start_transaction(name="cache-add", op="test"):
571+
assert cache.add("cache-add-key", "value")
572+
573+
sentry_sdk.flush()
574+
spans = [item.payload for item in items if item.type == "span"]
575+
assert len(spans) == 1
576+
assert spans[0]["attributes"]["sentry.op"] == "cache.put"
577+
assert spans[0]["name"] == "cache-add-key"
578+
assert spans[0]["attributes"]["cache.key"] == ["cache-add-key"]
579+
assert spans[0]["attributes"]["cache.item_size"] == 5
580+
else:
581+
events = capture_events()
582+
583+
with sentry_sdk.start_transaction(name="cache-add", op="test"):
584+
assert cache.add("cache-add-key", "value")
585+
586+
sentry_sdk.flush()
587+
assert len(events) == 1
588+
spans = events[0]["spans"]
589+
assert len(spans) == 1
590+
assert spans[0]["op"] == "cache.put"
591+
assert spans[0]["description"] == "cache-add-key"
592+
assert spans[0]["data"]["cache.key"] == ["cache-add-key"]
593+
assert spans[0]["data"]["cache.item_size"] == 5
594+
595+
545596
@pytest.mark.parametrize(
546597
"method_name, args, kwargs, expected_name",
547598
[

0 commit comments

Comments
 (0)