Use HTTP mock server for aiohttp tests (#1849)

Fixes #1842
This commit is contained in:
Diego Hurtado
2023-06-13 10:40:41 +02:00
committed by GitHub
parent 776f9d4643
commit 26c673e7c9
2 changed files with 23 additions and 11 deletions

View File

@ -38,6 +38,7 @@ instruments = [
] ]
test = [ test = [
"opentelemetry-instrumentation-aiohttp-client[instruments]", "opentelemetry-instrumentation-aiohttp-client[instruments]",
"http-server-mock"
] ]
[project.entry-points.opentelemetry_instrumentor] [project.entry-points.opentelemetry_instrumentor]

View File

@ -23,6 +23,7 @@ from unittest import mock
import aiohttp import aiohttp
import aiohttp.test_utils import aiohttp.test_utils
import yarl import yarl
from http_server_mock import HttpServerMock
from pkg_resources import iter_entry_points from pkg_resources import iter_entry_points
from opentelemetry import context from opentelemetry import context
@ -313,18 +314,26 @@ class TestAioHttpIntegration(TestBase):
def test_credential_removal(self): def test_credential_removal(self):
trace_configs = [aiohttp_client.create_trace_config()] trace_configs = [aiohttp_client.create_trace_config()]
url = "http://username:password@httpbin.org/status/200" app = HttpServerMock("test_credential_removal")
with self.subTest(url=url):
async def do_request(url): @app.route("/status/200")
async with aiohttp.ClientSession( def index():
trace_configs=trace_configs, return "hello"
) as session:
async with session.get(url):
pass
loop = asyncio.get_event_loop() url = "http://username:password@localhost:5000/status/200"
loop.run_until_complete(do_request(url))
with app.run("localhost", 5000):
with self.subTest(url=url):
async def do_request(url):
async with aiohttp.ClientSession(
trace_configs=trace_configs,
) as session:
async with session.get(url):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(do_request(url))
self.assert_spans( self.assert_spans(
[ [
@ -333,7 +342,9 @@ class TestAioHttpIntegration(TestBase):
(StatusCode.UNSET, None), (StatusCode.UNSET, None),
{ {
SpanAttributes.HTTP_METHOD: "GET", SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: "http://httpbin.org/status/200", SpanAttributes.HTTP_URL: (
"http://localhost:5000/status/200"
),
SpanAttributes.HTTP_STATUS_CODE: int(HTTPStatus.OK), SpanAttributes.HTTP_STATUS_CODE: int(HTTPStatus.OK),
}, },
) )