1212from sentry_sdk .consts import OP
1313from sentry_sdk .integrations .asyncio import (
1414 AsyncioIntegration ,
15- patch_asyncio ,
1615 enable_asyncio_integration ,
16+ patch_asyncio ,
1717)
1818
1919try :
@@ -63,22 +63,30 @@ def get_sentry_task_factory(mock_get_running_loop):
6363
6464@minimum_python_38
6565@pytest .mark .asyncio (loop_scope = "module" )
66+ @pytest .mark .parametrize ("span_streaming" , [True , False ])
6667async def test_create_task (
6768 sentry_init ,
6869 capture_events ,
70+ capture_items ,
71+ span_streaming ,
6972):
7073 sentry_init (
7174 traces_sample_rate = 1.0 ,
7275 send_default_pii = True ,
7376 integrations = [
7477 AsyncioIntegration (),
7578 ],
79+ _experiments = {
80+ "trace_lifecycle" : "stream" if span_streaming else "static" ,
81+ },
7682 )
7783
78- events = capture_events ()
84+ if span_streaming :
85+ items = capture_items ("span" )
7986
80- with sentry_sdk .start_transaction (name = "test_transaction_for_create_task" ):
81- with sentry_sdk .start_span (op = "root" , name = "not so important" ):
87+ with sentry_sdk .traces .start_span (
88+ name = "not so important" , attributes = {"sentry.op" : "root" }
89+ ):
8290 foo_task = asyncio .create_task (foo ())
8391 bar_task = asyncio .create_task (bar ())
8492
@@ -91,26 +99,61 @@ async def test_create_task(
9199
92100 await asyncio .wait (tasks , return_when = asyncio .FIRST_EXCEPTION )
93101
94- sentry_sdk .flush ()
102+ else :
103+ events = capture_events ()
95104
96- (transaction_event ,) = events
105+ with sentry_sdk .start_transaction (name = "test_transaction_for_create_task" ):
106+ with sentry_sdk .start_span (op = "root" , name = "not so important" ):
107+ foo_task = asyncio .create_task (foo ())
108+ bar_task = asyncio .create_task (bar ())
97109
98- assert transaction_event ["spans" ][0 ]["op" ] == "root"
99- assert transaction_event ["spans" ][0 ]["description" ] == "not so important"
110+ if hasattr (foo_task .get_coro (), "__name__" ):
111+ assert foo_task .get_coro ().__name__ == "foo"
112+ if hasattr (bar_task .get_coro (), "__name__" ):
113+ assert bar_task .get_coro ().__name__ == "bar"
100114
101- assert transaction_event ["spans" ][1 ]["op" ] == OP .FUNCTION
102- assert transaction_event ["spans" ][1 ]["description" ] == "foo"
103- assert (
104- transaction_event ["spans" ][1 ]["parent_span_id" ]
105- == transaction_event ["spans" ][0 ]["span_id" ]
106- )
115+ tasks = [foo_task , bar_task ]
107116
108- assert transaction_event ["spans" ][2 ]["op" ] == OP .FUNCTION
109- assert transaction_event ["spans" ][2 ]["description" ] == "bar"
110- assert (
111- transaction_event ["spans" ][2 ]["parent_span_id" ]
112- == transaction_event ["spans" ][0 ]["span_id" ]
113- )
117+ await asyncio .wait (tasks , return_when = asyncio .FIRST_EXCEPTION )
118+
119+ sentry_sdk .flush ()
120+
121+ if span_streaming :
122+ segment = items .pop ().payload
123+
124+ assert segment ["is_segment" ] is True
125+ assert segment ["name" ] == "not so important"
126+ assert segment ["attributes" ]["sentry.op" ] == "root"
127+
128+ spans = [item .payload for item in items ]
129+ assert len (spans ) == 2
130+
131+ assert spans [0 ]["attributes" ]["sentry.op" ] == OP .FUNCTION
132+ assert spans [0 ]["name" ] == "foo"
133+ assert spans [0 ]["parent_span_id" ] == segment ["span_id" ]
134+
135+ assert spans [1 ]["attributes" ]["sentry.op" ] == OP .FUNCTION
136+ assert spans [1 ]["name" ] == "bar"
137+ assert spans [1 ]["parent_span_id" ] == segment ["span_id" ]
138+ else :
139+ (transaction_event ,) = events
140+
141+ assert transaction_event ["spans" ][0 ]["op" ] == "root"
142+ assert transaction_event ["spans" ][0 ]["description" ] == "not so important"
143+
144+ assert transaction_event ["spans" ][1 ]["op" ] == OP .FUNCTION
145+ assert transaction_event ["spans" ][1 ]["description" ] == "foo"
146+ assert (
147+ transaction_event ["spans" ][1 ]["parent_span_id" ]
148+ == transaction_event ["spans" ][0 ]["span_id" ]
149+ )
150+
151+ assert transaction_event ["spans" ][2 ]["op" ] == OP .FUNCTION
152+ assert transaction_event ["spans" ][2 ]["description" ] == "bar"
153+ assert (
154+ transaction_event ["spans" ][2 ]["parent_span_id" ]
155+ == transaction_event ["spans" ][0 ]["span_id" ]
156+ )
114157
115158
116159@minimum_python_38
0 commit comments