|
| 1 | +# Profiler Package |
| 2 | + |
| 3 | +The profiler package provides a clean interface for managing Go's pprof server within the frame framework. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Automatic pprof server lifecycle management |
| 8 | +- Configuration-based enable/disable |
| 9 | +- Configurable port binding |
| 10 | +- Graceful shutdown support |
| 11 | +- Thread-safe operations |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Basic Usage |
| 16 | + |
| 17 | +```go |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "github.com/pitabwire/frame/profiler" |
| 21 | + "github.com/pitabwire/frame/config" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + cfg := &config.ConfigurationDefault{ |
| 26 | + ProfilerEnable: true, |
| 27 | + ProfilerPortAddr: ":6060", |
| 28 | + } |
| 29 | + |
| 30 | + server := profiler.NewServer() |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + // Start the profiler if enabled |
| 34 | + err := server.StartIfEnabled(ctx, cfg) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + |
| 39 | + // ... your application logic ... |
| 40 | + |
| 41 | + // Gracefully stop the profiler |
| 42 | + err = server.Stop(ctx) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Integration with Frame Service |
| 50 | + |
| 51 | +The profiler package is automatically integrated into the frame service when using the default configuration: |
| 52 | + |
| 53 | +```go |
| 54 | +import "github.com/pitabwire/frame" |
| 55 | + |
| 56 | +func main() { |
| 57 | + // Set PROFILER_ENABLE=true environment variable |
| 58 | + ctx, svc := frame.NewService() |
| 59 | + |
| 60 | + // Profiler will automatically start on :6060 |
| 61 | + err := svc.Run(ctx, "") |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Configuration |
| 69 | + |
| 70 | +The profiler responds to the following configuration options: |
| 71 | + |
| 72 | +### Environment Variables |
| 73 | + |
| 74 | +- `PROFILER_ENABLE`: Set to "true" to enable the profiler (default: false) |
| 75 | +- `PROFILER_PORT`: Port to bind the profiler server (default: ":6060") |
| 76 | + |
| 77 | +### YAML Configuration |
| 78 | + |
| 79 | +```yaml |
| 80 | +profiler_enable: true |
| 81 | +profiler_port: ":6061" |
| 82 | +``` |
| 83 | +
|
| 84 | +## Available Endpoints |
| 85 | +
|
| 86 | +When the profiler is enabled, the following endpoints are available: |
| 87 | +
|
| 88 | +- `/debug/pprof/` - Index of all available profiles |
| 89 | +- `/debug/pprof/profile` - CPU profile (30 seconds by default) |
| 90 | +- `/debug/pprof/heap` - Heap profile |
| 91 | +- `/debug/pprof/goroutine` - Goroutine profile |
| 92 | +- `/debug/pprof/block` - Blocking profile |
| 93 | +- `/debug/pprof/mutex` - Mutex contention profile |
| 94 | +- `/debug/pprof/trace` - Execution trace |
| 95 | + |
| 96 | +## Command Line Tools |
| 97 | + |
| 98 | +### CPU Profiling |
| 99 | +```bash |
| 100 | +# Collect 30-second CPU profile |
| 101 | +go tool pprof http://localhost:6060/debug/pprof/profile |
| 102 | +
|
| 103 | +# Collect 10-second CPU profile |
| 104 | +go tool pprof http://localhost:6060/debug/pprof/profile?seconds=10 |
| 105 | +``` |
| 106 | + |
| 107 | +### Heap Profiling |
| 108 | +```bash |
| 109 | +go tool pprof http://localhost:6060/debug/pprof/heap |
| 110 | +``` |
| 111 | + |
| 112 | +### Web Interface |
| 113 | +```bash |
| 114 | +go tool pprof -http=:8080 http://localhost:6060/debug/pprof/profile |
| 115 | +``` |
| 116 | + |
| 117 | +## API Reference |
| 118 | + |
| 119 | +### Server |
| 120 | + |
| 121 | +The main type for managing the pprof server. |
| 122 | + |
| 123 | +#### Methods |
| 124 | + |
| 125 | +- `NewServer() *Server` - Creates a new profiler server instance |
| 126 | +- `StartIfEnabled(ctx context.Context, cfg config.ConfigurationProfiler) error` - Starts the profiler if enabled in configuration |
| 127 | +- `Stop(ctx context.Context) error` - Gracefully stops the profiler server |
| 128 | +- `IsRunning() bool` - Returns true if the profiler server is currently running |
| 129 | + |
| 130 | +## Security Considerations |
| 131 | + |
| 132 | +- The profiler should only be enabled in development/staging environments |
| 133 | +- Consider firewall rules to restrict access to profiler endpoints |
| 134 | +- The profiler exposes sensitive information about your application's internals |
| 135 | +- Always disable the profiler in production environments |
| 136 | + |
| 137 | +## Thread Safety |
| 138 | + |
| 139 | +All methods on the Server type are thread-safe and can be called concurrently from multiple goroutines. |
0 commit comments