This file provides AI agents with project background, structure, coding conventions, and workflow information.
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.
- Traffic Filtering: Multi-dimensional filtering by process/container, L7 protocol, request/response size, latency, etc.
- Traffic Analysis: Aggregated metrics for rapid issue identification (e.g., finding largest responses when bandwidth is saturated)
- Kernel-level Latency Details: Visual representation of packet journey from NIC to socket buffer
- Automatic SSL Decryption: Automatic HTTPS traffic decryption to plaintext
- Zero Dependencies: Single binary file with command-line interface
- Language: Go 1.23+
- Kernel Technology: eBPF (using cilium/ebpf library)
- UI: Charmbracelet ecosystem (Bubble Tea, Bubbles, Lipgloss)
- CLI: Cobra + Viper
- Supported Protocols: HTTP, Redis, MySQL, Kafka, MongoDB, RocketMQ, DNS
kyanos/
├── main.go # Entry point, calls cmd.Execute()
├── go.mod # Go dependency management
├── Makefile # Build scripts
├── bpf/ # eBPF C programs and headers
│ ├── pktlatency.bpf.c # Main eBPF program
│ ├── openssl_*.bpf.c # OpenSSL uprobes for various versions
│ ├── gotls.bpf.c # Go TLS uprobe
│ ├── *.h # BPF header files
│ └── loader/ # BPF loader (Go)
├── cmd/ # CLI command definitions
│ ├── root.go # Root command and global flags
│ ├── watch.go # watch subcommand
│ ├── stat.go # stat subcommand
│ └── *.go # Other protocol commands
├── agent/ # Core Agent logic
│ ├── agent.go # Agent startup and main loop
│ ├── conn/ # Connection management, event handling
│ ├── protocol/ # Protocol parsers
│ ├── analysis/ # Traffic analysis
│ ├── render/ # UI rendering
│ └── metadata/ # Container/K8s metadata
├── common/ # Shared utilities and types
│ ├── log.go # Logging system
│ ├── utils.go # General utilities
│ └── *.go
├── version/ # Version information
├── vmlinux/ # vmlinux.h for different architectures
├── libbpf/ # libbpf submodule
└── docs/ # Documentation
- Go: 1.23+
- Clang: 10.0+
- LLVM: 10.0+
- Linux Headers: linux-tools-common, linux-tools-generic
- Others: pkgconf, libelf-dev
# Development build (local testing)
make build-bpf && make
# Full build with BTF (for older kernels)
make build-bpf && make btfgen BUILD_ARCH=x86_64 ARCH_BPF_NAME=x86 && make
# Debug build
make kyanos-debug
# Run tests
make test
# Format code
make formatThe project uses go generate to generate BPF skeleton code:
# Defined in bpf/loader/loader.go
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go ...
TARGET=amd64 go generate ./bpf/ # x86_64
TARGET=arm64 go generate ./bpf/ # arm64- Package Naming: All lowercase, short and meaningful, avoid underscores
- File Naming: All lowercase, use underscores for separation, e.g.,
kern_event_handler.go - Interface Naming: Verb + Noun, e.g.,
ProtocolStreamParser - Error Handling: Explicit handling, use
common.DefaultLogfor logging - Logging: Use dedicated loggers like
common.AgentLog,common.BPFLog
// agent/agent.go: SetupAgent()
1. Check BPF permissions (CAP_BPF)
2. Initialize ConnManager
3. Initialize ProcessorManager
4. Load BPF programs (loader.LoadBPF)
5. Start event pulling goroutines
6. Start rendering UI// agent/protocol/protocol.go
// Implement ProtocolStreamParser interface
type ProtocolStreamParser interface {
Match(reqStreams, respStreams) []Record
FindBoundary(streamBuffer, messageType, startPos) int
ParseRequest(streamBuffer, messageType) *ParsedMessage
// ...
}
// Register parser
func init() {
ParsersMap[bpf.AgentTrafficProtocolTKProtocolXXX] = func() ProtocolStreamParser {
return &XXXStreamParser{}
}
}// bpf/pktlatency.bpf.c
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(struct sock_key));
__uint(value_size, sizeof(struct conn_id_s_t));
__uint(max_entries, 65535);
} sock_key_conn_id_map SEC(".maps");agent/
├── agent_test.go # Agent tests
├── agent_utils_test.go # Utility tests
└── protocol/
└── http_test.go # Protocol parser tests
# All tests
go test -v ./...
# Specific package tests
go test -v ./agent/...
# Benchmark tests
go test -bench=. ./...- Check kernel version (requires 3.10.0-957+ or 4.14+)
- Check if BTF is enabled:
zgrep CONFIG_DEBUG_INFO_BTF /proc/config.gz - Use
--btfflag to specify external BTF file
- Ensure access to Docker/Containerd/CRI
- Use
--docker-address,--containerd-addressto specify endpoints
- Check if OpenSSL version is supported
- Ensure the process has ptrace permissions
- Add protocol detection logic in
bpf/protocol_inference.h - Create parser in
agent/protocol/implementingProtocolStreamParser - Add corresponding subcommand in
cmd/ - Add test cases
- Modify
.cor.hfiles - Run
make build-bpfto regenerate skeleton code - Test and verify
- Documentation: https://kyanos.io/
- GitHub: https://github.com/hengyoush/kyanos
- FAQ: https://kyanos.io/faq.html
- eBPF Reference: https://ebpf.io/
- Cilium eBPF: https://github.com/cilium/ebpf
Kyanos development was inspired by the following projects: