diff --git a/internal/app/app.go b/internal/app/app.go index cec854a..6208b29 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -31,6 +31,9 @@ func Run() error { var enablePprof bool flag.BoolVar(&enablePprof, "pprof", false, "Enable pprof") + var enableProfiling bool + flag.BoolVar(&enableProfiling, "profiling", false, "Enable profiling") + var prometheusPort uint64 flag.Uint64Var( &prometheusPort, @@ -43,6 +46,14 @@ func Run() error { log := logger.Init() + if enableProfiling { + log.Info("Profiling enabled") + err := profile.SetupProfiling(log) + if err != nil { + return fmt.Errorf("failed to setup profiling: %w", err) + } + } + if kubernetesMode { if !workload.IsSocketPresent() { return errors.New("runtime socket not found") diff --git a/internal/profile/pyroscope.go b/internal/profile/pyroscope.go new file mode 100644 index 0000000..50211e8 --- /dev/null +++ b/internal/profile/pyroscope.go @@ -0,0 +1,49 @@ +package profile + +import ( + "fmt" + "log/slog" + "os" + + "github.com/grafana/pyroscope-go" +) + +func SetupProfiling(log *slog.Logger) error { + serverAddress := os.Getenv("PYROSCOPE_SERVER") + if serverAddress == "" { + return fmt.Errorf("PYROSCOPE_SERVER is not set") + } + + user := os.Getenv("PYROSCOPE_USER") + if user == "" { + log.Warn("PYROSCOPE_USER is not set") + } + + password := os.Getenv("PYROSCOPE_PASSWORD") + if password == "" { + log.Warn("PYROSCOPE_PASSWORD is not set") + } + + pyroscope.Start(pyroscope.Config{ + ApplicationName: "bpfsnitch", + ServerAddress: serverAddress, + BasicAuthUser: user, + BasicAuthPassword: password, + Logger: pyroscope.StandardLogger, + Tags: map[string]string{"hostname": os.Getenv("HOSTNAME")}, + ProfileTypes: []pyroscope.ProfileType{ + pyroscope.ProfileCPU, + pyroscope.ProfileAllocObjects, + pyroscope.ProfileAllocSpace, + pyroscope.ProfileInuseObjects, + pyroscope.ProfileInuseSpace, + pyroscope.ProfileGoroutines, + pyroscope.ProfileMutexCount, + pyroscope.ProfileMutexDuration, + pyroscope.ProfileBlockCount, + pyroscope.ProfileBlockDuration, + }, + }) + + return nil +}