-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: Catch panic to generate report and reraise #7341
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
595b51e
feat: Catch panic to generate report and reraise
placer14 f79d34b
chore: Separate repo and persistance paths
placer14 3508a5b
chore: Use After hooks and proper args
placer14 ccf24b9
chore: Configurable LOTUS_PANIC_JOURNAL_LOOKBACK
placer14 b4a1290
fix: lint errors
placer14 926858a
fix: persist defaults to repo path; incl version dump
placer14 b3816bc
chore: inline build version within PanicReporter
placer14 574b5c0
chore: Expose build.BuildTypeString()
placer14 c3faadf
fix: Remove debug syscall; Tighten perms; Strip spaces in label
placer14 89d7a72
fix: undo hasty changes
placer14 2f8a2fc
fix: Update lotusminer default paths
placer14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package build | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime/debug" | ||
"runtime/pprof" | ||
"strconv" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/icza/backscanner" | ||
logging "github.com/ipfs/go-log/v2" | ||
) | ||
|
||
var ( | ||
panicLog = logging.Logger("panic-reporter") | ||
defaultJournalTail = 500 | ||
) | ||
|
||
// PanicReportingPath is the name of the subdir created within the repoPath | ||
// path provided to GeneratePanicReport | ||
var PanicReportingPath = "panic-reports" | ||
|
||
// PanicReportJournalTail is the number of lines captured from the end of | ||
// the lotus journal to be included in the panic report. | ||
var PanicReportJournalTail = defaultJournalTail | ||
|
||
// GeneratePanicReport produces a timestamped dump of the application state | ||
// for inspection and debugging purposes. Call this function from any place | ||
// where a panic or severe error needs to be examined. `persistPath` is the | ||
// path where the reports should be saved. `repoPath` is the path where the | ||
// journal should be read from. `label` is an optional string to include | ||
// next to the report timestamp. | ||
func GeneratePanicReport(persistPath, repoPath, label string) { | ||
// make sure we always dump the latest logs on the way out | ||
// especially since we're probably panicking | ||
defer panicLog.Sync() //nolint:errcheck | ||
|
||
if persistPath == "" && repoPath == "" { | ||
panicLog.Warn("missing persist and repo paths, aborting panic report creation") | ||
return | ||
} | ||
|
||
reportPath := filepath.Join(repoPath, PanicReportingPath, generateReportName(label)) | ||
if persistPath != "" { | ||
reportPath = filepath.Join(persistPath, generateReportName(label)) | ||
} | ||
panicLog.Warnf("generating panic report at %s", reportPath) | ||
|
||
tl := os.Getenv("LOTUS_PANIC_JOURNAL_LOOKBACK") | ||
if tl != "" && PanicReportJournalTail == defaultJournalTail { | ||
i, err := strconv.Atoi(tl) | ||
if err == nil { | ||
PanicReportJournalTail = i | ||
} | ||
} | ||
|
||
syscall.Umask(0) | ||
err := os.MkdirAll(reportPath, 0755) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
return | ||
} | ||
|
||
writeAppVersion(filepath.Join(reportPath, "version")) | ||
writeStackTrace(filepath.Join(reportPath, "stacktrace.dump")) | ||
writeProfile("goroutines", filepath.Join(reportPath, "goroutines.pprof.gz")) | ||
writeProfile("heap", filepath.Join(reportPath, "heap.pprof.gz")) | ||
writeJournalTail(PanicReportJournalTail, repoPath, filepath.Join(reportPath, "journal.ndjson")) | ||
placer14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func writeAppVersion(file string) { | ||
f, err := os.Create(file) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
} | ||
defer f.Close() //nolint:errcheck | ||
|
||
versionString := []byte(BuildVersion + BuildTypeString() + CurrentCommit + "\n") | ||
if _, err := f.Write(versionString); err != nil { | ||
panicLog.Error(err.Error()) | ||
} | ||
} | ||
|
||
func writeStackTrace(file string) { | ||
f, err := os.Create(file) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
} | ||
defer f.Close() //nolint:errcheck | ||
|
||
if _, err := f.Write(debug.Stack()); err != nil { | ||
panicLog.Error(err.Error()) | ||
} | ||
|
||
} | ||
|
||
func writeProfile(profileType string, file string) { | ||
p := pprof.Lookup(profileType) | ||
if p == nil { | ||
panicLog.Warnf("%s profile not available", profileType) | ||
return | ||
} | ||
f, err := os.Create(file) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
return | ||
} | ||
defer f.Close() //nolint:errcheck | ||
|
||
if err := p.WriteTo(f, 0); err != nil { | ||
panicLog.Error(err.Error()) | ||
} | ||
} | ||
|
||
func writeJournalTail(tailLen int, repoPath, file string) { | ||
if repoPath == "" { | ||
panicLog.Warn("repo path is empty, aborting copy of journal log") | ||
return | ||
} | ||
|
||
f, err := os.Create(file) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
return | ||
} | ||
defer f.Close() //nolint:errcheck | ||
|
||
jPath, err := getLatestJournalFilePath(repoPath) | ||
if err != nil { | ||
panicLog.Warnf("failed getting latest journal: %s", err.Error()) | ||
return | ||
} | ||
j, err := os.OpenFile(jPath, os.O_RDONLY, 0400) | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
return | ||
} | ||
js, err := j.Stat() | ||
if err != nil { | ||
panicLog.Error(err.Error()) | ||
return | ||
} | ||
jScan := backscanner.New(j, int(js.Size())) | ||
linesWritten := 0 | ||
for { | ||
if linesWritten > tailLen { | ||
break | ||
} | ||
line, _, err := jScan.LineBytes() | ||
if err != nil { | ||
if err != io.EOF { | ||
panicLog.Error(err.Error()) | ||
} | ||
break | ||
} | ||
if _, err := f.Write(line); err != nil { | ||
panicLog.Error(err.Error()) | ||
break | ||
} | ||
if _, err := f.Write([]byte("\n")); err != nil { | ||
panicLog.Error(err.Error()) | ||
break | ||
} | ||
linesWritten++ | ||
} | ||
} | ||
|
||
func getLatestJournalFilePath(repoPath string) (string, error) { | ||
journalPath := filepath.Join(repoPath, "journal") | ||
entries, err := os.ReadDir(journalPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(journalPath, entries[len(entries)-1].Name()), nil | ||
} | ||
|
||
func generateReportName(label string) string { | ||
return fmt.Sprintf("report_%s_%s", label, time.Now().Format("2006-01-02T150405Z0700")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(If yes, would add a comment explaining why)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was debugging something and accidentally committed it. Removed.