core/server: Add environment variable to write stacktrace to file (#17929)

* Add env to write stacktrace to file

* changelog

* Use os.MkdirTemp

* Properly close file

* Adding path override

* Use temp file
This commit is contained in:
Jason O'Donnell 2022-11-15 12:03:17 -05:00 committed by GitHub
parent 12a2451479
commit de70878e16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

4
changelog/17929.txt Normal file
View File

@ -0,0 +1,4 @@
```release-note:improvement
core/server: Added an environment variable to write goroutine stacktraces to a
temporary file for SIGUSR2 signals.
```

View File

@ -1760,6 +1760,41 @@ func (c *ServerCommand) Run(args []string) int {
case <-c.SigUSR2Ch:
logWriter := c.logger.StandardWriter(&hclog.StandardLoggerOptions{})
pprof.Lookup("goroutine").WriteTo(logWriter, 2)
if os.Getenv("VAULT_STACKTRACE_WRITE_TO_FILE") != "" {
c.logger.Info("Writing stacktrace to file")
dir := ""
path := os.Getenv("VAULT_STACKTRACE_FILE_PATH")
if path != "" {
if _, err := os.Stat(path); err != nil {
c.logger.Error("Checking stacktrace path failed", "error", err)
continue
}
dir = path
} else {
dir, err = os.MkdirTemp("", "vault-stacktrace")
if err != nil {
c.logger.Error("Could not create temporary directory for stacktrace", "error", err)
continue
}
}
f, err := os.CreateTemp(dir, "stacktrace")
if err != nil {
c.logger.Error("Could not create stacktrace file", "error", err)
continue
}
if err := pprof.Lookup("goroutine").WriteTo(f, 2); err != nil {
f.Close()
c.logger.Error("Could not write stacktrace to file", "error", err)
continue
}
c.logger.Info(fmt.Sprintf("Wrote stacktrace to: %s", f.Name()))
f.Close()
}
}
}
// Notify systemd that the server is shutting down