-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
sentry_integration.go
105 lines (92 loc) · 2.71 KB
/
sentry_integration.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package x
import (
"errors"
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/golang/glog"
"github.com/mitchellh/panicwrap"
)
var env string
// InitSentry intiializes the sentry machinery
func InitSentry(ee bool) {
if ee {
env = "enterprise"
} else {
env = "oss"
}
initSentry()
}
func initSentry() {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://58a035f0d85a4c1c80aee0a3e72f3899@sentry.io/1805390",
Debug: true,
AttachStacktrace: true,
ServerName: WorkerConfig.MyAddr,
Environment: env,
Release: Version(),
}); err != nil {
glog.Fatalf("Sentry init failed: %v", err)
}
}
// FlushSentry flushes the buffered events/errors
func FlushSentry() {
sentry.Flush(time.Second * 2)
}
// ConfigureSentryScope configure the scope on the global hub of Sentry.
func ConfigureSentryScope(subcmd string) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("dgraph", subcmd)
scope.SetLevel(sentry.LevelFatal)
})
}
// PanicWithSentryException sends the error report to Sentry and then panics.
func PanicWithSentryException(err error) {
if err != nil {
CaptureSentryException(err)
panic(err)
}
}
// CaptureSentryException sends the error report to Sentry.
func CaptureSentryException(err error) {
if err != nil {
sentry.CaptureException(err)
}
}
// PanicHandler is the callback function when a panic happens. It does not recover and is
// only used to log panics. (in our case send an event to sentry)
func PanicHandler(out string) {
// output contains the full output (including stack traces) of the
// panic.
sentry.CaptureException(errors.New(out))
FlushSentry() // need to flush asap. Dont defer here.
os.Exit(1)
}
// WrapPanics is a wrapper on panics. We use it to send sentry events about panics
// and crash right after.
func WrapPanics() {
exitStatus, err := panicwrap.BasicWrap(PanicHandler)
if err != nil {
panic(err)
}
// If exitStatus >= 0, then we're the parent process and the panicwrap
// re-executed ourselves and completed. Just exit with the proper status.
if exitStatus >= 0 {
os.Exit(exitStatus)
}
}