@@ -156,6 +156,33 @@ async def app(scope, receive, send):
156156 return app
157157
158158
159+ @pytest .fixture
160+ def asgi3_app_with_span ():
161+ async def app (scope , receive , send ):
162+ if scope ["type" ] == "lifespan" :
163+ while True :
164+ message = await receive ()
165+ if message ["type" ] == "lifespan.startup" :
166+ await send ({"type" : "lifespan.startup.complete" })
167+ elif message ["type" ] == "lifespan.shutdown" :
168+ await send ({"type" : "lifespan.shutdown.complete" })
169+ return
170+
171+ with sentry_sdk .traces .start_span (name = "child-span" ):
172+ pass
173+
174+ await send (
175+ {
176+ "type" : "http.response.start" ,
177+ "status" : 200 ,
178+ "headers" : [[b"content-type" , b"text/plain" ]],
179+ }
180+ )
181+ await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
182+
183+ return app
184+
185+
159186def test_invalid_transaction_style (asgi3_app ):
160187 with pytest .raises (ValueError ) as exp :
161188 SentryAsgiMiddleware (asgi3_app , transaction_style = "URL" )
@@ -1181,39 +1208,18 @@ async def test_user_ip_address_on_all_spans(
11811208 capture_items ,
11821209 init_kwargs ,
11831210 expect_ip ,
1211+ asgi3_app_with_span ,
11841212):
1185- async def app (scope , receive , send ):
1186- if scope ["type" ] == "lifespan" :
1187- while True :
1188- message = await receive ()
1189- if message ["type" ] == "lifespan.startup" :
1190- await send ({"type" : "lifespan.startup.complete" })
1191- elif message ["type" ] == "lifespan.shutdown" :
1192- await send ({"type" : "lifespan.shutdown.complete" })
1193- return
1194-
1195- with sentry_sdk .start_span (name = "child-span" ):
1196- pass
1197-
1198- await send (
1199- {
1200- "type" : "http.response.start" ,
1201- "status" : 200 ,
1202- "headers" : [[b"content-type" , b"text/plain" ]],
1203- }
1204- )
1205- await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
1206-
1207- kwargs = dict (init_kwargs )
12081213 sentry_init (
12091214 traces_sample_rate = 1.0 ,
1210- ** kwargs ,
1215+ ** init_kwargs ,
12111216 )
1212- sentry_app = SentryAsgiMiddleware (app )
1217+
1218+ app = SentryAsgiMiddleware (asgi3_app_with_span )
12131219
12141220 async def wrapped_app (scope , receive , send ):
12151221 scope ["client" ] = ("127.0.0.1" , 0 )
1216- await sentry_app (scope , receive , send )
1222+ await app (scope , receive , send )
12171223
12181224 async with TestClient (wrapped_app ) as client :
12191225 items = capture_items ("span" )
@@ -1256,32 +1262,13 @@ async def test_is_localhost_attribute(
12561262 server ,
12571263 host_header ,
12581264 is_localhost ,
1265+ asgi3_app_with_span ,
12591266):
1260- async def app (scope , receive , send ):
1261- if scope ["type" ] == "lifespan" :
1262- while True :
1263- message = await receive ()
1264- if message ["type" ] == "lifespan.startup" :
1265- await send ({"type" : "lifespan.startup.complete" })
1266- elif message ["type" ] == "lifespan.shutdown" :
1267- await send ({"type" : "lifespan.shutdown.complete" })
1268- return
1269-
1270- with sentry_sdk .traces .start_span (name = "child-span" ):
1271- pass
1272-
1273- await send (
1274- {
1275- "type" : "http.response.start" ,
1276- "status" : 200 ,
1277- "headers" : [[b"content-type" , b"text/plain" ]],
1278- }
1279- )
1280- await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
1281-
1282- sentry_init (traces_sample_rate = 1.0 )
1267+ sentry_init (
1268+ traces_sample_rate = 1.0 ,
1269+ )
12831270
1284- sentry_app = SentryAsgiMiddleware (app )
1271+ app = SentryAsgiMiddleware (asgi3_app_with_span )
12851272
12861273 async def wrapped_app (scope , receive , send ):
12871274 if scope ["type" ] != "lifespan" :
@@ -1291,7 +1278,7 @@ async def wrapped_app(scope, receive, send):
12911278 if host_header is not None :
12921279 scope ["headers" ].append ((b"host" , host_header ))
12931280
1294- await sentry_app (scope , receive , send )
1281+ await app (scope , receive , send )
12951282
12961283 async with TestClient (wrapped_app ) as client :
12971284 items = capture_items ("span" )
@@ -1303,3 +1290,27 @@ async def wrapped_app(scope, receive, send):
13031290
13041291 assert server_span ["attributes" ]["sentry.is_localhost" ] is is_localhost
13051292 assert child_span ["attributes" ]["sentry.is_localhost" ] is is_localhost
1293+
1294+
1295+ @pytest .mark .asyncio
1296+ async def test_user_agent_original_attribute (
1297+ sentry_init ,
1298+ capture_items ,
1299+ asgi3_app_with_span ,
1300+ ):
1301+ sentry_init (
1302+ traces_sample_rate = 1.0 ,
1303+ )
1304+
1305+ app = SentryAsgiMiddleware (asgi3_app_with_span )
1306+
1307+ async with TestClient (app ) as client :
1308+ items = capture_items ("span" )
1309+ await client .get ("/some_url" , headers = {"User-Agent" : "TestBrowser/1.0" })
1310+
1311+ sentry_sdk .flush ()
1312+
1313+ child_span , server_span = [item .payload for item in items ]
1314+
1315+ assert server_span ["attributes" ]["user_agent.original" ] == "TestBrowser/1.0"
1316+ assert child_span ["attributes" ]["user_agent.original" ] == "TestBrowser/1.0"
0 commit comments