Skip to content

Commit 9765e3f

Browse files
author
parasssh
authored
Send CID for sentry events. (#5625)
1 parent 55e3e9f commit 9765e3f

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

dgraph/cmd/alpha/run.go

+1
Original file line numberDiff line numberDiff line change
@@ -709,5 +709,6 @@ func run() {
709709
adminCloser.SignalAndWait()
710710
glog.Info("Disposing server state.")
711711
worker.State.Dispose()
712+
x.RemoveCidFile()
712713
glog.Infoln("Server shutdown. Bye!")
713714
}

dgraph/cmd/zero/raft.go

+1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ func (n *node) initAndStartNode() error {
508508
err := n.proposeAndWait(context.Background(), &pb.ZeroProposal{Cid: id})
509509
if err == nil {
510510
glog.Infof("CID set for cluster: %v", id)
511+
x.WriteCidFile(id)
511512
break
512513
}
513514
if err == errInvalidProposal {

dgraph/cmd/zero/run.go

+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ func run() {
303303
store.Closer.SignalAndWait()
304304
// Stop all internal requests.
305305
_ = grpcListener.Close()
306+
307+
x.RemoveCidFile()
306308
}()
307309

308310
glog.Infoln("Running Dgraph Zero...")

worker/groups.go

+1
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ START:
811811
}
812812
if i == 0 {
813813
glog.Infof("Received first state update from Zero: %+v", state)
814+
x.WriteCidFile(state.Cid)
814815
}
815816
select {
816817
case stateCh <- state:

x/sentry_integration.go

+45-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package x
1818

1919
import (
2020
"errors"
21+
"io/ioutil"
2122
"os"
2223
"strings"
2324
"time"
@@ -28,8 +29,9 @@ import (
2829
)
2930

3031
var (
31-
env string
32-
dsn string // API KEY to use
32+
env string
33+
dsn string // API KEY to use
34+
cidPath string
3335
)
3436

3537
// Sentry API KEYs to use.
@@ -99,6 +101,41 @@ func ConfigureSentryScope(subcmd string) {
99101
scope.SetTag("dgraph", subcmd)
100102
scope.SetLevel(sentry.LevelFatal)
101103
})
104+
105+
// e.g. /tmp/dgraph-alpha-cid-sentry
106+
cidPath = os.TempDir() + "/" + "dgraph-" + subcmd + "-cid-sentry"
107+
}
108+
109+
// WriteCidFile writes the CID to a well-known location so it can be read and
110+
// sent to Sentry on panic.
111+
func WriteCidFile(cid string) {
112+
if cid == "" {
113+
return
114+
}
115+
if err := ioutil.WriteFile(cidPath, []byte(cid), 0644); err != nil {
116+
glog.Warningf("unable to write CID to file %v %v", cidPath, err)
117+
return
118+
}
119+
}
120+
121+
// readAndRemoveCidFile reads the file from a well-known location so
122+
// it can be read and sent to Sentry on panic.
123+
func readAndRemoveCidFile() string {
124+
cid, err := ioutil.ReadFile(cidPath)
125+
if err != nil {
126+
glog.Warningf("unable to read CID from file %v %v. Skip", cidPath, err)
127+
return ""
128+
}
129+
RemoveCidFile()
130+
return string(cid)
131+
}
132+
133+
// RemoveCidFile removes the file.
134+
func RemoveCidFile() {
135+
if err := os.RemoveAll(cidPath); err != nil {
136+
glog.Warningf("unable to remove the CID file at %v %v. Skip", cidPath, err)
137+
return
138+
}
102139
}
103140

104141
// CaptureSentryException sends the error report to Sentry.
@@ -111,6 +148,12 @@ func CaptureSentryException(err error) {
111148
// PanicHandler is the callback function when a panic happens. It does not recover and is
112149
// only used to log panics (in our case send an event to sentry).
113150
func PanicHandler(out string) {
151+
if cid := readAndRemoveCidFile(); cid != "" {
152+
// re-configure sentry scope to include cid if found.
153+
sentry.ConfigureScope(func(scope *sentry.Scope) {
154+
scope.SetTag("CID", cid)
155+
})
156+
}
114157
// Output contains the full output (including stack traces) of the panic.
115158
sentry.CaptureException(errors.New(out))
116159
FlushSentry() // Need to flush asap. Don't defer here.
@@ -119,7 +162,6 @@ func PanicHandler(out string) {
119162
}
120163

121164
// WrapPanics is a wrapper on panics. We use it to send sentry events about panics
122-
// and crash right after.
123165
func WrapPanics() {
124166
exitStatus, err := panicwrap.BasicWrap(PanicHandler)
125167
if err != nil {

0 commit comments

Comments
 (0)