1
+ # Copyright The OpenTelemetry Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
1
15
import os
2
16
3
17
import opentelemetry .instrumentation .asgi as otel_asgi
4
- from opentelemetry .test .asgitestutil import AsgiTestBase
18
+ from opentelemetry .test .asgitestutil import AsyncAsgiTestBase
5
19
from opentelemetry .test .test_base import TestBase
6
20
from opentelemetry .trace import SpanKind
7
21
from opentelemetry .util .http import (
@@ -90,7 +104,7 @@ async def websocket_app_with_custom_headers(scope, receive, send):
90
104
break
91
105
92
106
93
- class TestCustomHeaders (AsgiTestBase , TestBase ):
107
+ class TestCustomHeaders (AsyncAsgiTestBase ):
94
108
constructor_params = {}
95
109
__test__ = False
96
110
@@ -108,7 +122,7 @@ def setUp(self):
108
122
** self .constructor_params ,
109
123
)
110
124
111
- def test_http_custom_request_headers_in_span_attributes (self ):
125
+ async def test_http_custom_request_headers_in_span_attributes (self ):
112
126
self .scope ["headers" ].extend (
113
127
[
114
128
(b"custom-test-header-1" , b"test-header-value-1" ),
@@ -119,8 +133,8 @@ def test_http_custom_request_headers_in_span_attributes(self):
119
133
]
120
134
)
121
135
self .seed_app (self .app )
122
- self .send_default_request ()
123
- self .get_all_output ()
136
+ await self .send_default_request ()
137
+ await self .get_all_output ()
124
138
span_list = self .exporter .get_finished_spans ()
125
139
expected = {
126
140
"http.request.header.custom_test_header_1" : (
@@ -139,16 +153,16 @@ def test_http_custom_request_headers_in_span_attributes(self):
139
153
if span .kind == SpanKind .SERVER :
140
154
self .assertSpanHasAttributes (span , expected )
141
155
142
- def test_http_repeat_request_headers_in_span_attributes (self ):
156
+ async def test_http_repeat_request_headers_in_span_attributes (self ):
143
157
self .scope ["headers" ].extend (
144
158
[
145
159
(b"custom-test-header-1" , b"test-header-value-1" ),
146
160
(b"custom-test-header-1" , b"test-header-value-2" ),
147
161
]
148
162
)
149
163
self .seed_app (self .app )
150
- self .send_default_request ()
151
- self .get_all_output ()
164
+ await self .send_default_request ()
165
+ await self .get_all_output ()
152
166
span_list = self .exporter .get_finished_spans ()
153
167
expected = {
154
168
"http.request.header.custom_test_header_1" : (
@@ -159,15 +173,15 @@ def test_http_repeat_request_headers_in_span_attributes(self):
159
173
span = next (span for span in span_list if span .kind == SpanKind .SERVER )
160
174
self .assertSpanHasAttributes (span , expected )
161
175
162
- def test_http_custom_request_headers_not_in_span_attributes (self ):
176
+ async def test_http_custom_request_headers_not_in_span_attributes (self ):
163
177
self .scope ["headers" ].extend (
164
178
[
165
179
(b"custom-test-header-1" , b"test-header-value-1" ),
166
180
]
167
181
)
168
182
self .seed_app (self .app )
169
- self .send_default_request ()
170
- self .get_all_output ()
183
+ await self .send_default_request ()
184
+ await self .get_all_output ()
171
185
span_list = self .exporter .get_finished_spans ()
172
186
expected = {
173
187
"http.request.header.custom_test_header_1" : (
@@ -185,15 +199,15 @@ def test_http_custom_request_headers_not_in_span_attributes(self):
185
199
for key , _ in not_expected .items ():
186
200
self .assertNotIn (key , span .attributes )
187
201
188
- def test_http_custom_response_headers_in_span_attributes (self ):
202
+ async def test_http_custom_response_headers_in_span_attributes (self ):
189
203
self .app = otel_asgi .OpenTelemetryMiddleware (
190
204
http_app_with_custom_headers ,
191
205
tracer_provider = self .tracer_provider ,
192
206
** self .constructor_params ,
193
207
)
194
208
self .seed_app (self .app )
195
- self .send_default_request ()
196
- self .get_all_output ()
209
+ await self .send_default_request ()
210
+ await self .get_all_output ()
197
211
span_list = self .exporter .get_finished_spans ()
198
212
expected = {
199
213
"http.response.header.custom_test_header_1" : (
@@ -214,15 +228,15 @@ def test_http_custom_response_headers_in_span_attributes(self):
214
228
if span .kind == SpanKind .SERVER :
215
229
self .assertSpanHasAttributes (span , expected )
216
230
217
- def test_http_repeat_response_headers_in_span_attributes (self ):
231
+ async def test_http_repeat_response_headers_in_span_attributes (self ):
218
232
self .app = otel_asgi .OpenTelemetryMiddleware (
219
233
http_app_with_repeat_headers ,
220
234
tracer_provider = self .tracer_provider ,
221
235
** self .constructor_params ,
222
236
)
223
237
self .seed_app (self .app )
224
- self .send_default_request ()
225
- self .get_all_output ()
238
+ await self .send_default_request ()
239
+ await self .get_all_output ()
226
240
span_list = self .exporter .get_finished_spans ()
227
241
expected = {
228
242
"http.response.header.custom_test_header_1" : (
@@ -233,15 +247,15 @@ def test_http_repeat_response_headers_in_span_attributes(self):
233
247
span = next (span for span in span_list if span .kind == SpanKind .SERVER )
234
248
self .assertSpanHasAttributes (span , expected )
235
249
236
- def test_http_custom_response_headers_not_in_span_attributes (self ):
250
+ async def test_http_custom_response_headers_not_in_span_attributes (self ):
237
251
self .app = otel_asgi .OpenTelemetryMiddleware (
238
252
http_app_with_custom_headers ,
239
253
tracer_provider = self .tracer_provider ,
240
254
** self .constructor_params ,
241
255
)
242
256
self .seed_app (self .app )
243
- self .send_default_request ()
244
- self .get_all_output ()
257
+ await self .send_default_request ()
258
+ await self .get_all_output ()
245
259
span_list = self .exporter .get_finished_spans ()
246
260
not_expected = {
247
261
"http.response.header.custom_test_header_3" : (
@@ -253,7 +267,7 @@ def test_http_custom_response_headers_not_in_span_attributes(self):
253
267
for key , _ in not_expected .items ():
254
268
self .assertNotIn (key , span .attributes )
255
269
256
- def test_websocket_custom_request_headers_in_span_attributes (self ):
270
+ async def test_websocket_custom_request_headers_in_span_attributes (self ):
257
271
self .scope = {
258
272
"type" : "websocket" ,
259
273
"http_version" : "1.1" ,
@@ -271,11 +285,11 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
271
285
"server" : ("127.0.0.1" , 80 ),
272
286
}
273
287
self .seed_app (self .app )
274
- self .send_input ({"type" : "websocket.connect" })
275
- self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
276
- self .send_input ({"type" : "websocket.disconnect" })
288
+ await self .send_input ({"type" : "websocket.connect" })
289
+ await self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
290
+ await self .send_input ({"type" : "websocket.disconnect" })
277
291
278
- self .get_all_output ()
292
+ await self .get_all_output ()
279
293
span_list = self .exporter .get_finished_spans ()
280
294
expected = {
281
295
"http.request.header.custom_test_header_1" : (
@@ -294,7 +308,9 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
294
308
if span .kind == SpanKind .SERVER :
295
309
self .assertSpanHasAttributes (span , expected )
296
310
297
- def test_websocket_custom_request_headers_not_in_span_attributes (self ):
311
+ async def test_websocket_custom_request_headers_not_in_span_attributes (
312
+ self ,
313
+ ):
298
314
self .scope = {
299
315
"type" : "websocket" ,
300
316
"http_version" : "1.1" ,
@@ -309,11 +325,11 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
309
325
"server" : ("127.0.0.1" , 80 ),
310
326
}
311
327
self .seed_app (self .app )
312
- self .send_input ({"type" : "websocket.connect" })
313
- self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
314
- self .send_input ({"type" : "websocket.disconnect" })
328
+ await self .send_input ({"type" : "websocket.connect" })
329
+ await self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
330
+ await self .send_input ({"type" : "websocket.disconnect" })
315
331
316
- self .get_all_output ()
332
+ await self .get_all_output ()
317
333
span_list = self .exporter .get_finished_spans ()
318
334
not_expected = {
319
335
"http.request.header.custom_test_header_3" : (
@@ -325,7 +341,7 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
325
341
for key , _ in not_expected .items ():
326
342
self .assertNotIn (key , span .attributes )
327
343
328
- def test_websocket_custom_response_headers_in_span_attributes (self ):
344
+ async def test_websocket_custom_response_headers_in_span_attributes (self ):
329
345
self .scope = {
330
346
"type" : "websocket" ,
331
347
"http_version" : "1.1" ,
@@ -342,10 +358,10 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
342
358
** self .constructor_params ,
343
359
)
344
360
self .seed_app (self .app )
345
- self .send_input ({"type" : "websocket.connect" })
346
- self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
347
- self .send_input ({"type" : "websocket.disconnect" })
348
- self .get_all_output ()
361
+ await self .send_input ({"type" : "websocket.connect" })
362
+ await self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
363
+ await self .send_input ({"type" : "websocket.disconnect" })
364
+ await self .get_all_output ()
349
365
span_list = self .exporter .get_finished_spans ()
350
366
expected = {
351
367
"http.response.header.custom_test_header_1" : (
@@ -366,7 +382,9 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
366
382
if span .kind == SpanKind .SERVER :
367
383
self .assertSpanHasAttributes (span , expected )
368
384
369
- def test_websocket_custom_response_headers_not_in_span_attributes (self ):
385
+ async def test_websocket_custom_response_headers_not_in_span_attributes (
386
+ self ,
387
+ ):
370
388
self .scope = {
371
389
"type" : "websocket" ,
372
390
"http_version" : "1.1" ,
@@ -383,10 +401,10 @@ def test_websocket_custom_response_headers_not_in_span_attributes(self):
383
401
** self .constructor_params ,
384
402
)
385
403
self .seed_app (self .app )
386
- self .send_input ({"type" : "websocket.connect" })
387
- self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
388
- self .send_input ({"type" : "websocket.disconnect" })
389
- self .get_all_output ()
404
+ await self .send_input ({"type" : "websocket.connect" })
405
+ await self .send_input ({"type" : "websocket.receive" , "text" : "ping" })
406
+ await self .send_input ({"type" : "websocket.disconnect" })
407
+ await self .get_all_output ()
390
408
span_list = self .exporter .get_finished_spans ()
391
409
not_expected = {
392
410
"http.response.header.custom_test_header_3" : (
0 commit comments