Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asgi tests with asgiref 3.8.1 #2818

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asgiref==3.7.2
asgiref==3.8.1
Deprecated==1.2.14
importlib-metadata==6.11.0
iniconfig==2.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import asyncio
from unittest import IsolatedAsyncioTestCase

from asgiref.testing import ApplicationCommunicator

from opentelemetry.test.test_base import TestBase


def setup_testing_defaults(scope):
scope.update(
{
"client": ("127.0.0.1", 32767),
"headers": [],
"http_version": "1.0",
"method": "GET",
"path": "/",
"query_string": b"",
"scheme": "http",
"server": ("127.0.0.1", 80),
"type": "http",
}
)


class AsyncAsgiTestBase(TestBase, IsolatedAsyncioTestCase):
def setUp(self):
super().setUp()

self.scope = {}
setup_testing_defaults(self.scope)
self.communicator = None

def tearDown(self):
if self.communicator:
asyncio.get_event_loop().run_until_complete(
self.communicator.wait()
)

def seed_app(self, app):
self.communicator = ApplicationCommunicator(app, self.scope)

async def send_input(self, message):
await self.communicator.send_input(message)

async def send_default_request(self):
await self.send_input({"type": "http.request", "body": b""})

async def get_output(self, timeout=1):
return await self.communicator.receive_output(timeout)

async def get_all_output(self, timeout=1):
outputs = []
while True:
try:
outputs.append(await self.communicator.receive_output(timeout))
except asyncio.TimeoutError:
break
return outputs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import opentelemetry.instrumentation.asgi as otel_asgi
from opentelemetry.test.asgitestutil import AsgiTestBase
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import SpanKind
from opentelemetry.util.http import (
Expand All @@ -10,8 +9,11 @@
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
)

from . import AsyncAsgiTestBase
from .test_asgi_middleware import simple_asgi

_TIMEOUT = 0.01


async def http_app_with_custom_headers(scope, receive, send):
message = await receive()
Expand Down Expand Up @@ -90,7 +92,7 @@ async def websocket_app_with_custom_headers(scope, receive, send):
break


class TestCustomHeaders(AsgiTestBase, TestBase):
class TestCustomHeaders(AsyncAsgiTestBase):
constructor_params = {}
__test__ = False

Expand All @@ -108,7 +110,7 @@ def setUp(self):
**self.constructor_params,
)

def test_http_custom_request_headers_in_span_attributes(self):
async def test_http_custom_request_headers_in_span_attributes(self):
self.scope["headers"].extend(
[
(b"custom-test-header-1", b"test-header-value-1"),
Expand All @@ -119,8 +121,8 @@ def test_http_custom_request_headers_in_span_attributes(self):
]
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.request.header.custom_test_header_1": (
Expand All @@ -139,16 +141,16 @@ def test_http_custom_request_headers_in_span_attributes(self):
if span.kind == SpanKind.SERVER:
self.assertSpanHasAttributes(span, expected)

def test_http_repeat_request_headers_in_span_attributes(self):
async def test_http_repeat_request_headers_in_span_attributes(self):
self.scope["headers"].extend(
[
(b"custom-test-header-1", b"test-header-value-1"),
(b"custom-test-header-1", b"test-header-value-2"),
]
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.request.header.custom_test_header_1": (
Expand All @@ -159,15 +161,15 @@ def test_http_repeat_request_headers_in_span_attributes(self):
span = next(span for span in span_list if span.kind == SpanKind.SERVER)
self.assertSpanHasAttributes(span, expected)

def test_http_custom_request_headers_not_in_span_attributes(self):
async def test_http_custom_request_headers_not_in_span_attributes(self):
self.scope["headers"].extend(
[
(b"custom-test-header-1", b"test-header-value-1"),
]
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.request.header.custom_test_header_1": (
Expand All @@ -185,15 +187,15 @@ def test_http_custom_request_headers_not_in_span_attributes(self):
for key, _ in not_expected.items():
self.assertNotIn(key, span.attributes)

def test_http_custom_response_headers_in_span_attributes(self):
async def test_http_custom_response_headers_in_span_attributes(self):
self.app = otel_asgi.OpenTelemetryMiddleware(
http_app_with_custom_headers,
tracer_provider=self.tracer_provider,
**self.constructor_params,
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.response.header.custom_test_header_1": (
Expand All @@ -214,15 +216,15 @@ def test_http_custom_response_headers_in_span_attributes(self):
if span.kind == SpanKind.SERVER:
self.assertSpanHasAttributes(span, expected)

def test_http_repeat_response_headers_in_span_attributes(self):
async def test_http_repeat_response_headers_in_span_attributes(self):
self.app = otel_asgi.OpenTelemetryMiddleware(
http_app_with_repeat_headers,
tracer_provider=self.tracer_provider,
**self.constructor_params,
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.response.header.custom_test_header_1": (
Expand All @@ -233,15 +235,15 @@ def test_http_repeat_response_headers_in_span_attributes(self):
span = next(span for span in span_list if span.kind == SpanKind.SERVER)
self.assertSpanHasAttributes(span, expected)

def test_http_custom_response_headers_not_in_span_attributes(self):
async def test_http_custom_response_headers_not_in_span_attributes(self):
self.app = otel_asgi.OpenTelemetryMiddleware(
http_app_with_custom_headers,
tracer_provider=self.tracer_provider,
**self.constructor_params,
)
self.seed_app(self.app)
self.send_default_request()
self.get_all_output()
await self.send_default_request()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
not_expected = {
"http.response.header.custom_test_header_3": (
Expand All @@ -253,7 +255,7 @@ def test_http_custom_response_headers_not_in_span_attributes(self):
for key, _ in not_expected.items():
self.assertNotIn(key, span.attributes)

def test_websocket_custom_request_headers_in_span_attributes(self):
async def test_websocket_custom_request_headers_in_span_attributes(self):
self.scope = {
"type": "websocket",
"http_version": "1.1",
Expand All @@ -271,11 +273,11 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
"server": ("127.0.0.1", 80),
}
self.seed_app(self.app)
self.send_input({"type": "websocket.connect"})
self.send_input({"type": "websocket.receive", "text": "ping"})
self.send_input({"type": "websocket.disconnect"})
await self.send_input({"type": "websocket.connect"})
await self.send_input({"type": "websocket.receive", "text": "ping"})
await self.send_input({"type": "websocket.disconnect"})

self.get_all_output()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.request.header.custom_test_header_1": (
Expand All @@ -294,7 +296,9 @@ def test_websocket_custom_request_headers_in_span_attributes(self):
if span.kind == SpanKind.SERVER:
self.assertSpanHasAttributes(span, expected)

def test_websocket_custom_request_headers_not_in_span_attributes(self):
async def test_websocket_custom_request_headers_not_in_span_attributes(
self,
):
self.scope = {
"type": "websocket",
"http_version": "1.1",
Expand All @@ -309,11 +313,11 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
"server": ("127.0.0.1", 80),
}
self.seed_app(self.app)
self.send_input({"type": "websocket.connect"})
self.send_input({"type": "websocket.receive", "text": "ping"})
self.send_input({"type": "websocket.disconnect"})
await self.send_input({"type": "websocket.connect"})
await self.send_input({"type": "websocket.receive", "text": "ping"})
await self.send_input({"type": "websocket.disconnect"})

self.get_all_output()
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
not_expected = {
"http.request.header.custom_test_header_3": (
Expand All @@ -325,7 +329,7 @@ def test_websocket_custom_request_headers_not_in_span_attributes(self):
for key, _ in not_expected.items():
self.assertNotIn(key, span.attributes)

def test_websocket_custom_response_headers_in_span_attributes(self):
async def test_websocket_custom_response_headers_in_span_attributes(self):
self.scope = {
"type": "websocket",
"http_version": "1.1",
Expand All @@ -342,10 +346,10 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
**self.constructor_params,
)
self.seed_app(self.app)
self.send_input({"type": "websocket.connect"})
self.send_input({"type": "websocket.receive", "text": "ping"})
self.send_input({"type": "websocket.disconnect"})
self.get_all_output()
await self.send_input({"type": "websocket.connect"})
await self.send_input({"type": "websocket.receive", "text": "ping"})
await self.send_input({"type": "websocket.disconnect"})
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
expected = {
"http.response.header.custom_test_header_1": (
Expand All @@ -366,7 +370,9 @@ def test_websocket_custom_response_headers_in_span_attributes(self):
if span.kind == SpanKind.SERVER:
self.assertSpanHasAttributes(span, expected)

def test_websocket_custom_response_headers_not_in_span_attributes(self):
async def test_websocket_custom_response_headers_not_in_span_attributes(
self,
):
self.scope = {
"type": "websocket",
"http_version": "1.1",
Expand All @@ -383,10 +389,10 @@ def test_websocket_custom_response_headers_not_in_span_attributes(self):
**self.constructor_params,
)
self.seed_app(self.app)
self.send_input({"type": "websocket.connect"})
self.send_input({"type": "websocket.receive", "text": "ping"})
self.send_input({"type": "websocket.disconnect"})
self.get_all_output()
await self.send_input({"type": "websocket.connect"})
await self.send_input({"type": "websocket.receive", "text": "ping"})
await self.send_input({"type": "websocket.disconnect"})
await self.get_all_output(_TIMEOUT)
span_list = self.exporter.get_finished_spans()
not_expected = {
"http.response.header.custom_test_header_3": (
Expand Down
Loading