Skip to content

Tracing Plugin

记录 Agent 调用链,支持分布式追踪。

使用

typescript
import { createAgentServer, createTracingPlugin } from '@multi-agent/a2a'

const server = createAgentServer(config)
  .use(createTracingPlugin({
    provider: {
      reportTrace: async (record) => {
        console.log('Trace:', record)
      }
    }
  }))

await server.start()

TraceRecord 结构

typescript
interface TraceRecord {
  traceId: string       // 请求链 ID
  spanId: string        // 当前调用 ID
  parentSpanId?: string // 父调用 ID
  agentId: string       // Agent ID
  skill: string         // 技能名
  startedAt: string     // 开始时间 (ISO 8601)
  duration: number      // 耗时 (ms)
}

传递追踪信息到下游

在调用其他 Agent 时传递追踪信息:

typescript
const skill = defineSkill('task', async (params, ctx) => {
  const chatClient = createAgentClient({ agentId: 'chat-agent', address: 'a2a://localhost:50054' })

  const stream = await chatClient.call('chat', params, {
    metadata: {
      'x-trace-id': ctx.getMetadata('x-trace-id'),
      'x-span-id': currentSpanId,  // 成为下游的 parentSpanId
    }
  })

  // ...
})

调用链示例

调用链示例

上报数据:

json
[
  { "spanId": "span-001", "parentSpanId": null, "agentId": "host-agent" },
  { "spanId": "span-002", "parentSpanId": "span-001", "agentId": "chat-agent" },
  { "spanId": "span-003", "parentSpanId": "span-001", "agentId": "tool-agent" }
]

MIT Licensed