You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何反序列化本地UDP端口接收的Jaeger span上报数据?

解决Jaeger UDP上报数据反序列化问题

原理说明

Jaeger客户端上报给Agent的UDP报文采用**Thrift紧凑型协议(TCompactProtocol)**序列化,报文结构对应Jaeger官方定义的Agent服务emitBatch接口的入参结构,直接依赖Thrift和Jaeger预定义的Thrift结构即可完成反序列化,不需要自行解析二进制格式。

步骤1:安装依赖

pip install jaeger-client thrift

步骤2:反序列化实现代码

from jaeger_client.agent.thrift import Agent
from thrift.protocol.TCompactProtocol import TCompactProtocol
from thrift.transport.TTransport import TMemoryBuffer

def decode_jaeger_udp_packet(packet_data: bytes):
    # 用内存缓冲区承载接收到的二进制报文
    transport = TMemoryBuffer(packet_data)
    # 指定Jaeger约定的紧凑型Thrift协议做解析
    protocol = TCompactProtocol(transport)
    client = Agent.Client(protocol)
    # 读取emitBatch方法的入参,就是上报的完整批次数据
    protocol.readMessageBegin()
    args = Agent.emitBatch_args()
    args.read(protocol)
    protocol.readMessageEnd()
    return args.batch

# 把你接收得到的字节数组直接传入即可
# 示例:batch = decode_jaeger_udp_packet(all_data)

结构化数据读取示例

返回的batch对象是完全结构化的,核心字段直接读取即可:

# 读取上报服务的基础信息
print("服务名:", batch.process.serviceName)
print("服务标签:", batch.process.tags)

# 读取Span的全量信息
if batch.spans:
    first_span = batch.spans[0]
    print("操作名:", first_span.operationName)
    print("TraceID:", hex(first_span.traceIdLow))
    print("SpanID:", hex(first_span.spanId))
    print("Span自定义标签:", first_span.tags)
    print("Span日志:", first_span.logs)

注意事项

  • 必须使用TCompactProtocol解析,使用默认的二进制Thrift协议会直接解析失败
  • 如果是128位TraceID,需要把traceIdHigh和traceIdLow两个字段拼接后才是完整ID
  • 不要把多个UDP包的字节拼接在一起解析,每个UDP包对应一个独立的上报批次,需要单独解析

内容的提问来源于stack exchange,提问作者Guillermo Ares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 16:06:04