反序列化 Jaeger 跨度报告

Deserialize Jaeger span reports

出于测试目的,我尝试在 localhost 中接收 Jaeger 报告并检查某些值是否符合要求。我这样设置 Jaeger:

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 是我在 localhost 中打开的套接字的端口,如下所示:

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

在我的测试中,我是

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)

所有这一切都非常有效。我在套接字中得到 something,我看到它是

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

现在我需要将其反序列化为我可以正确检查的内容。我尝试了各种方法,none 成功了。有人知道怎么做吗?

好的,所以我终于弄明白了。此代码段展示了如何做这件事。

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