Skip to content

Commit 19f8e77

Browse files
authored
Prepare asgi tests to support newer asgiref version (#2778)
1 parent bc4d2c5 commit 19f8e77

File tree

2 files changed

+195
-163
lines changed

2 files changed

+195
-163
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_custom_headers.py

+58-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
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+
115
import os
216

317
import opentelemetry.instrumentation.asgi as otel_asgi
4-
from opentelemetry.test.asgitestutil import AsgiTestBase
18+
from opentelemetry.test.asgitestutil import AsyncAsgiTestBase
519
from opentelemetry.test.test_base import TestBase
620
from opentelemetry.trace import SpanKind
721
from opentelemetry.util.http import (
@@ -90,7 +104,7 @@ async def websocket_app_with_custom_headers(scope, receive, send):
90104
break
91105

92106

93-
class TestCustomHeaders(AsgiTestBase, TestBase):
107+
class TestCustomHeaders(AsyncAsgiTestBase):
94108
constructor_params = {}
95109
__test__ = False
96110

@@ -108,7 +122,7 @@ def setUp(self):
108122
**self.constructor_params,
109123
)
110124

111-
def test_http_custom_request_headers_in_span_attributes(self):
125+
async def test_http_custom_request_headers_in_span_attributes(self):
112126
self.scope["headers"].extend(
113127
[
114128
(b"custom-test-header-1", b"test-header-value-1"),
@@ -119,8 +133,8 @@ def test_http_custom_request_headers_in_span_attributes(self):
119133
]
120134
)
121135
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()
124138
span_list = self.exporter.get_finished_spans()
125139
expected = {
126140
"http.request.header.custom_test_header_1": (
@@ -139,16 +153,16 @@ def test_http_custom_request_headers_in_span_attributes(self):
139153
if span.kind == SpanKind.SERVER:
140154
self.assertSpanHasAttributes(span, expected)
141155

142-
def test_http_repeat_request_headers_in_span_attributes(self):
156+
async def test_http_repeat_request_headers_in_span_attributes(self):
143157
self.scope["headers"].extend(
144158
[
145159
(b"custom-test-header-1", b"test-header-value-1"),
146160
(b"custom-test-header-1", b"test-header-value-2"),
147161
]
148162
)
149163
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()
152166
span_list = self.exporter.get_finished_spans()
153167
expected = {
154168
"http.request.header.custom_test_header_1": (
@@ -159,15 +173,15 @@ def test_http_repeat_request_headers_in_span_attributes(self):
159173
span = next(span for span in span_list if span.kind == SpanKind.SERVER)
160174
self.assertSpanHasAttributes(span, expected)
161175

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):
163177
self.scope["headers"].extend(
164178
[
165179
(b"custom-test-header-1", b"test-header-value-1"),
166180
]
167181
)
168182
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()
171185
span_list = self.exporter.get_finished_spans()
172186
expected = {
173187
"http.request.header.custom_test_header_1": (
@@ -185,15 +199,15 @@ def test_http_custom_request_headers_not_in_span_attributes(self):
185199
for key, _ in not_expected.items():
186200
self.assertNotIn(key, span.attributes)
187201

188-
def test_http_custom_response_headers_in_span_attributes(self):
202+
async def test_http_custom_response_headers_in_span_attributes(self):
189203
self.app = otel_asgi.OpenTelemetryMiddleware(
190204
http_app_with_custom_headers,
191205
tracer_provider=self.tracer_provider,
192206
**self.constructor_params,
193207
)
194208
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()
197211
span_list = self.exporter.get_finished_spans()
198212
expected = {
199213
"http.response.header.custom_test_header_1": (
@@ -214,15 +228,15 @@ def test_http_custom_response_headers_in_span_attributes(self):
214228
if span.kind == SpanKind.SERVER:
215229
self.assertSpanHasAttributes(span, expected)
216230

217-
def test_http_repeat_response_headers_in_span_attributes(self):
231+
async def test_http_repeat_response_headers_in_span_attributes(self):
218232
self.app = otel_asgi.OpenTelemetryMiddleware(
219233
http_app_with_repeat_headers,
220234
tracer_provider=self.tracer_provider,
221235
**self.constructor_params,
222236
)
223237
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()
226240
span_list = self.exporter.get_finished_spans()
227241
expected = {
228242
"http.response.header.custom_test_header_1": (
@@ -233,15 +247,15 @@ def test_http_repeat_response_headers_in_span_attributes(self):
233247
span = next(span for span in span_list if span.kind == SpanKind.SERVER)
234248
self.assertSpanHasAttributes(span, expected)
235249

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):
237251
self.app = otel_asgi.OpenTelemetryMiddleware(
238252
http_app_with_custom_headers,
239253
tracer_provider=self.tracer_provider,
240254
**self.constructor_params,
241255
)
242256
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()
245259
span_list = self.exporter.get_finished_spans()
246260
not_expected = {
247261
"http.response.header.custom_test_header_3": (
@@ -253,7 +267,7 @@ def test_http_custom_response_headers_not_in_span_attributes(self):
253267
for key, _ in not_expected.items():
254268
self.assertNotIn(key, span.attributes)
255269

256-
def test_websocket_custom_request_headers_in_span_attributes(self):
270+
async def test_websocket_custom_request_headers_in_span_attributes(self):
257271
self.scope = {
258272
"type": "websocket",
259273
"http_version": "1.1",
@@ -271,11 +285,11 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
271285
"server": ("127.0.0.1", 80),
272286
}
273287
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"})
277291

278-
self.get_all_output()
292+
await self.get_all_output()
279293
span_list = self.exporter.get_finished_spans()
280294
expected = {
281295
"http.request.header.custom_test_header_1": (
@@ -294,7 +308,9 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
294308
if span.kind == SpanKind.SERVER:
295309
self.assertSpanHasAttributes(span, expected)
296310

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+
):
298314
self.scope = {
299315
"type": "websocket",
300316
"http_version": "1.1",
@@ -309,11 +325,11 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
309325
"server": ("127.0.0.1", 80),
310326
}
311327
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"})
315331

316-
self.get_all_output()
332+
await self.get_all_output()
317333
span_list = self.exporter.get_finished_spans()
318334
not_expected = {
319335
"http.request.header.custom_test_header_3": (
@@ -325,7 +341,7 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
325341
for key, _ in not_expected.items():
326342
self.assertNotIn(key, span.attributes)
327343

328-
def test_websocket_custom_response_headers_in_span_attributes(self):
344+
async def test_websocket_custom_response_headers_in_span_attributes(self):
329345
self.scope = {
330346
"type": "websocket",
331347
"http_version": "1.1",
@@ -342,10 +358,10 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
342358
**self.constructor_params,
343359
)
344360
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()
349365
span_list = self.exporter.get_finished_spans()
350366
expected = {
351367
"http.response.header.custom_test_header_1": (
@@ -366,7 +382,9 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
366382
if span.kind == SpanKind.SERVER:
367383
self.assertSpanHasAttributes(span, expected)
368384

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+
):
370388
self.scope = {
371389
"type": "websocket",
372390
"http_version": "1.1",
@@ -383,10 +401,10 @@ def test_websocket_custom_response_headers_not_in_span_attributes(self):
383401
**self.constructor_params,
384402
)
385403
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()
390408
span_list = self.exporter.get_finished_spans()
391409
not_expected = {
392410
"http.response.header.custom_test_header_3": (

0 commit comments

Comments
 (0)