Skip to content

Commit 7d1be5f

Browse files
committed
add test
1 parent 09c4164 commit 7d1be5f

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from opentelemetry.test.globals_test import reset_trace_globals
3333
from opentelemetry.test.test_base import TestBase
3434
from opentelemetry.util._importlib_metadata import entry_points
35-
35+
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
3636

3737
class HTTPMethod(Enum):
3838
"""HTTP methods and descriptions"""
@@ -76,9 +76,9 @@ def fixture_suppress():
7676

7777
@pytest_asyncio.fixture(name="server_fixture")
7878
async def fixture_server_fixture(tracer, aiohttp_server, suppress):
79-
_, memory_exporter = tracer
79+
tracer_provider, memory_exporter = tracer
8080

81-
AioHttpServerInstrumentor().instrument()
81+
AioHttpServerInstrumentor().instrument(tracer_provider=tracer_provider)
8282

8383
app = aiohttp.web.Application()
8484
app.add_routes([aiohttp.web.get("/test-path", default_handler)])
@@ -195,3 +195,34 @@ async def handler(request):
195195
# Clean up
196196
AioHttpServerInstrumentor().uninstrument()
197197
memory_exporter.clear()
198+
199+
200+
201+
@pytest.mark.asyncio
202+
@pytest.mark.parametrize(
203+
"tracer", [TestBase().create_tracer_provider(sampler=ParentBased(TraceIdRatioBased(0.05)))]
204+
)
205+
async def test_non_global_tracer_provider(
206+
tracer,
207+
server_fixture,
208+
aiohttp_client,
209+
):
210+
n_requests = 1000
211+
collection_ratio = 0.05
212+
n_expected_trace_ids = n_requests * collection_ratio
213+
214+
_, memory_exporter = tracer
215+
server, _ = server_fixture
216+
217+
assert len(memory_exporter.get_finished_spans()) == 0
218+
219+
client = await aiohttp_client(server)
220+
for _ in range(n_requests):
221+
await client.get("/test-path")
222+
223+
trace_ids = {
224+
span.context.trace_id
225+
for span in memory_exporter.get_finished_spans()
226+
if span.context is not None
227+
}
228+
assert 0.5 * n_expected_trace_ids <= len(trace_ids) <= 1.5 * n_expected_trace_ids

0 commit comments

Comments
 (0)