Skip to content

Commit

Permalink
tetragon: Add pprof http support
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Nov 28, 2022
1 parent c338a63 commit 60e8349
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/tetragon/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (

keyCpuProfile = "cpuprofile"
keyMemProfile = "memprofile"
keyPprofAddr = "pprof-addr"

keyExportFilename = "export-filename"
keyExportFileMaxSizeMB = "export-file-max-size-mb"
Expand Down Expand Up @@ -90,6 +91,7 @@ var (

cpuProfile string
memProfile string
pprofAddr string
)

func readAndSetFlags() {
Expand Down Expand Up @@ -137,6 +139,7 @@ func readAndSetFlags() {

cpuProfile = viper.GetString(keyCpuProfile)
memProfile = viper.GetString(keyMemProfile)
pprofAddr = viper.GetString(keyPprofAddr)

option.Config.EventQueueSize = viper.GetUint(keyEventQueueSize)

Expand Down
23 changes: 23 additions & 0 deletions cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/json"
"fmt"
"net"
"net/http"
pprofhttp "net/http/pprof"
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -162,6 +164,14 @@ func tetragonExecute() error {
bpf.CheckOrMountDebugFS()
bpf.CheckOrMountCgroup2()

if pprofAddr != "" {
go func() {
if err := servePprof(pprofAddr); err != nil {
log.Warnf("serving pprof via http: %v", err)
}
}()
}

// Start profilers first as we have to capture them in signal handling
if memProfile != "" {
log.WithField("file", memProfile).Info("Starting mem profiling")
Expand Down Expand Up @@ -480,6 +490,9 @@ func execute() error {
flags.String(keyMemProfile, "", "Store MEM profile into provided file")
flags.MarkHidden(keyMemProfile)

flags.String(keyPprofAddr, "", "Profile via pprof http")
flags.MarkHidden(keyPprofAddr)

// JSON export aggregation options.
flags.Bool(keyEnableExportAggregation, false, "Enable JSON export aggregation")
flags.Duration(keyExportAggregationWindowSize, 15*time.Second, "JSON export aggregation time window")
Expand Down Expand Up @@ -509,3 +522,13 @@ func execute() error {
viper.BindPFlags(flags)
return rootCmd.Execute()
}

func servePprof(addr string) error {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprofhttp.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprofhttp.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprofhttp.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprofhttp.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprofhttp.Trace)
return http.ListenAndServe(addr, mux)
}

0 comments on commit 60e8349

Please sign in to comment.