|
32 | 32 | from opentelemetry.test.globals_test import reset_trace_globals |
33 | 33 | from opentelemetry.test.test_base import TestBase |
34 | 34 | from opentelemetry.util._importlib_metadata import entry_points |
35 | | - |
| 35 | +from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased |
36 | 36 |
|
37 | 37 | class HTTPMethod(Enum): |
38 | 38 | """HTTP methods and descriptions""" |
@@ -76,9 +76,9 @@ def fixture_suppress(): |
76 | 76 |
|
77 | 77 | @pytest_asyncio.fixture(name="server_fixture") |
78 | 78 | async def fixture_server_fixture(tracer, aiohttp_server, suppress): |
79 | | - _, memory_exporter = tracer |
| 79 | + tracer_provider, memory_exporter = tracer |
80 | 80 |
|
81 | | - AioHttpServerInstrumentor().instrument() |
| 81 | + AioHttpServerInstrumentor().instrument(tracer_provider=tracer_provider) |
82 | 82 |
|
83 | 83 | app = aiohttp.web.Application() |
84 | 84 | app.add_routes([aiohttp.web.get("/test-path", default_handler)]) |
@@ -195,3 +195,34 @@ async def handler(request): |
195 | 195 | # Clean up |
196 | 196 | AioHttpServerInstrumentor().uninstrument() |
197 | 197 | 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