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

command/server: remove env var requirement for sigusr2 pprof output #25391

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions changelog/25391.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:improvement
command/server: Removed environment variable requirement to generate pprof
files using SIGUSR2. Added CPU profile support.
```
53 changes: 27 additions & 26 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1760,42 +1760,43 @@ func (c *ServerCommand) Run(args []string) int {
// 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.
if os.Getenv("VAULT_PPROF_WRITE_TO_FILE") != "" {
dir := ""
path := os.Getenv("VAULT_PPROF_FILE_PATH")
if path != "" {
if _, err := os.Stat(path); err != nil {
c.logger.Error("Checking pprof path failed", "error", err)
continue
}
dir = path
} else {
dir, err = os.MkdirTemp("", "vault-pprof")
if err != nil {
c.logger.Error("Could not create temporary directory for pprof", "error", err)
continue
}
}
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"}
for _, dump := range dumps {
pFile, err := os.Create(filepath.Join(dir, dump))
if err != nil {
c.logger.Error("error creating pprof file", "name", dump, "error", err)
break
}
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
}
pFile.Close()
} 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()
}

c.logger.Info(fmt.Sprintf("Wrote pprof files to: %s", dir))
pFile.Close()
}

c.logger.Info(fmt.Sprintf("Wrote pprof files to: %s", pprofPath))
}
}
// Notify systemd that the server is shutting down
Expand Down
Loading