11# Kyanos AGENTS.md
22
3- > 本文件为 AI Agent 提供项目背景、结构、编码规范和工作流程信息。
3+ > This file provides AI agents with project background, structure, coding conventions, and workflow information.
44
5- ## 项目概述
5+ ## Project Overview
66
7- ** Kyanos** 是一个基于 eBPF 的网络问题分析工具,用于捕获和分析网络请求( HTTP、 Redis、 MySQL 等),帮助快速诊断网络相关问题,如慢查询、高流量和异常。
7+ ** Kyanos** is an eBPF-based network troubleshooting tool for capturing and analyzing network requests ( HTTP, Redis, MySQL, etc.), helping to quickly diagnose network-related issues such as slow queries, high traffic, and anomalies.
88
9- ### 核心特性
9+ ### Core Features
1010
11- 1 . ** 流量过滤 ** :支持按进程/容器、 L7 协议、请求/响应大小、延迟等多维度过滤
12- 2 . ** 流量分析 ** :聚合指标,快速定位问题(如带宽占满时找出最大响应)
13- 3 . ** 内核级延迟详情 ** :可视化展示数据包从网卡到 socket 缓冲区的各阶段耗时
14- 4 . ** SSL 自动解密 ** :自动解密 HTTPS 流量为明文
15- 5 . ** 零依赖 ** :单二进制文件,命令行交互
11+ 1 . ** Traffic Filtering ** : Multi-dimensional filtering by process/container, L7 protocol, request/response size, latency, etc.
12+ 2 . ** Traffic Analysis ** : Aggregated metrics for rapid issue identification (e.g., finding largest responses when bandwidth is saturated)
13+ 3 . ** Kernel-level Latency Details ** : Visual representation of packet journey from NIC to socket buffer
14+ 4 . ** Automatic SSL Decryption ** : Automatic HTTPS traffic decryption to plaintext
15+ 5 . ** Zero Dependencies ** : Single binary file with command-line interface
1616
17- ### 技术栈
17+ ### Technology Stack
1818
19- - ** 语言 ** : Go 1.23+
20- - ** 内核技术 ** : eBPF (使用 cilium/ebpf 库 )
21- - ** UI** : Charmbracelet 生态 (Bubble Tea, Bubbles, Lipgloss)
19+ - ** Language ** : Go 1.23+
20+ - ** Kernel Technology ** : eBPF (using cilium/ebpf library )
21+ - ** UI** : Charmbracelet ecosystem (Bubble Tea, Bubbles, Lipgloss)
2222- ** CLI** : Cobra + Viper
23- - ** 支持协议 ** : HTTP, Redis, MySQL, Kafka, MongoDB, RocketMQ, DNS
23+ - ** Supported Protocols ** : HTTP, Redis, MySQL, Kafka, MongoDB, RocketMQ, DNS
2424
2525---
2626
27- ## 项目结构
27+ ## Project Structure
2828
2929```
3030kyanos/
31- ├── main.go # 入口文件,调用 cmd.Execute()
32- ├── go.mod # Go 依赖管理
33- ├── Makefile # 构建脚本
34- ├── bpf/ # eBPF C 程序和头文件
35- │ ├── pktlatency.bpf.c # 主 eBPF 程序
36- │ ├── openssl_*.bpf.c # 各版本 OpenSSL uprobe
31+ ├── main.go # Entry point, calls cmd.Execute()
32+ ├── go.mod # Go dependency management
33+ ├── Makefile # Build scripts
34+ ├── bpf/ # eBPF C programs and headers
35+ │ ├── pktlatency.bpf.c # Main eBPF program
36+ │ ├── openssl_*.bpf.c # OpenSSL uprobes for various versions
3737│ ├── gotls.bpf.c # Go TLS uprobe
38- │ ├── *.h # BPF 头文件
39- │ └── loader/ # BPF 加载器 (Go)
40- ├── cmd/ # CLI 命令定义
41- │ ├── root.go # 根命令和全局 flags
42- │ ├── watch.go # watch 子命令
43- │ ├── stat.go # stat 子命令
44- │ └── *.go # 其他协议命令
45- ├── agent/ # 核心 Agent 逻辑
46- │ ├── agent.go # Agent 启动和主循环
47- │ ├── conn/ # 连接管理、事件处理
48- │ ├── protocol/ # 协议解析器
49- │ ├── analysis/ # 流量分析
50- │ ├── render/ # UI 渲染
51- │ └── metadata/ # 容器 /K8s 元数据
52- ├── common/ # 公共工具和类型
53- │ ├── log.go # 日志系统
54- │ ├── utils.go # 通用工具
38+ │ ├── *.h # BPF header files
39+ │ └── loader/ # BPF loader (Go)
40+ ├── cmd/ # CLI command definitions
41+ │ ├── root.go # Root command and global flags
42+ │ ├── watch.go # watch subcommand
43+ │ ├── stat.go # stat subcommand
44+ │ └── *.go # Other protocol commands
45+ ├── agent/ # Core Agent logic
46+ │ ├── agent.go # Agent startup and main loop
47+ │ ├── conn/ # Connection management, event handling
48+ │ ├── protocol/ # Protocol parsers
49+ │ ├── analysis/ # Traffic analysis
50+ │ ├── render/ # UI rendering
51+ │ └── metadata/ # Container /K8s metadata
52+ ├── common/ # Shared utilities and types
53+ │ ├── log.go # Logging system
54+ │ ├── utils.go # General utilities
5555│ └── *.go
56- ├── version/ # 版本信息
57- ├── vmlinux/ # 各架构的 vmlinux.h
58- ├── libbpf/ # libbpf 子模块
59- └── docs/ # 文档
56+ ├── version/ # Version information
57+ ├── vmlinux/ # vmlinux.h for different architectures
58+ ├── libbpf/ # libbpf submodule
59+ └── docs/ # Documentation
6060```
6161
6262---
6363
64- ## 构建系统
64+ ## Build System
6565
66- ### 依赖要求
66+ ### Dependencies
6767
6868- ** Go** : 1.23+
6969- ** Clang** : 10.0+
7070- ** LLVM** : 10.0+
71- - ** Linux 头文件 ** : linux-tools-common, linux-tools-generic
72- - ** 其他 ** : pkgconf, libelf-dev
71+ - ** Linux Headers ** : linux-tools-common, linux-tools-generic
72+ - ** Others ** : pkgconf, libelf-dev
7373
74- ### 常用构建命令
74+ ### Common Build Commands
7575
7676``` bash
77- # 开发构建(本地测试)
77+ # Development build (local testing)
7878make build-bpf && make
7979
80- # 生成带 BTF 的完整构建(用于低版本内核)
80+ # Full build with BTF (for older kernels)
8181make build-bpf && make btfgen BUILD_ARCH=x86_64 ARCH_BPF_NAME=x86 && make
8282
83- # 调试构建
83+ # Debug build
8484make kyanos-debug
8585
86- # 测试
86+ # Run tests
8787make test
8888
89- # 格式化代码
89+ # Format code
9090make format
9191```
9292
93- ### BPF 代码生成
93+ ### BPF Code Generation
9494
95- 项目使用 ` go generate ` 生成 BPF 骨架代码:
95+ The project uses ` go generate ` to generate BPF skeleton code:
9696
9797``` bash
98- # 在 bpf/loader/loader.go 中定义
98+ # Defined in bpf/loader/loader.go
9999//go:generate go run github.com/cilium/ebpf/cmd/bpf2go ...
100100
101101TARGET=amd64 go generate ./bpf/ # x86_64
@@ -104,52 +104,52 @@ TARGET=arm64 go generate ./bpf/ # arm64
104104
105105---
106106
107- ## 代码规范
107+ ## Coding Conventions
108108
109- ### Go 编码风格
109+ ### Go Style Guidelines
110110
111- 1 . ** 包命名 ** : 全小写,简短有意义,避免下划线
112- 2 . ** 文件命名 ** : 全小写,使用下划线分隔,如 ` kern_event_handler.go `
113- 3 . ** 接口命名 ** : 动词+名词,如 ` ProtocolStreamParser `
114- 4 . ** 错误处理 ** : 显式处理,使用 ` common.DefaultLog ` 记录
115- 5 . ** 日志 ** : 使用 ` common.AgentLog ` , ` common.BPFLog ` 等专用 logger
111+ 1 . ** Package Naming ** : All lowercase, short and meaningful, avoid underscores
112+ 2 . ** File Naming ** : All lowercase, use underscores for separation, e.g., ` kern_event_handler.go `
113+ 3 . ** Interface Naming ** : Verb + Noun, e.g., ` ProtocolStreamParser `
114+ 4 . ** Error Handling ** : Explicit handling, use ` common.DefaultLog ` for logging
115+ 5 . ** Logging ** : Use dedicated loggers like ` common.AgentLog ` , ` common.BPFLog `
116116
117- ### 关键模式
117+ ### Key Patterns
118118
119- #### Agent 启动流程
119+ #### Agent Startup Flow
120120
121121``` go
122122// agent/agent.go: SetupAgent()
123- 1 . 检查 BPF 权限 (CAP_BPF)
124- 2 . 初始化 ConnManager
125- 3 . 初始化 ProcessorManager
126- 4 . 加载 BPF 程序 (loader.LoadBPF )
127- 5 . 启动事件拉取 goroutines
128- 6 . 启动渲染 UI
123+ 1 . Check BPF permissions (CAP_BPF)
124+ 2 . Initialize ConnManager
125+ 3 . Initialize ProcessorManager
126+ 4 . Load BPF programs (loader.LoadBPF )
127+ 5 . Start event pulling goroutines
128+ 6 . Start rendering UI
129129```
130130
131- #### 协议解析器
131+ #### Protocol Parser
132132
133133``` go
134134// agent/protocol/protocol.go
135135
136- // 实现 ProtocolStreamParser 接口
136+ // Implement ProtocolStreamParser interface
137137type ProtocolStreamParser interface {
138138 Match (reqStreams, respStreams) []Record
139139 FindBoundary (streamBuffer, messageType, startPos) int
140140 ParseRequest (streamBuffer, messageType) *ParsedMessage
141141 // ...
142142}
143143
144- // 注册解析器
144+ // Register parser
145145func init () {
146146 ParsersMap[bpf.AgentTrafficProtocolTKProtocolXXX ] = func () ProtocolStreamParser {
147147 return &XXXStreamParser{}
148148 }
149149}
150150```
151151
152- #### eBPF Map 定义
152+ #### eBPF Map Definition
153153
154154``` c
155155// bpf/pktlatency.bpf.c
@@ -163,84 +163,84 @@ struct {
163163
164164---
165165
166- ## 测试
166+ ## Testing
167167
168- ### 测试结构
168+ ### Test Structure
169169
170170```
171171agent/
172- ├── agent_test.go # Agent 测试
173- ├── agent_utils_test.go # 工具测试
172+ ├── agent_test.go # Agent tests
173+ ├── agent_utils_test.go # Utility tests
174174└── protocol/
175- └── http_test.go # 协议解析测试
175+ └── http_test.go # Protocol parser tests
176176```
177177
178- ### 运行测试
178+ ### Running Tests
179179
180180```bash
181- # 所有测试
181+ # All tests
182182go test -v ./...
183183
184- # 特定包测试
184+ # Specific package tests
185185go test -v ./agent/...
186186
187- # 性能测试
187+ # Benchmark tests
188188go test -bench=. ./...
189189```
190190
191191---
192192
193- ## 常见问题
193+ ## Troubleshooting
194194
195- ### 1. BPF 加载失败
195+ ### 1. BPF Loading Failed
196196
197- - 检查内核版本(要求 3.10.0-957+ 或 4.14+)
198- - 检查 BTF 是否启用 : ` zgrep CONFIG_DEBUG_INFO_BTF /proc/config.gz `
199- - 使用 ` --btf ` 指定外部 BTF 文件
197+ - Check kernel version (requires 3.10.0-957+ or 4.14+)
198+ - Check if BTF is enabled : ` zgrep CONFIG_DEBUG_INFO_BTF /proc/config.gz `
199+ - Use ` --btf ` flag to specify external BTF file
200200
201- ### 2. 容器相关功能不工作
201+ ### 2. Container-related Features Not Working
202202
203- - 确保有访问 Docker/Containerd/CRI 的权限
204- - 使用 ` --docker-address ` , ` --containerd-address ` 指定端点
203+ - Ensure access to Docker/Containerd/CRI
204+ - Use ` --docker-address ` , ` --containerd-address ` to specify endpoints
205205
206- ### 3. SSL 解密失败
206+ ### 3. SSL Decryption Failed
207207
208- - 检查 OpenSSL 版本是否支持
209- - 确保进程有 ptrace 权限
208+ - Check if OpenSSL version is supported
209+ - Ensure the process has ptrace permissions
210210
211211---
212212
213- ## 贡献指南
213+ ## Contributing
214214
215- ### 添加新协议支持
215+ ### Adding New Protocol Support
216216
217- 1 . 在 ` bpf/protocol_inference.h ` 添加协议检测逻辑
218- 2 . 在 ` agent/protocol/ ` 创建解析器,实现 ` ProtocolStreamParser `
219- 3 . 在 ` cmd/ ` 添加对应的子命令
220- 4 . 添加测试用例
217+ 1 . Add protocol detection logic in ` bpf/protocol_inference.h `
218+ 2 . Create parser in ` agent/protocol/ ` implementing ` ProtocolStreamParser `
219+ 3 . Add corresponding subcommand in ` cmd/ `
220+ 4 . Add test cases
221221
222- ### 修改 BPF 代码
222+ ### Modifying BPF Code
223223
224- 1 . 修改 ` .c ` 或 ` .h ` 文件
225- 2 . 运行 ` make build-bpf ` 重新生成骨架代码
226- 3 . 测试验证
224+ 1 . Modify ` .c ` or ` .h ` files
225+ 2 . Run ` make build-bpf ` to regenerate skeleton code
226+ 3 . Test and verify
227227
228228---
229229
230- ## 参考资源
230+ ## Resources
231231
232- - ** 文档 ** : https://kyanos.io/
232+ - ** Documentation ** : https://kyanos.io/
233233- ** GitHub** : https://github.com/hengyoush/kyanos
234234- ** FAQ** : https://kyanos.io/faq.html
235- - ** eBPF 参考 ** : https://ebpf.io/
235+ - ** eBPF Reference ** : https://ebpf.io/
236236- ** Cilium eBPF** : https://github.com/cilium/ebpf
237237
238238---
239239
240- ## 相关项目
240+ ## Related Projects
241241
242- Kyanos 开发过程中参考了以下项目:
242+ Kyanos development was inspired by the following projects:
243243
244- - [ eCapture] ( https://ecapture.cc/zh/ ) - SSL 捕获
245- - [ pixie] ( https://github.com/pixie-io/pixie ) - K8s 可观测性
246- - [ ptcpdump] ( https://github.com/mozillazg/ptcpdump ) - 进程级 tcpdump
244+ - [ eCapture] ( https://ecapture.cc/zh/ ) - SSL capture
245+ - [ pixie] ( https://github.com/pixie-io/pixie ) - K8s observability
246+ - [ ptcpdump] ( https://github.com/mozillazg/ptcpdump ) - Process-level tcpdump
0 commit comments