-
Notifications
You must be signed in to change notification settings - Fork 240
/
sentry.go
136 lines (106 loc) · 2.46 KB
/
sentry.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Package sentry implements sentry-related functionality.
package sentry
import (
"bytes"
"fmt"
"os"
"runtime/debug"
"time"
"github.com/getsentry/sentry-go"
"github.com/logrusorgru/aurora"
"github.com/superfly/flyctl/internal/buildinfo"
)
var initError error // set during init
func init() {
opts := sentry.ClientOptions{
Dsn: "https://89fa584dc19b47a6952dd94bf72dbab4@sentry.io/4492967",
// TODO: maybe set Debug to buildinfo.IsDev?
// Debug: true,
Environment: buildinfo.Environment(),
Release: buildinfo.Version().String(),
Transport: &sentry.HTTPSyncTransport{
Timeout: 3 * time.Second,
},
BeforeSend: func(event *sentry.Event, _ *sentry.EventHint) *sentry.Event {
if buildinfo.IsDev() {
return nil
}
return event
},
}
initError = sentry.Init(opts)
}
type CaptureOption func(scope *sentry.Scope)
func WithExtra(key string, val interface{}) CaptureOption {
return func(scope *sentry.Scope) {
scope.SetExtra(key, val)
}
}
func WithContext(key string, val interface{}) CaptureOption {
return func(scope *sentry.Scope) {
scope.SetContext(key, val)
}
}
func WithContexts(contexts map[string]interface{}) CaptureOption {
return func(scope *sentry.Scope) {
scope.SetContexts(contexts)
}
}
func WithTag(key, value string) CaptureOption {
return func(scope *sentry.Scope) {
scope.SetTag(key, value)
}
}
func CaptureException(err error, opts ...CaptureOption) {
if !isInitialized() {
return
}
sentry.WithScope(func(s *sentry.Scope) {
for _, opt := range opts {
opt(s)
}
_ = sentry.CaptureException(err)
})
}
func CaptureMessage(msg string, opts ...CaptureOption) {
if !isInitialized() {
return
}
sentry.WithScope(func(s *sentry.Scope) {
for _, opt := range opts {
opt(s)
}
_ = sentry.CaptureMessage(msg)
})
}
// Recover records the given panic to sentry.
func Recover(v interface{}) {
if !isInitialized() {
return
}
_ = sentry.CurrentHub().Recover(v)
printError(v)
}
func printError(v interface{}) {
var buf bytes.Buffer
fmt.Fprintln(&buf, aurora.Red("Oops, something went wrong! Could you try that again?"))
if buildinfo.IsDev() {
fmt.Fprintln(&buf)
fmt.Fprintln(&buf, v)
fmt.Fprintln(&buf, string(debug.Stack()))
}
buf.WriteTo(os.Stdout)
}
func isInitialized() bool {
if initError != nil {
fmt.Fprintf(os.Stderr, "sentry.Init: %v\n", initError)
return false
}
return true
}
func Flush() {
if !isInitialized() {
return
}
_ = sentry.Flush(time.Second << 1)
}