Tool calls produce no span and the kubectl subprocess drops trace context

未关闭
#102 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

@braghettos 已经在做这个了。

开始于 2026年11月11日。

  • #103 来自 @braghettos —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
go, kubernetes

调研方向

Start with cmd/main.go, internal/telemetry/middleware.go, and pkg/k8s/k8s.go; trace how the HTTP handler extracts context and how runKubectlCommand starts the subprocess. First add or locate tests for incoming traceparent propagation and tool-call span creation. Done means failed and successful tool calls have the expected spans; carrying context through Kubernetes API requests may require replacing kubectl with client-go.

由索引模型根据 Issue 内容生成。

描述

Summary

With tracing enabled, an MCP tool call to kagent-tools doesn't show up in the caller's trace. The server extracts the incoming W3C traceparent, but it never starts a span for the request or the tool call. The k8s tools then run kubectl as a subprocess, which receives no trace context, so the Kubernetes API requests behind the call can't join the trace either.

We see this as an agent turn whose trace ends at the gateway's MCP hop: the tool call itself, and everything it did against the API server, is invisible.

Where (v0.3.1)

  • cmd/main.go: the HTTP handler is wrapped only in telemetry.HTTPMiddleware. There's no otelhttp handler and no other server span.
  • internal/telemetry/middleware.go HTTPMiddleware: it calls propagator.Extract(...) and stores the trace and span IDs in the context, but starts no span. Its own comment says ExtractTraceInfo has no non-test callers since the Go SDK migration (#66).
  • pkg/k8s/k8s.go runKubectlCommand / runKubectlCommandWithTimeout: these run kubectl through commands.NewCommandBuilder("kubectl"). The child process gets no traceparent, and kubectl has no way to send one.

The only spans in a tool call's context are the cache.* spans.

Expected

  • A server span per MCP request (or per tools/call), a child of the incoming traceparent, named for the tool (for example tools/call k8s_get_resources), with the tool name and an error status on failure.
  • Ideally, the Kubernetes API requests the tool makes are children of that span, so API-server tracing (APIServerTracing) links up with the agent's trace.

Possible fixes

  1. Wrap the MCP handler in otelhttp.NewHandler (or start a span in HTTPMiddleware), and start a child span per tool call in the handlers. That alone makes tool calls visible in the caller's trace.
  2. For the Kubernetes requests: use client-go in-process for the read verbs, with an otelhttp transport, instead of a kubectl subprocess. Then traceparent reaches the API server. A subprocess can't carry it, because kubectl doesn't read trace context from its environment or flags.

Happy to help test a change.

主要语言
Go
星标
39
派生
35
平均合并
4 天 10 小时
30 天内合并 PR
8

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

kagent-dev/tools 的其他 Issue

查看 kagent-dev/tools 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。