2020import opentelemetry .ext .asgi as otel_asgi
2121from opentelemetry import trace as trace_api
2222from opentelemetry .ext .testutil .asgitestutil import (
23- AsgiTestBase , setup_testing_defaults
23+ AsgiTestBase ,
24+ setup_testing_defaults ,
2425)
2526
2627
2728async def simple_asgi (scope , receive , send ):
2829 assert isinstance (scope , dict )
29- assert scope .get (' type' ) == "http"
30+ assert scope .get (" type" ) == "http"
3031 payload = await receive ()
31- if payload .get ('type' ) == "http.request" :
32- await send ({
33- 'type' : 'http.response.start' ,
34- 'status' : 200 ,
35- 'headers' : [
36- [b'Content-Type' , b'text/plain' ],
37- ],
38- })
39- await send ({
40- 'type' : 'http.response.body' ,
41- 'body' : b"*"
42- })
32+ if payload .get ("type" ) == "http.request" :
33+ await send (
34+ {
35+ "type" : "http.response.start" ,
36+ "status" : 200 ,
37+ "headers" : [[b"Content-Type" , b"text/plain" ]],
38+ }
39+ )
40+ await send ({"type" : "http.response.body" , "body" : b"*" })
4341
4442
4543async def error_asgi (scope , receive , send ):
4644 assert isinstance (scope , dict )
47- assert scope .get (' type' ) == "http"
45+ assert scope .get (" type" ) == "http"
4846 payload = await receive ()
49- if payload .get (' type' ) == "http.request" :
47+ if payload .get (" type" ) == "http.request" :
5048 try :
5149 raise ValueError
5250 except ValueError :
53- scope ['hack_exc_info' ] = sys .exc_info ()
54- await send ({
55- 'type' : 'http.response.start' ,
56- 'status' : 200 ,
57- 'headers' : [
58- [b'Content-Type' , b'text/plain' ],
59- ],
60- })
61- await send ({
62- 'type' : 'http.response.body' ,
63- 'body' : b"*"
64- })
51+ scope ["hack_exc_info" ] = sys .exc_info ()
52+ await send (
53+ {
54+ "type" : "http.response.start" ,
55+ "status" : 200 ,
56+ "headers" : [[b"Content-Type" , b"text/plain" ]],
57+ }
58+ )
59+ await send ({"type" : "http.response.body" , "body" : b"*" })
6560
6661
6762class TestAsgiApplication (AsgiTestBase ):
@@ -70,17 +65,19 @@ def validate_outputs(self, outputs, error=None):
7065 self .assertEqual (len (outputs ), 2 )
7166 response_start = outputs [0 ]
7267 response_body = outputs [1 ]
73- self .assertEqual (response_start [' type' ], ' http.response.start' )
74- self .assertEqual (response_body [' type' ], ' http.response.body' )
68+ self .assertEqual (response_start [" type" ], " http.response.start" )
69+ self .assertEqual (response_body [" type" ], " http.response.body" )
7570
7671 # Check http response body
77- self .assertEqual (response_body [' body' ], b"*" )
72+ self .assertEqual (response_body [" body" ], b"*" )
7873
7974 # Check http response start
80- self .assertEqual (response_start ['status' ], 200 )
81- self .assertEqual (response_start ['headers' ], [[b'Content-Type' , b'text/plain' ]])
75+ self .assertEqual (response_start ["status" ], 200 )
76+ self .assertEqual (
77+ response_start ["headers" ], [[b"Content-Type" , b"text/plain" ]]
78+ )
8279
83- exc_info = self .scope .get (' hack_exc_info' )
80+ exc_info = self .scope .get (" hack_exc_info" )
8481 if error :
8582 self .assertIs (exc_info [0 ], error )
8683 self .assertIsInstance (exc_info [1 ], error )
@@ -93,32 +90,27 @@ def validate_outputs(self, outputs, error=None):
9390 self .assertEqual (len (span_list ), 4 )
9491 expected = [
9592 {
96- 'name' : "/ (http.request)" ,
97- 'kind' : trace_api .SpanKind .INTERNAL ,
98- 'attributes' : {
99- "type" : "http.request" ,
100- },
93+ "name" : "/ (http.request)" ,
94+ "kind" : trace_api .SpanKind .INTERNAL ,
95+ "attributes" : {"type" : "http.request" },
10196 },
102-
10397 {
104- ' name' : "/ (http.response.start)" ,
105- ' kind' : trace_api .SpanKind .INTERNAL ,
106- ' attributes' : {
98+ " name" : "/ (http.response.start)" ,
99+ " kind" : trace_api .SpanKind .INTERNAL ,
100+ " attributes" : {
107101 "http.status_code" : 200 ,
108102 "type" : "http.response.start" ,
109103 },
110104 },
111105 {
112- 'name' : "/ (http.response.body)" ,
113- 'kind' : trace_api .SpanKind .INTERNAL ,
114- 'attributes' : {
115- "type" : "http.response.body" ,
116- },
106+ "name" : "/ (http.response.body)" ,
107+ "kind" : trace_api .SpanKind .INTERNAL ,
108+ "attributes" : {"type" : "http.response.body" },
117109 },
118110 {
119- ' name' : "/" ,
120- ' kind' : trace_api .SpanKind .SERVER ,
121- ' attributes' : {
111+ " name" : "/" ,
112+ " kind" : trace_api .SpanKind .SERVER ,
113+ " attributes" : {
122114 "component" : "http" ,
123115 "http.method" : "GET" ,
124116 "http.server_name" : "127.0.0.1" ,
@@ -134,9 +126,9 @@ def validate_outputs(self, outputs, error=None):
134126 },
135127 ]
136128 for span , expected in zip (span_list , expected ):
137- self .assertEqual (span .name , expected [' name' ])
138- self .assertEqual (span .kind , expected [' kind' ])
139- self .assertEqual (span .attributes , expected [' attributes' ])
129+ self .assertEqual (span .name , expected [" name" ])
130+ self .assertEqual (span .kind , expected [" kind" ])
131+ self .assertEqual (span .attributes , expected [" attributes" ])
140132
141133 def test_basic_asgi_call (self ):
142134 app = otel_asgi .OpenTelemetryMiddleware (simple_asgi )
@@ -176,15 +168,13 @@ def test_request_attributes(self):
176168 "http.server_name" : "127.0.0.1" ,
177169 "http.flavor" : "1.0" ,
178170 "net.peer.ip" : "127.0.0.1" ,
179- "net.peer.port" : 32767
171+ "net.peer.port" : 32767 ,
180172 },
181173 )
182174
183175 def test_response_attributes (self ):
184176 otel_asgi .set_status_code (self .span , 404 )
185- expected = (
186- mock .call ("http.status_code" , 404 ),
187- )
177+ expected = (mock .call ("http.status_code" , 404 ),)
188178 self .assertEqual (self .span .set_attribute .call_count , 1 )
189179 self .assertEqual (self .span .set_attribute .call_count , 1 )
190180 self .span .set_attribute .assert_has_calls (expected , any_order = True )
0 commit comments