forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
41 lines (38 loc) · 1.12 KB
/
panic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package serviceability
import (
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)
// BehaviorOnPanic is a helper for setting the crash mode of OpenShift when a panic is caught.
// It returns a function that should be the defer handler for the caller.
func BehaviorOnPanic(mode string) (fn func()) {
fn = func() {}
switch {
case mode == "crash":
glog.Infof("OpenShift will terminate as soon as a panic occurs.")
util.ReallyCrash = true
case strings.HasPrefix(mode, "sentry:"):
url := strings.TrimPrefix(mode, "sentry:")
m, err := NewSentryMonitor(url)
if err != nil {
glog.Errorf("Unable to start Sentry for panic tracing: %v", err)
return
}
glog.Infof("OpenShift will log all panics and errors to Sentry.")
util.PanicHandlers = append(util.PanicHandlers, m.CapturePanic)
util.ErrorHandlers = append(util.ErrorHandlers, m.CaptureError)
fn = func() {
if r := recover(); r != nil {
m.CapturePanicAndWait(r, 2*time.Second)
panic(r)
}
}
case len(mode) == 0:
// default panic behavior
default:
glog.Errorf("Unrecognized panic behavior")
}
return
}