def test_http_fetch_with_interceptor(base_url, http_client, tornado_http_patch, tracer): @tornado.gen.coroutine def make_downstream_call(): resp = yield http_client.fetch(base_url) raise tornado.gen.Return(resp) with patch('opentracing.tracer', tracer): assert opentracing.tracer == tracer # sanity check that patch worked span = tracer.start_span('test') trace_id = '{:x}'.format(span.context.trace_id) with patch('opentracing_instrumentation.http_client.ClientInterceptors') as MockClientInterceptors: mock_interceptor = Mock(spec=OpenTracingInterceptor) MockClientInterceptors.get_interceptors.return_value = [mock_interceptor] with span_in_stack_context(span): response = make_downstream_call() response = yield response # cannot yield when in StackContext context mock_interceptor.process.assert_called_once() assert mock_interceptor.process.call_args_list[0][1]['span'].tracer == tracer span.finish() assert response.code == 200 assert response.body.decode('utf-8') == trace_id
def run_coroutine_with_span(span, coro, *args, **kwargs): """Wrap the execution of a Tornado coroutine func in a tracing span. This makes the span available through the get_current_span() function. :param span: The tracing span to expose. :param func: Co-routine to execute in the scope of tracing span. :param args: Positional args to func, if any. :param kwargs: Keyword args to func, if any. """ with span_in_stack_context(span=span): return coro(*args, **kwargs)
def run_coroutine_with_span(span, coro, *args, **kwargs): """Wrap the execution of a Tornado coroutine func in a tracing span. This makes the span available through the get_current_span() function. :param span: The tracing span to expose. :param coro: Co-routine to execute in the scope of tracing span. :param args: Positional args to func, if any. :param kwargs: Keyword args to func, if any. """ with span_in_stack_context(span=span): return coro(*args, **kwargs)
def span_in_context(self, span): """ Store the `span` in the request context and return a `StackContext`. This method is meant to be used as a context manager: .. code-block:: python with tchannel.context_provider.span_in_context(span): f = handler_fn() res = yield f Note: StackContext does not allow yield when used a context manager. Instead, save the future and yield it outside of `with:` statement. :param span: an OpenTracing Span :return: ``StackContext``-based context manager """ return opentracing_instrumentation.span_in_stack_context(span)
def test_http_fetch(base_url, http_client, tornado_http_patch, tracer): @tornado.gen.coroutine def make_downstream_call(): resp = yield http_client.fetch(base_url) raise tornado.gen.Return(resp) with patch('opentracing.tracer', tracer): assert opentracing.tracer == tracer # sanity check that patch worked span = tracer.start_span('test') trace_id = '{:x}'.format(span.context.trace_id) with span_in_stack_context(span): response = make_downstream_call() response = yield response # cannot yield when in StackContext context span.finish() assert response.code == 200 assert response.body.decode('utf-8') == trace_id
def test_http_fetch(base_url, http_client, tornado_http_patch, tracer): @tornado.gen.coroutine def make_downstream_call(): resp = yield http_client.fetch(base_url) raise tornado.gen.Return(resp) with patch('opentracing.tracer', tracer): assert opentracing.tracer == tracer # sanity check that patch worked span = tracer.start_span('test') trace_id = '{:x}'.format(span.context.trace_id) with span_in_stack_context(span): response = make_downstream_call() response = yield response # cannot yield when in StackContext context span.finish() assert response.code == 200 assert response.body == trace_id