pythonencodingdeserializationjaegeropentracing

Deserialize Jaeger span reports


For testing purposes, I'm trying to receive Jaeger reports in localhost and check that some values are what they should. I set up Jaeger like this:

def setup_env(tracing_enabled, tracing_port):
    os.environ['JAEGER_DISABLED'] = 'false'
    os.environ['JAEGER_REPORTER_FLUSH_INTERVAL'] = '1ms'
    os.environ['JAEGER_AGENT_HOST'] = 'localhost'
    os.environ['JAEGER_AGENT_PORT'] = f'{tracing_port}'
    os.environ['JAEGER_SAMPLER_TYPE'] = 'const'
    os.environ['JAEGER_SAMPLER_PARAM'] = '1'
    os.environ['JAEGER_REPORTER_LOG_SPANS'] = 'true'

tracing_port is the port of a socket I opened in localhost like this:

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1', 0))
sock.setblocking(0)

In my test, I'm

def test_jaeger():
    // start the service ...
    
    all_data = bytearray()
    data = bytes()
    try:
        data, _ = udp_socket.recvfrom(1024)
    except BlockingIOError:
        pass

    while data:

        all_data.extend(data)
        try:
            data, _ = udp_socket.recvfrom(1024)
        except BlockingIOError:
            data = None

    print(all_data)

All this works like a charm. I get something in the socket, which I get to see as

bytearray(b'\x82\x81\x01\temitBatch\x1c\x1c\x18\x0becho_server\x19<\x18\x0ejaeger.version\x15\x00\x18\x0cPython-4.8.0\x00\x18\x02ip\x15\x00\x18\r172.16.20.205\x00\x18\x08hostname\x15\x00\x18\x1cGuillermos-MacBook-Pro.local\x00\x00\x19\x1c\x16\xe6\xc2\x84\x96\xe7\xd4\xf2\x88\x04\x16\x00\x16\x8b\xeb\xa6\xb0\x81\x99\xec\xfc\xe3\x01\x16\x00\x18\x03GET%\x02\x16\xe6\xca\xfb\xe4\x88\xcd\xe7\x05\x16\xfc\x17\x19l\x18\x0csampler.type\x15\x00\x18\x05const\x00\x18\rsampler.param\x15\x041\x00\x18\tspan.kind\x15\x00\x18\x06server\x00\x18\x08http.url\x15\x00\x18\x1dhttp://127.0.0.1:54298/health\x00\x18\x0bhttp.method\x15\x00\x18\x03GET\x00\x18\tpeer.ipv4\x15\x00\x18\t127.0.0.1\x00\x19\x0c\x00\x00\x00')

Now I need to deserialize this into something I can properly inspect. I tried all kinds of things, none worked. Does anybody know how to do this?


Solution

  • Ok, so I eventually figured it out. This snippet shows how to do the thing.

    from jaeger_client.thrift_gen.agent.Agent import emitBatch_args
    from thrift.protocol.TCompactProtocol import TCompactProtocol
    from thrift.transport.TTransport import TMemoryBuffer
    
    def deserialize_jaeger_batch(bin_data: bytearray):
        trans = TMemoryBuffer(data)
        prot = TCompactProtocol(trans)
        prot.readMessageBegin()
        emitBatch = emitBatch_args()
        emitBatch.read(prot)
        prot.readMessageEnd()
    
        return emitBatch.batch