Skip to content

Commit 7d0705c

Browse files
committed
more tests
1 parent b1e0d3f commit 7d0705c

3 files changed

Lines changed: 100 additions & 3 deletions

File tree

sentry_sdk/_span_batcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _to_transport_format(item: "StreamedSpan") -> "Any":
7373
"trace_id": item.trace_id,
7474
"span_id": item.span_id,
7575
"name": item.get_name(),
76-
"status": item.status.value,
76+
"status": item.status,
7777
"is_segment": item.is_segment(),
7878
"start_timestamp": item.start_timestamp.timestamp(), # TODO[span-first]
7979
}

sentry_sdk/traces.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def set_attributes(self, attributes: "Attributes") -> None:
444444
for key, value in attributes.items():
445445
self.set_attribute(key, value)
446446

447-
def set_status(self, status: SpanStatus) -> None:
447+
def set_status(self, status: "Union[SpanStatus, str]") -> None:
448+
if isinstance(status, Enum):
449+
status = status.value
450+
448451
self.status = status
449452

450453
def get_name(self) -> str:

tests/tracing/test_span_streaming.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
import sentry_sdk
9-
from sentry_sdk.traces import SegmentSource
9+
from sentry_sdk.traces import SegmentSource, SpanStatus
1010

1111
minimum_python_38 = pytest.mark.skipif(
1212
sys.version_info < (3, 8), reason="Asyncio tests need Python >= 3.8"
@@ -562,6 +562,30 @@ def traced_function(): ...
562562
assert span["status"] == "ok"
563563

564564

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+
565589
@minimum_python_38
566590
def test_trace_decorator_async(sentry_init, capture_envelopes):
567591
sentry_init(
@@ -589,6 +613,76 @@ async def traced_function(): ...
589613
assert span["status"] == "ok"
590614

591615

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+
592686
def test_set_span_op(sentry_init, capture_envelopes):
593687
sentry_init(
594688
traces_sample_rate=1.0,

0 commit comments

Comments
 (0)