11import pytest
22
3+ import sentry_sdk
34from sentry_sdk import capture_message , start_transaction
45from sentry_sdk .consts import SPANDATA
56from sentry_sdk .integrations .redis import RedisIntegration
@@ -35,6 +36,7 @@ async def test_async_basic(sentry_init, capture_events):
3536 }
3637
3738
39+ @pytest .mark .parametrize ("span_streaming" , [True , False ])
3840@pytest .mark .parametrize (
3941 "is_transaction, send_default_pii, expected_first_ten" ,
4042 [
@@ -44,69 +46,127 @@ async def test_async_basic(sentry_init, capture_events):
4446)
4547@pytest .mark .asyncio
4648async def test_async_redis_pipeline (
47- sentry_init , capture_events , is_transaction , send_default_pii , expected_first_ten
49+ sentry_init ,
50+ capture_events ,
51+ capture_items ,
52+ is_transaction ,
53+ send_default_pii ,
54+ expected_first_ten ,
55+ span_streaming ,
4856):
4957 sentry_init (
5058 integrations = [RedisIntegration ()],
5159 traces_sample_rate = 1.0 ,
5260 send_default_pii = send_default_pii ,
61+ _experiments = {"trace_lifecycle" : "stream" if span_streaming else "static" },
5362 )
54- events = capture_events ()
5563
5664 connection = FakeRedis ()
57- with start_transaction ():
58- pipeline = connection .pipeline (transaction = is_transaction )
59- pipeline .get ("foo" )
60- pipeline .set ("bar" , 1 )
61- pipeline .set ("baz" , 2 )
62- await pipeline .execute ()
6365
64- (event ,) = events
65- (span ,) = event ["spans" ]
66- assert span ["op" ] == "db.redis"
67- assert span ["description" ] == "redis.pipeline.execute"
68- assert span ["data" ] == ApproxDict (
69- {
70- "redis.commands" : {
71- "count" : 3 ,
72- "first_ten" : expected_first_ten ,
73- },
74- SPANDATA .DB_SYSTEM : "redis" ,
75- SPANDATA .DB_NAME : "0" ,
76- SPANDATA .SERVER_ADDRESS : connection .connection_pool .connection_kwargs .get (
77- "host"
78- ),
79- SPANDATA .SERVER_PORT : 6379 ,
66+ if span_streaming :
67+ items = capture_items ("span" )
68+ with sentry_sdk .traces .start_span (name = "custom parent" ):
69+ pipeline = connection .pipeline (transaction = is_transaction )
70+ pipeline .get ("foo" )
71+ pipeline .set ("bar" , 1 )
72+ pipeline .set ("baz" , 2 )
73+ await pipeline .execute ()
74+ sentry_sdk .flush ()
75+
76+ assert len (items ) == 2
77+ pipeline_span , parent_span = items [0 ].payload , items [1 ].payload
78+
79+ assert parent_span ["name" ] == "custom parent"
80+ assert pipeline_span ["name" ] == "redis.pipeline.execute"
81+ attrs = pipeline_span ["attributes" ]
82+ assert attrs ["sentry.op" ] == "db.redis"
83+ assert attrs ["db.system.name" ] == "redis"
84+ assert attrs ["db.namespace" ] == "0"
85+ assert attrs [SPANDATA .SERVER_ADDRESS ] == (
86+ connection .connection_pool .connection_kwargs .get ("host" )
87+ )
88+ assert attrs [SPANDATA .SERVER_PORT ] == 6379
89+ else :
90+ events = capture_events ()
91+ with start_transaction ():
92+ pipeline = connection .pipeline (transaction = is_transaction )
93+ pipeline .get ("foo" )
94+ pipeline .set ("bar" , 1 )
95+ pipeline .set ("baz" , 2 )
96+ await pipeline .execute ()
97+
98+ (event ,) = events
99+ (span ,) = event ["spans" ]
100+ assert span ["op" ] == "db.redis"
101+ assert span ["description" ] == "redis.pipeline.execute"
102+ assert span ["data" ] == ApproxDict (
103+ {
104+ "redis.commands" : {
105+ "count" : 3 ,
106+ "first_ten" : expected_first_ten ,
107+ },
108+ SPANDATA .DB_SYSTEM : "redis" ,
109+ SPANDATA .DB_NAME : "0" ,
110+ SPANDATA .SERVER_ADDRESS : connection .connection_pool .connection_kwargs .get (
111+ "host"
112+ ),
113+ SPANDATA .SERVER_PORT : 6379 ,
114+ }
115+ )
116+ assert span ["tags" ] == {
117+ "redis.transaction" : is_transaction ,
118+ "redis.is_cluster" : False ,
80119 }
81- )
82- assert span ["tags" ] == {
83- "redis.transaction" : is_transaction ,
84- "redis.is_cluster" : False ,
85- }
86120
87121
122+ @pytest .mark .parametrize ("span_streaming" , [True , False ])
88123@pytest .mark .asyncio
89- async def test_async_span_origin (sentry_init , capture_events ):
124+ async def test_async_span_origin (
125+ sentry_init , capture_events , capture_items , span_streaming
126+ ):
90127 sentry_init (
91128 integrations = [RedisIntegration ()],
92129 traces_sample_rate = 1.0 ,
130+ _experiments = {"trace_lifecycle" : "stream" if span_streaming else "static" },
93131 )
94- events = capture_events ()
95132
96133 connection = FakeRedis ()
97- with start_transaction (name = "custom_transaction" ):
98- # default case
99- await connection .set ("somekey" , "somevalue" )
100-
101- # pipeline
102- pipeline = connection .pipeline (transaction = False )
103- pipeline .get ("somekey" )
104- pipeline .set ("anotherkey" , 1 )
105- await pipeline .execute ()
106-
107- (event ,) = events
108-
109- assert event ["contexts" ]["trace" ]["origin" ] == "manual"
110134
111- for span in event ["spans" ]:
112- assert span ["origin" ] == "auto.db.redis"
135+ if span_streaming :
136+ items = capture_items ("span" )
137+ with sentry_sdk .traces .start_span (name = "custom parent" ):
138+ # default case
139+ await connection .set ("somekey" , "somevalue" )
140+
141+ # pipeline
142+ pipeline = connection .pipeline (transaction = False )
143+ pipeline .get ("somekey" )
144+ pipeline .set ("anotherkey" , 1 )
145+ await pipeline .execute ()
146+ sentry_sdk .flush ()
147+
148+ assert len (items ) == 3
149+ set_span , pipeline_span , parent_span = [item .payload for item in items ]
150+
151+ assert parent_span ["name" ] == "custom parent"
152+ assert parent_span ["attributes" ]["sentry.origin" ] == "manual"
153+ assert set_span ["attributes" ]["sentry.origin" ] == "auto.db.redis"
154+ assert pipeline_span ["attributes" ]["sentry.origin" ] == "auto.db.redis"
155+ else :
156+ events = capture_events ()
157+ with start_transaction (name = "custom_transaction" ):
158+ # default case
159+ await connection .set ("somekey" , "somevalue" )
160+
161+ # pipeline
162+ pipeline = connection .pipeline (transaction = False )
163+ pipeline .get ("somekey" )
164+ pipeline .set ("anotherkey" , 1 )
165+ await pipeline .execute ()
166+
167+ (event ,) = events
168+
169+ assert event ["contexts" ]["trace" ]["origin" ] == "manual"
170+
171+ for span in event ["spans" ]:
172+ assert span ["origin" ] == "auto.db.redis"
0 commit comments