0

这些天我正在使用opentracing,我看到了一个中间件示例。但我不知道如何将它添加到我的龙卷风项目中。

代码在这里:

class TracerMiddleware(object):
    def __init__(self):
        # perform initialization similar to above, including installing
        # the client_hooks
        pass

    @gen.coroutine
    def __call__(self, request, handler, next_mw):
        request_wrapper = TornadoRequestWrapper(request=request)
        span = before_request(request=request_wrapper)

        @gen.coroutine
        def next_middleware_with_span():
            yield next_mw()

        yield run_coroutine_with_span(span=span, func=next_middleware_with_span)

        span.finish()


def run_coroutine_with_span(span, func, *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):
        return func(*args, **kwargs)
4

1 回答 1

0

Tornado 没有“中间件”的概念。这看起来像是设计用于Application(也许这个?)的子类。最好的办法是请这个“中间件”的作者提供更好的说明(参见这个问题)。

请注意,此实现依赖tornado.stack_context于 Tornado 6 中不再存在的 。它需要更新contextvars支持 Tornado 6。

于 2019-10-19T15:56:44.793 回答