1

I'm using the Open Tracing Python library for GRPC and am trying to build off of the example script here: https://github.com/opentracing-contrib/python-grpc/blob/master/examples/trivial/trivial_client.py.

Once I have sent a request through the intercepted channel, how do I find the trace-id value for the request? I want to use this to look at the traced data in the Jaeger UI.

Nate Glenn
  • 6,455
  • 8
  • 52
  • 95

2 Answers2

1

I had missed a key piece of documentation. In order to get a trace ID, you have to create a span on the client side. This span will have the trace ID that can be used to examine data in the Jaeger UI. The span has to be added into the GRPC messages via an ActiveSpanSource instance.

# 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)

Of course, you could switch the ordering of the with statements so that the span is created after the GRPC channel. That part doesn't make any difference.

Nate Glenn
  • 6,455
  • 8
  • 52
  • 95
0

Correct me, if I'm wrong. If you mean how to find the trace-id on the server side, you can try to access the OpenTracing span by get_active_span. The trace-id, I suppose, should be one of the tags in it.

Lidi Zheng
  • 1,801
  • 8
  • 13
  • I think I was unclear. I need the trace ID on the client side after sending a request. – Nate Glenn May 29 '19 at 09:08
  • I'm not an expert of OpenTracing. By reading its code, I see that on the client side, the OpenTracing interceptor provided the tracing span for each gRPC call, so you may obtain access the current span by `tracer.active_span`. Then you may have access to your attributes in the span object. – Lidi Zheng May 29 '19 at 18:13
  • That appears to only be present if the user provides an `ActiveSpanSource`, which I don't do (meaning that the traceIds are being produced by some other mechanism). – Nate Glenn Jun 11 '19 at 10:00
  • Do you think it is possible to get the trace ID in gRPC interceptor and then save it to thread locals or global variable? Then you can try to fetch the trace ID once the gRPC call started. – Lidi Zheng Jun 12 '19 at 18:20
  • I figured it out. I'll write it in another answer :) – Nate Glenn Jun 13 '19 at 15:35