Skip to content

Commit df15faa

Browse files
committed
Add test for aiohttp-server
And make the implementation testable
1 parent 926d0bb commit df15faa

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ class AioHttpServerInstrumentor(BaseInstrumentor):
315315
"""
316316

317317
def _instrument(self, **kwargs):
318+
# update the excluded urls value at instrument time so we can test it
319+
global _excluded_urls
320+
_excluded_urls = get_excluded_urls("AIOHTTP_SERVER")
321+
318322
self._original_app = web.Application
319323
setattr(web, "Application", _InstrumentedApplication)
320324

instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,36 @@ async def handler(request):
195195
# Clean up
196196
AioHttpServerInstrumentor().uninstrument()
197197
memory_exporter.clear()
198+
199+
200+
@pytest.mark.asyncio
201+
@pytest.mark.parametrize(
202+
"env_var",
203+
["OTEL_PYTHON_AIOHTTP_SERVER_EXCLUDED_URLS", "OTEL_PYTHON_EXCLUDED_URLS"],
204+
)
205+
async def test_excluded_urls(tracer, aiohttp_server, monkeypatch, env_var):
206+
"""Test that excluded env vars are taken into account."""
207+
_, memory_exporter = tracer
208+
209+
monkeypatch.setenv(env_var, "/status/200")
210+
AioHttpServerInstrumentor().instrument()
211+
212+
app = aiohttp.web.Application()
213+
214+
async def handler(request):
215+
return aiohttp.web.Response(text="hello")
216+
217+
app.router.add_get("/status/200", handler)
218+
219+
server = await aiohttp_server(app)
220+
221+
url = f"http://{server.host}:{server.port}/status/200"
222+
async with aiohttp.ClientSession() as session:
223+
async with session.get(url) as response:
224+
assert response.status == 200
225+
assert await response.text() == "hello"
226+
227+
spans = memory_exporter.get_finished_spans()
228+
assert len(spans) == 0
229+
230+
AioHttpServerInstrumentor().uninstrument()

0 commit comments

Comments
 (0)