2424)
2525
2626
27- async def simple_asgi (scope , receive , send ):
28- assert isinstance (scope , dict )
29- assert scope .get ("type" ) == "http"
27+ async def http_app (scope , receive , send ):
3028 payload = await receive ()
3129 if payload .get ("type" ) == "http.request" :
3230 await send (
@@ -39,6 +37,28 @@ async def simple_asgi(scope, receive, send):
3937 await send ({"type" : "http.response.body" , "body" : b"*" })
4038
4139
40+ async def websocket_app (scope , receive , send ):
41+ while True :
42+ payload = await receive ()
43+ if payload .get ("type" ) == "websocket.connect" :
44+ await send ({"type" : "websocket.accept" })
45+
46+ if payload .get ("type" ) == "websocket.receive" :
47+ if payload .get ("text" ) == "ping" :
48+ await send ({"type" : "websocket.send" , "text" : "pong" })
49+
50+ if payload .get ("type" ) == "websocket.disconnect" :
51+ break
52+
53+
54+ async def simple_asgi (scope , receive , send ):
55+ assert isinstance (scope , dict )
56+ if scope ["type" ] == "http" :
57+ await http_app (scope , receive , send )
58+ elif scope ["type" ] == "websocket" :
59+ await websocket_app (scope , receive , send )
60+
61+
4262async def error_asgi (scope , receive , send ):
4363 assert isinstance (scope , dict )
4464 assert scope .get ("type" ) == "http"
@@ -91,25 +111,25 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
91111 self .assertEqual (len (span_list ), 4 )
92112 expected = [
93113 {
94- "name" : "HTTP GET ( asgi.http.receive) " ,
114+ "name" : "GET asgi.http.receive" ,
95115 "kind" : trace_api .SpanKind .INTERNAL ,
96116 "attributes" : {"type" : "http.request" },
97117 },
98118 {
99- "name" : "HTTP GET ( asgi.http.send) " ,
119+ "name" : "GET asgi.http.send" ,
100120 "kind" : trace_api .SpanKind .INTERNAL ,
101121 "attributes" : {
102122 "http.status_code" : 200 ,
103123 "type" : "http.response.start" ,
104124 },
105125 },
106126 {
107- "name" : "HTTP GET ( asgi.http.send) " ,
127+ "name" : "GET asgi.http.send" ,
108128 "kind" : trace_api .SpanKind .INTERNAL ,
109129 "attributes" : {"type" : "http.response.body" },
110130 },
111131 {
112- "name" : "HTTP GET ( asgi.connection) " ,
132+ "name" : "GET asgi" ,
113133 "kind" : trace_api .SpanKind .SERVER ,
114134 "attributes" : {
115135 "component" : "http" ,
@@ -228,41 +248,38 @@ def update_expected_user_agent(expected):
228248 self .validate_outputs (outputs , modifiers = [update_expected_user_agent ])
229249
230250 def test_websocket (self ):
231- async def app_asgi (scope , receive , send ):
232- assert scope ["type" ] == "websocket"
233- payload = await receive ()
234- if payload .get ("type" ) == "http.request" :
235- await send (
236- {
237- "type" : "http.response.start" ,
238- "status" : 200 ,
239- "headers" : [[b"Content-Type" , b"text/plain" ]],
240- }
241- )
242- await send ({"type" : "http.response.body" , "body" : b"*" })
243-
244- self .scope ["type" ] = "websocket"
245- app = otel_asgi .OpenTelemetryMiddleware (app_asgi )
251+ self .scope = {
252+ "type" : "websocket" ,
253+ "http_version" : "1.1" ,
254+ "scheme" : "ws" ,
255+ "path" : "/" ,
256+ "query_string" : b"" ,
257+ "headers" : [],
258+ "client" : ("127.0.0.1" , 32767 ),
259+ "server" : ("127.0.0.1" , 80 ),
260+ }
261+ app = otel_asgi .OpenTelemetryMiddleware (simple_asgi )
246262 self .seed_app (app )
247- self .send_default_request ()
263+ self .send_input ({"type" : "websocket.connect" })
264+ self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
265+ self .send_input ({"type" : "websocket.disconnect" })
266+ outputs = self .get_all_output ()
248267 span_list = self .memory_exporter .get_finished_spans ()
249- self .assertEqual (len (span_list ), 4 )
268+ self .assertEqual (len (span_list ), 6 )
250269 expected = [
251- "HTTP GET (asgi.websocket.receive)" ,
252- "HTTP GET (asgi.websocket.send)" ,
253- "HTTP GET (asgi.websocket.send)" ,
270+ "/ asgi.websocket.receive" ,
271+ "/ asgi.websocket.send" ,
272+ "/ asgi.websocket.receive" ,
273+ "/ asgi.websocket.send" ,
274+ "/ asgi.websocket.receive" ,
275+ "/ asgi" ,
254276 ]
255277 actual = [span .name for span in span_list ]
256- self .assertListEqual (actual [: 3 ] , expected )
278+ self .assertListEqual (actual , expected )
257279
258280 def test_lifespan (self ):
259- async def app_asgi (scope , receive , send ):
260- assert scope ["type" ] == "lifespan"
261- await receive ()
262- await send ({})
263-
264281 self .scope ["type" ] = "lifespan"
265- app = otel_asgi .OpenTelemetryMiddleware (app_asgi )
282+ app = otel_asgi .OpenTelemetryMiddleware (simple_asgi )
266283 self .seed_app (app )
267284 self .send_default_request ()
268285 span_list = self .memory_exporter .get_finished_spans ()
0 commit comments