获取已发送请求的跟踪ID

2024-06-10 09:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

一旦我通过截获的通道发送了一个请求,如何找到该请求的traceid值?我想用这个来查看Jaeger用户界面中的跟踪数据。在


Tags: pyhttpsgithubmaster脚本comclient示例
2条回答

我错过了一份关键的文件。为了获得跟踪ID,必须在客户端创建一个span。此范围将具有可用于检查Jaeger UI中的数据的跟踪ID。span必须通过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通道之后创建span。那部分没什么区别。在

如果我错了,请纠正我。如果您想知道如何在服务器端找到跟踪id,那么可以尝试通过get_active_span访问OpenTracing范围。我想,跟踪id应该是其中的一个标记。在

相关问题 更多 >