反序列化Jaeger span报告



出于测试目的,我试图在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中打开的socket的端口,如下所示:

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)

这一切都很有魅力。我在套接字中得到一些,我看到的是

bytearray(b'x82x81x01temitBatchx1cx1cx18x0becho_serverx19<x18x0ejaeger.versionx15x00x18x0cPython-4.8.0x00x18x02ipx15x00x18r172.16.20.205x00x18x08hostnamex15x00x18x1cGuillermos-MacBook-Pro.localx00x00x19x1cx16xe6xc2x84x96xe7xd4xf2x88x04x16x00x16x8bxebxa6xb0x81x99xecxfcxe3x01x16x00x18x03GET%x02x16xe6xcaxfbxe4x88xcdxe7x05x16xfcx17x19lx18x0csampler.typex15x00x18x05constx00x18rsampler.paramx15x041x00x18tspan.kindx15x00x18x06serverx00x18x08http.urlx15x00x18x1dhttp://127.0.0.1:54298/healthx00x18x0bhttp.methodx15x00x18x03GETx00x18tpeer.ipv4x15x00x18t127.0.0.1x00x19x0cx00x00x00')

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

好了,我终于弄明白了。下面的代码片段展示了如何做这件事。

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

相关内容

  • 没有找到相关文章

最新更新