【问题标题】:get trace ID of sent request获取已发送请求的跟踪 ID
【发布时间】:2019-05-14 16:47:08
【问题描述】:

我正在为 GRPC 使用 Open Tracing Python 库,并尝试在此处构建示例脚本:https://github.com/opentracing-contrib/python-grpc/blob/master/examples/trivial/trivial_client.py

一旦我通过截获的通道发送了一个请求,我如何找到该请求的 trace-id 值?我想用它来查看 Jaeger UI 中的跟踪数据。

【问题讨论】:

    标签: python grpc zipkin opentracing jaeger


    【解决方案1】:

    我错过了一个关键的文档。为了获得跟踪 ID,您必须在客户端创建一个跨度。此范围将具有可用于检查 Jaeger UI 中的数据的跟踪 ID。必须通过 ActiveSpanSource 实例将 span 添加到 GRPC 消息中。

    # opentracing-related imports
    from grpc_opentracing import open_tracing_client_interceptor, ActiveSpanSource
    from grpc_opentracing.grpcext import intercept_channel
    from jaeger_client import Config
    
    # dummy class to hold span data for passing into GRPC channel
    class FixedActiveSpanSource(ActiveSpanSource):
    
        def __init__(self):
            self.active_span = None
    
        def get_active_span(self):
            return self.active_span
    
    config = Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name='foo')
    
    tracer = config.initialize_tracer()
    
    # ...
    # In the method where GRPC requests are sent
    # ...
    active_span_source = FixedActiveSpanSource()
    tracer_interceptor = open_tracing_client_interceptor(
        tracer,
        log_payloads=True,
        active_span_source=active_span_source)
    
    with tracer.start_span('span-foo') as span:
        print(f"Created span: trace_id:{span.trace_id:x}, span_id:{span.span_id:x}, parent_id:{span.parent_id}, flags:{span.flags:x}")
        # provide the span to the GRPC interceptor here
        active_span_source.active_span = span
        with grpc.insecure_channel(...) as channel:
            channel = intercept_channel(channel, tracer_interceptor)
    

    当然,您可以切换with 语句的顺序,以便在 GRPC 通道之后创建 span。那部分没有任何区别。

    【讨论】:

      【解决方案2】:

      如果我错了,请纠正我。如果您的意思是如何在服务器端找到trace-id,您可以尝试通过get_active_span 访问OpenTracing span。我想,trace-id 应该是其中的标签之一。

      【讨论】:

      • 我想我不清楚。发送请求后,我需要客户端的跟踪 ID。
      • 我不是 OpenTracing 专家。通过阅读其代码,我看到在客户端,OpenTracing 拦截器为每个 gRPC 调用提供了跟踪跨度,因此您可以通过tracer.active_span 获取当前跨度的访问权限。然后,您可以访问 span 对象中的属性。
      • 只有当用户提供ActiveSpanSource 时才会出现这种情况,而我没有这样做(这意味着 traceId 是由其他机制生成的)。
      • 你认为可以在 gRPC 拦截器中获取跟踪 ID,然后将其保存到线程本地或全局变量吗?然后,您可以在 gRPC 调用开始后尝试获取跟踪 ID。
      • 我想通了。我会在另一个答案中写它:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2020-02-05
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多