Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/django/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


METHODS_TO_INSTRUMENT = [
"add",
"set",
"set_many",
"get",
Expand Down Expand Up @@ -52,7 +53,7 @@ def _instrument_call(
address: "Optional[str]",
port: "Optional[int]",
) -> "Any":
is_set_operation = method_name.startswith("set")
is_set_operation = method_name.startswith("set") or method_name == "add"
is_get_method = method_name == "get"
is_get_many_method = method_name == "get_many"

Expand Down
51 changes: 51 additions & 0 deletions tests/integrations/django/test_cache_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from django import VERSION as DJANGO_VERSION
from django.core.cache import cache
from werkzeug.test import Client

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


@pytest.mark.forked
@pytest.mark.parametrize("span_streaming", [True, False])
def test_cache_spans_add(
sentry_init,
capture_events,
capture_items,
use_django_caching,
span_streaming,
):
sentry_init(
integrations=[
DjangoIntegration(
cache_spans=True,
middleware_spans=False,
signals_spans=False,
)
],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)

if span_streaming:
items = capture_items("span")

with sentry_sdk.start_transaction(name="cache-add", op="test"):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the context of span streaming, we no longer have the concept of a transaction, we instead have a segment.

Because of that, we shouldn't be invoking this method, but if we need to create a span to attach the generated cache span to, use with sentry_sdk.traces.start_span(...) instead.

The assertions from lines 575 to 579 will likely need to be updated as a result, as there will be another span caught by the capture_items line above, so the index value used will change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just adapt the test to follow the existing pattern in the same file @immanuwell

assert cache.add("cache-add-key", "value")

sentry_sdk.flush()
spans = [item.payload for item in items if item.type == "span"]
assert len(spans) == 1
assert spans[0]["attributes"]["sentry.op"] == "cache.put"
assert spans[0]["name"] == "cache-add-key"
assert spans[0]["attributes"]["cache.key"] == ["cache-add-key"]
assert spans[0]["attributes"]["cache.item_size"] == 5
else:
events = capture_events()

with sentry_sdk.start_transaction(name="cache-add", op="test"):
assert cache.add("cache-add-key", "value")

sentry_sdk.flush()
assert len(events) == 1
spans = events[0]["spans"]
assert len(spans) == 1
assert spans[0]["op"] == "cache.put"
assert spans[0]["description"] == "cache-add-key"
assert spans[0]["data"]["cache.key"] == ["cache-add-key"]
assert spans[0]["data"]["cache.item_size"] == 5


@pytest.mark.parametrize(
"method_name, args, kwargs, expected_name",
[
Expand Down
Loading