Skip to content

Commit

Permalink
feat(app): add profiling support using Pyroscope
Browse files Browse the repository at this point in the history
  • Loading branch information
nullswan committed Sep 17, 2024
1 parent 5c58346 commit 9ac59cd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
49 changes: 49 additions & 0 deletions internal/profile/pyroscope.go
Original file line number Diff line number Diff line change
@@ -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")

Check failure on line 14 in internal/profile/pyroscope.go

View workflow job for this annotation

GitHub Actions / lint

fmt.Errorf can be replaced with errors.New (perfsprint)

Check failure on line 14 in internal/profile/pyroscope.go

View workflow job for this annotation

GitHub Actions / lint

fmt.Errorf can be replaced with errors.New (perfsprint)
}

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{

Check failure on line 27 in internal/profile/pyroscope.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `pyroscope.Start` is not checked (errcheck)

Check failure on line 27 in internal/profile/pyroscope.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `pyroscope.Start` is not checked (errcheck)
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
}

0 comments on commit 9ac59cd

Please sign in to comment.