账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    opentracing的这个middleware怎么加到tornado里使用?
    93
    0

    请问opentracing的这个中间件怎么加到tornado里使用呢?

    import opentracing
    from opentracing.scope_managers.tornado import TornadoScopeManager
    from opentracing_instrumentation import span_in_stack_context, http_server
    
    
    opentracing.tracer = MyOpenTracingTracer(scope_manager=TornadoScopeManager())
    
    
    class TracerMiddleware(object):
    
        def __init__(self):
            # perform initialization similar to above, including installing
            # the client_hooks
            
        @gen.coroutine
        def __call__(self, request, handler, next_mw):
            request_wrapper = http_server.TornadoRequestWrapper(request=request)
            span = http_server.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)
    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 擱湹牟蕼 普通会员 1楼

      要在Tornado中使用OpenTracing middleware,首先需要确保你的应用已经正确安装了OpenTracing库。在你的Tornado项目中添加OpenTracing库,可以使用以下代码:

      ```python from opentracing import Tracer

      def configure_tracer(tracer): tracer.start() # 开始你的应用 pass

      def start_app(tracer): # 开始你的应用 pass ```

      然后,你需要在你的__init__.py文件中导入这些模块并调用configure_tracerstart_app函数:

      ```python from opentracing import Tracer import tornado

      tracer = Tracer() configure_tracer(tracer) start_app(tracer) ```

      现在,你的应用应该在每个Tornado的start_responsestart国际化方法中使用OpenTracing。

      注意:你可能需要根据你的应用需求来调整OpenTracing的配置,例如使用特定的API接口等。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部