From cb54541c5f2570a2e9acba04a2b100f128a764ca Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Fri, 12 Aug 2022 11:42:11 -0400 Subject: [PATCH] Chore: Panic! in the Logs (#53664) (#53669) (cherry picked from commit cc777e175e9042716ab4ad878b0876cbd0a95da7) Co-authored-by: Emil Tullstedt --- pkg/cmd/grafana-server/commands/cli.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/cmd/grafana-server/commands/cli.go b/pkg/cmd/grafana-server/commands/cli.go index bd1ea326adc..4021caab685 100644 --- a/pkg/cmd/grafana-server/commands/cli.go +++ b/pkg/cmd/grafana-server/commands/cli.go @@ -127,6 +127,20 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos } }() + defer func() { + // If we've managed to initialize them, this is the last place + // where we're able to log anything that'll end up in Grafana's + // log files. + // Since operators are not always looking at stderr, we'll try + // to log any and all panics that are about to crash Grafana to + // our regular log locations before exiting. + if r := recover(); r != nil { + reason := fmt.Sprintf("%v", r) + clilog.Error("Critical error", "reason", reason, "stackTrace", string(debug.Stack())) + panic(r) + } + }() + if traceDiagnostics.enabled { fmt.Println("diagnostics: tracing enabled", "file", traceDiagnostics.file) f, err := os.Create(traceDiagnostics.file)