Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SIGUSR2 pprof to agent and proxy #27510

Merged
merged 10 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/27510.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
agent: Add the ability to dump pprof to the filesystem using SIGUSR2
```
44 changes: 44 additions & 0 deletions command/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"runtime/pprof"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -74,6 +76,7 @@ type AgentCommand struct {

ShutdownCh chan struct{}
SighupCh chan struct{}
SighUSR2Ch chan struct{}

tlsReloadFuncsLock sync.RWMutex
tlsReloadFuncs []reloadutil.ReloadFunc
Expand Down Expand Up @@ -758,6 +761,47 @@ func (c *AgentCommand) Run(args []string) int {
case c.reloadedCh <- struct{}{}:
default:
}
case <-c.SighUSR2Ch:
// We can only get pprof outputs via the API but sometimes Vault can get
// into a state where it cannot process requests so we can get pprof outputs
// via SIGUSR2.
pprofPath := filepath.Join(os.TempDir(), "vault-pprof")
err := os.MkdirAll(pprofPath, os.ModePerm)
if err != nil {
c.logger.Error("Could not create temporary directory for pprof", "error", err)
continue
}

dumps := []string{"goroutine", "heap", "allocs", "threadcreate", "profile"}
for _, dump := range dumps {
pFile, err := os.Create(filepath.Join(pprofPath, dump))
if err != nil {
c.logger.Error("error creating pprof file", "name", dump, "error", err)
break
}

if dump != "profile" {
err = pprof.Lookup(dump).WriteTo(pFile, 0)
if err != nil {
c.logger.Error("error generating pprof data", "name", dump, "error", err)
pFile.Close()
break
}
} else {
// CPU profiles need to run for a duration so we're going to run it
// just for one second to avoid blocking here.
if err := pprof.StartCPUProfile(pFile); err != nil {
c.logger.Error("could not start CPU profile: ", err)
pFile.Close()
break
}
time.Sleep(time.Second * 1)
pprof.StopCPUProfile()
}
pFile.Close()
}

c.logger.Info(fmt.Sprintf("Wrote pprof files to: %s", pprofPath))
case <-ctx.Done():
return nil
}
Expand Down
1 change: 1 addition & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) map[string]cli.Co
},
ShutdownCh: MakeShutdownCh(),
SighupCh: MakeSighupCh(),
SighUSR2Ch: MakeSigUSR2Ch(),
}, nil
},
"agent generate-config": func() (cli.Command, error) {
Expand Down
Loading