|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | import sentry_sdk |
9 | | -from sentry_sdk.traces import SegmentSource |
| 9 | +from sentry_sdk.traces import SegmentSource, SpanStatus |
10 | 10 |
|
11 | 11 | minimum_python_38 = pytest.mark.skipif( |
12 | 12 | sys.version_info < (3, 8), reason="Asyncio tests need Python >= 3.8" |
@@ -562,6 +562,30 @@ def traced_function(): ... |
562 | 562 | assert span["status"] == "ok" |
563 | 563 |
|
564 | 564 |
|
| 565 | +def test_trace_decorator_arguments(sentry_init, capture_envelopes): |
| 566 | + sentry_init( |
| 567 | + traces_sample_rate=1.0, |
| 568 | + _experiments={"trace_lifecycle": "stream"}, |
| 569 | + ) |
| 570 | + |
| 571 | + events = capture_envelopes() |
| 572 | + |
| 573 | + @sentry_sdk.traces.trace(name="traced", attributes={"traced.attribute": 123}) |
| 574 | + def traced_function(): ... |
| 575 | + |
| 576 | + traced_function() |
| 577 | + |
| 578 | + sentry_sdk.get_client().flush() |
| 579 | + spans = envelopes_to_spans(events) |
| 580 | + |
| 581 | + assert len(spans) == 1 |
| 582 | + (span,) = spans |
| 583 | + |
| 584 | + assert span["name"] == "traced" |
| 585 | + assert span["attributes"]["traced.attribute"] == 123 |
| 586 | + assert span["status"] == "ok" |
| 587 | + |
| 588 | + |
565 | 589 | @minimum_python_38 |
566 | 590 | def test_trace_decorator_async(sentry_init, capture_envelopes): |
567 | 591 | sentry_init( |
@@ -589,6 +613,76 @@ async def traced_function(): ... |
589 | 613 | assert span["status"] == "ok" |
590 | 614 |
|
591 | 615 |
|
| 616 | +@minimum_python_38 |
| 617 | +def test_trace_decorator_async_arguments(sentry_init, capture_envelopes): |
| 618 | + sentry_init( |
| 619 | + traces_sample_rate=1.0, |
| 620 | + _experiments={"trace_lifecycle": "stream"}, |
| 621 | + ) |
| 622 | + |
| 623 | + events = capture_envelopes() |
| 624 | + |
| 625 | + @sentry_sdk.traces.trace(name="traced", attributes={"traced.attribute": 123}) |
| 626 | + async def traced_function(): ... |
| 627 | + |
| 628 | + asyncio.run(traced_function()) |
| 629 | + |
| 630 | + sentry_sdk.get_client().flush() |
| 631 | + spans = envelopes_to_spans(events) |
| 632 | + |
| 633 | + assert len(spans) == 1 |
| 634 | + (span,) = spans |
| 635 | + |
| 636 | + assert span["name"] == "traced" |
| 637 | + assert span["attributes"]["traced.attribute"] == 123 |
| 638 | + assert span["status"] == "ok" |
| 639 | + |
| 640 | + |
| 641 | +def test_set_span_status(sentry_init, capture_envelopes): |
| 642 | + sentry_init( |
| 643 | + traces_sample_rate=1.0, |
| 644 | + _experiments={"trace_lifecycle": "stream"}, |
| 645 | + ) |
| 646 | + |
| 647 | + events = capture_envelopes() |
| 648 | + |
| 649 | + with sentry_sdk.traces.start_span(name="span") as span: |
| 650 | + span.set_status(SpanStatus.ERROR) |
| 651 | + |
| 652 | + with sentry_sdk.traces.start_span(name="span") as span: |
| 653 | + span.set_status("error") |
| 654 | + |
| 655 | + sentry_sdk.get_client().flush() |
| 656 | + spans = envelopes_to_spans(events) |
| 657 | + |
| 658 | + assert len(spans) == 2 |
| 659 | + (span1, span2) = spans |
| 660 | + |
| 661 | + assert span1["status"] == "error" |
| 662 | + assert span2["status"] == "error" |
| 663 | + |
| 664 | + |
| 665 | +def test_set_span_status_on_error(sentry_init, capture_envelopes): |
| 666 | + sentry_init( |
| 667 | + traces_sample_rate=1.0, |
| 668 | + _experiments={"trace_lifecycle": "stream"}, |
| 669 | + ) |
| 670 | + |
| 671 | + events = capture_envelopes() |
| 672 | + |
| 673 | + with pytest.raises(ValueError): |
| 674 | + with sentry_sdk.traces.start_span(name="span") as span: |
| 675 | + raise ValueError("oh no!") |
| 676 | + |
| 677 | + sentry_sdk.get_client().flush() |
| 678 | + spans = envelopes_to_spans(events) |
| 679 | + |
| 680 | + assert len(spans) == 1 |
| 681 | + (span,) = spans |
| 682 | + |
| 683 | + assert span["status"] == "error" |
| 684 | + |
| 685 | + |
592 | 686 | def test_set_span_op(sentry_init, capture_envelopes): |
593 | 687 | sentry_init( |
594 | 688 | traces_sample_rate=1.0, |
|
0 commit comments