-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathmain.go
156 lines (130 loc) · 3.41 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/getsentry/sentry-go"
)
func prettyPrint(v interface{}) string {
pp, _ := json.MarshalIndent(v, "", " ")
return string(pp)
}
type devNullTransport struct{}
func (t *devNullTransport) Configure(options sentry.ClientOptions) {
dsn, _ := sentry.NewDsn(options.Dsn)
fmt.Println()
fmt.Println("Store Endpoint:", dsn.StoreAPIURL())
fmt.Println("Headers:", dsn.RequestHeaders())
fmt.Println()
}
func (t *devNullTransport) SendEvent(event *sentry.Event) {
fmt.Println("Faked Transport")
}
func (t *devNullTransport) Flush(timeout time.Duration) bool {
return true
}
func recoverHandler() {
defer sentry.Recover()
panic("ups")
}
func beforeSend() {
sentry.CaptureMessage("Drop me!")
}
func captureMessage() {
sentry.CaptureMessage("say what again. SAY WHAT again")
}
func configureScope() {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtra("oristhis", "justfantasy")
scope.SetTag("isthis", "reallife")
scope.SetLevel(sentry.LevelFatal)
scope.SetUser(sentry.User{
ID: "1337",
})
})
}
func withScope() {
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelFatal)
sentry.CaptureException(errors.New("say what again. SAY WHAT again"))
})
}
func addBreadcrumbs() {
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Message: "Random breadcrumb 1",
})
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Message: "Random breadcrumb 2",
})
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Message: "Random breadcrumb 3",
})
}
func withScopeAndConfigureScope() {
sentry.WithScope(func(scope *sentry.Scope) {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtras(map[string]interface{}{
"istillcant": 42,
"believe": "that",
})
scope.SetTags(map[string]string{
"italready": "works",
"just": "likethat",
})
})
event := sentry.NewEvent()
event.Message = "say what again. SAY WHAT again"
sentry.CaptureEvent(event)
})
}
type CustomComplexError struct {
Message string
AnswerToLife int
}
func (e CustomComplexError) Error() string {
return "CustomComplexError: " + e.Message
}
func (e CustomComplexError) GimmeMoreData() string {
return strconv.Itoa(e.AnswerToLife)
}
func eventHint() {
sentry.CaptureException(CustomComplexError{Message: "Captured", AnswerToLife: 42})
}
func main() {
if err := sentry.Init(sentry.ClientOptions{
Debug: true,
Dsn: "https://[email protected]/1337",
IgnoreErrors: []string{"^(?i)drop me"},
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
if ex, ok := hint.OriginalException.(CustomComplexError); ok {
event.Message = event.Message + " - " + ex.GimmeMoreData()
}
fmt.Printf("%s\n\n", prettyPrint(event))
return event
},
BeforeBreadcrumb: func(breadcrumb *sentry.Breadcrumb, _ *sentry.BreadcrumbHint) *sentry.Breadcrumb {
if breadcrumb.Message == "Random breadcrumb 3" {
breadcrumb.Message = "Not so random breadcrumb 3"
}
fmt.Printf("%s\n\n", prettyPrint(breadcrumb))
return breadcrumb
},
SampleRate: 1,
Transport: &devNullTransport{},
Integrations: func(integrations []sentry.Integration) []sentry.Integration {
return append(integrations, integrations[1])
},
}); err != nil {
panic(err)
}
beforeSend()
configureScope()
withScope()
captureMessage()
addBreadcrumbs()
withScopeAndConfigureScope()
recoverHandler()
eventHint()
}