FuncTrace is a comprehensive Go library designed for tracking and analyzing function calls in Go applications. Built with Domain-Driven Design (DDD) architecture, it provides detailed insights into function execution patterns, performance metrics, and goroutine lifecycles.
δΈζζζ‘£ / Chinese Documentation
- Decorator Pattern: Automatic function entry/exit tracking with simple decorator syntax
- Call Chain Analysis: Complete parent-child relationship mapping for nested function calls
- Execution Timing: Precise CPU execution time measurement for each function
- Hierarchical Display: Automatic indentation based on call depth for clear visualization
Three flexible parameter storage modes to balance functionality and memory usage:
- Records only function call chains and execution times
- Minimal memory footprint, ideal for production environments
- Best for performance monitoring without detailed debugging
- Captures parameters for regular functions and value receiver methods
- Moderate memory usage, suitable for development environments
- Good balance between debugging capability and resource consumption
- Records all parameters including complex object changes
- Uses JSON Patch technology for incremental storage of pointer receiver changes, greatly reducing redundant data and improving efficiency for large object tracking
- Highest memory usage, ideal for detailed problem analysis
- Includes built-in memory protection mechanisms
- Real-time Tracking: Monitor creation, execution, and termination of goroutines
- Lifecycle Management: Automatic recording of total goroutine execution times
- Background Cleanup: Periodic background tasks to clean up finished goroutine traces
- State Synchronization: Thread-safe goroutine state management
- Main Exit Data Safety: On
main.mainexit, automatically waits for all trace data to be persisted, ensuring data integrity
- Memory Monitor: Automatic memory usage monitoring in
allmode - Threshold Protection: Default 2GB memory limit with emergency exit to prevent OOM
- Smart Alerts: Clear error messages and solution suggestions
- Configurable Limits: Customizable memory thresholds via environment variables
Repository pattern supporting multiple storage backends:
- Three main tables:
TraceData,GoroutineTrace,ParamStoreData - Support for both synchronous and asynchronous insertion modes
- Automatic index creation for optimized query performance
- WAL mode for improved concurrent access
- Mock implementation for testing purposes
- High-speed in-memory operations
- Perfect for unit testing and development
Enhanced spew package with:
- JSON Output: Structured JSON format for complex objects
- Memory Pool Optimization: Object pooling to reduce memory allocation overhead
- Type Safety: Safe handling of all Go data types including unsafe operations
- Circular Reference Detection: Prevention of infinite recursion and stack overflow
- Advanced Type Support: Now supports interface, pointer, and byte array types
- Improved MaxDepth Truncation: Truncation output now includes detailed metadata (
__truncated__,num_fields,length,type) for easier debugging
go get github.com/toheart/functracepackage main
import (
"time"
"github.com/toheart/functrace"
)
func ExampleFunction(name string, count int) {
defer functrace.Trace([]interface{}{name, count})()
// Your function logic here
for i := 0; i < count; i++ {
processItem(name, i)
}
}
func processItem(name string, index int) {
defer functrace.Trace([]interface{}{name, index})()
// Processing logic
time.Sleep(10 * time.Millisecond)
}
func main() {
defer functrace.CloseTraceInstance()
ExampleFunction("test", 3)
}package main
import (
"os"
"github.com/toheart/functrace"
)
func main() {
// Configure parameter storage mode
os.Setenv("FUNCTRACE_PARAM_STORE_MODE", "normal")
// Configure async database operations
os.Setenv("ENV_DB_INSERT_MODE", "async")
// Configure memory limit (2GB)
os.Setenv("FUNCTRACE_MEMORY_LIMIT", "2147483648")
defer functrace.CloseTraceInstance()
// Your application logic
YourApplicationLogic()
}FuncTrace supports configuration through environment variables:
| Environment Variable | Default | Description |
|---|---|---|
FUNCTRACE_PARAM_STORE_MODE |
none |
Parameter storage mode: none/normal/all |
ENV_DB_INSERT_MODE |
sync |
Database insertion mode: sync/async |
FUNCTRACE_MEMORY_LIMIT |
2147483648 |
Memory limit in bytes (2GB default) |
FUNCTRACE_IGNORE_NAMES |
log,context,string |
Comma-separated function name keywords to ignore |
FUNCTRACE_GOROUTINE_MONITOR_INTERVAL |
10 |
Goroutine monitoring interval in seconds |
FUNCTRACE_MAX_DEPTH |
3 |
Maximum tracing depth |
| Mode | Memory Usage | Features | Use Case |
|---|---|---|---|
none |
Minimal | Function call chains + execution times | test monitoring |
normal |
Moderate | Regular function parameters + value methods | Development debugging |
all |
High | All parameters + pointer receiver diffs | Detailed problem analysis |
id: Unique identifiername: Function namegid: Goroutine IDindent: Indentation levelparamsCount: Number of parameterstimeCost: CPU execution timeparentId: Parent function IDcreatedAt: Creation timestampisFinished: Completion statusseq: Sequence number
id: Auto-increment IDoriginGid: Original Goroutine IDtimeCost: CPU execution timecreateTime: Creation timeisFinished: Completion statusinitFuncName: Initial function name
id: Unique identifiertraceId: Associated TraceData IDposition: Parameter positiondata: Parameter JSON dataisReceiver: Whether it's a receiver parameterbaseId: Base parameter ID (for incremental storage)
FuncTrace follows a clean layered architecture:
API Layer (functrace.go)
β
Core Layer (trace package)
β
Domain Layer (domain package)
β
Persistence Layer (persistence package)
- API Layer: Simple external interface (
functrace.go) - Core Layer: Main tracing logic (
trace/) - Domain Layer: Business models and repository interfaces (
domain/) - Persistence Layer: Data storage implementations (
persistence/)
- Object pooling for reduced garbage collection
- Configurable memory limits with automatic protection
- Efficient JSON serialization with incremental storage
- Asynchronous insertion mode for high-throughput scenarios
- Proper indexing for fast queries
- Connection pooling and WAL mode for SQLite
- Thread-safe goroutine state management
- Lock-free atomic operations where possible
- Proper synchronization for shared data structures
- Production Use: Use
noneparameter mode withasyncdatabase mode - Development: Use
normalparameter mode for balanced debugging - Deep Debugging: Use
allparameter mode with memory monitoring - Resource Management: Always call
functrace.CloseTraceInstance()before exit - Selective Tracing: Use ignore patterns to exclude frequently called functions
We welcome contributions! Please see our Contributing Guidelines for details.
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: Wiki
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Built with spew for advanced data serialization
- Uses SQLite for efficient data persistence
- Inspired by various Go profiling and tracing tools
All core features are covered by unit tests (target coverage 80%+). Use go test -cover to check coverage.