|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from django import VERSION as DJANGO_VERSION |
| 7 | +from django.core.cache import cache |
7 | 8 | from werkzeug.test import Client |
8 | 9 |
|
9 | 10 | try: |
@@ -542,6 +543,56 @@ def test_cache_spans_templatetag( |
542 | 543 | assert second_event["spans"][0]["data"]["cache.item_size"] == 51 |
543 | 544 |
|
544 | 545 |
|
| 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 | + |
545 | 596 | @pytest.mark.parametrize( |
546 | 597 | "method_name, args, kwargs, expected_name", |
547 | 598 | [ |
|
0 commit comments