async def test_multiple_full_request(patched_app_tracer, aiohttp_client, loop): NUMBER_REQUESTS = 10 responses = [] app, tracer = patched_app_tracer tracer.configure(context_provider=DefaultContextProvider()) client = await aiohttp_client(app) # it should produce a wrong trace, but the Context must # be finished def make_requests(): url = client.make_url("/delayed/") response = request.urlopen(str(url)).read().decode("utf-8") responses.append(response) # blocking call executed in different threads threads = [ threading.Thread(target=make_requests) for _ in range(NUMBER_REQUESTS) ] for t in threads: t.start() # yield back to the event loop until all requests are processed while len(responses) < NUMBER_REQUESTS: await asyncio.sleep(0.001) for response in responses: assert "Done" == response for t in threads: t.join() # the trace is wrong but the spans are finished and written spans = tracer.pop() assert NUMBER_REQUESTS == len(spans)
async def test_get_call_context(tracer): tracer.configure(context_provider=DefaultContextProvider()) ctx = tracer.current_trace_context() assert ctx is None # test that it behaves the wrong way task = asyncio_current_task() assert task task_ctx = getattr(task, "__datadog_context", None) assert task_ctx is None
async def test_get_call_context(tracer): tracer.configure(context_provider=DefaultContextProvider()) # it should return a context even if not attached to the Task ctx = tracer.get_call_context() assert ctx is not None # test that it behaves the wrong way task = asyncio_current_task() assert task task_ctx = getattr(task, "__datadog_context", None) assert task_ctx is None
def get_context_provider_for_scope_manager(scope_manager): """Returns the context_provider to use with a given scope_manager.""" scope_manager_type = type(scope_manager).__name__ # avoid having to import scope managers which may not be compatible # with the version of python being used if scope_manager_type == 'AsyncioScopeManager': dd_context_provider = ddtrace.contrib.asyncio.context_provider elif scope_manager_type == 'GeventScopeManager': dd_context_provider = ddtrace.contrib.gevent.context_provider else: dd_context_provider = DefaultContextProvider() return dd_context_provider
async def test_trace_multiple_calls(tracer): tracer.configure(context_provider=DefaultContextProvider()) async def coro(): # another traced coroutine with tracer.trace("coroutine"): await asyncio.sleep(0.01) futures = [asyncio.ensure_future(coro()) for x in range(1000)] for future in futures: await future # the trace is wrong but the Context is finished traces = tracer.pop_traces() assert 1 == len(traces) assert 1000 == len(traces[0])
async def test_full_request(patched_app_tracer, aiohttp_client, loop): app, tracer = patched_app_tracer tracer.configure(context_provider=DefaultContextProvider()) client = await aiohttp_client(app) # it should create a root span when there is a handler hit # with the proper tags request = await client.request("GET", "/template/") assert 200 == request.status await request.text() # the trace is created traces = tracer.pop_traces() assert 1 == len(traces) assert 2 == len(traces[0]) request_span = traces[0][0] template_span = traces[0][1] # request assert_is_measured(request_span) assert "aiohttp-web" == request_span.service assert "aiohttp.request" == request_span.name assert "GET /template/" == request_span.resource # template assert "aiohttp-web" == template_span.service assert "aiohttp.template" == template_span.name assert "aiohttp.template" == template_span.resource
def get_context_provider_for_scope_manager(scope_manager): # type: (ScopeManager) -> BaseContextProvider """Returns the context_provider to use with a given scope_manager.""" scope_manager_type = type(scope_manager).__name__ # avoid having to import scope managers which may not be compatible # with the version of python being used if scope_manager_type == "AsyncioScopeManager": import ddtrace.contrib.asyncio dd_context_provider = ddtrace.contrib.asyncio.context_provider # type: BaseContextProvider elif scope_manager_type == "GeventScopeManager": import ddtrace.contrib.gevent dd_context_provider = ddtrace.contrib.gevent.context_provider else: from ddtrace.provider import DefaultContextProvider dd_context_provider = DefaultContextProvider() _patch_scope_manager(scope_manager, dd_context_provider) return dd_context_provider
def enable_tracing(self): # aiohttp TestCase with the wrong context provider trace_app(self.app, self.tracer) patch() Pin.override(aiohttp_jinja2, tracer=self.tracer) self.tracer.configure(context_provider=DefaultContextProvider())
def setUp(self): # Asyncio TestCase with the wrong context provider super(TestAsyncioSafety, self).setUp() self.tracer.configure(context_provider=DefaultContextProvider())