python - 获取发送请求的跟踪 ID

标签 python grpc zipkin opentracing jaeger

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

通过拦截的 channel 发送请求后,如何找到该请求的trace-id 值?我想用它来查看 Jaeger UI 中的跟踪数据。

最佳答案

我错过了一个关键的文档。为了获取跟踪 ID,您必须在客户端创建一个跨度。该跨度将具有可用于检查 Jaeger UI 中的数据的跟踪 ID。必须通过 ActiveSpanSource 实例将跨度添加到 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 channel 之后创建跨度。这部分没有任何区别。

关于python - 获取发送请求的跟踪 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56135086/

相关文章:

python - Python 的 "None"的 Matlab 等价物

带密码子的 Python 正则表达式

ios - 多个完成 block

java - Gradle 无法检测到 osdetector 插件

node.js - 你如何在 TypeScript 中实现 GRPC 服务器?

elasticsearch - Elasticsearch -计数api显示索引的总文档计数不正确

java - 将经度、纬度转换为来自 s2 geometry-library python 的 cell id

mysql - 使用 Zipkin TracingStatementInterceptor 启动 c3po 连接池时发生死锁

spring - 跨线程发送 TraceId

python - 互信息的香农熵