forked from evalphobia/logrus_sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry_test.go
234 lines (202 loc) · 5.74 KB
/
sentry_test.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package logrus_sentry
import (
"compress/zlib"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/Sirupsen/logrus"
"github.com/getsentry/raven-go"
)
const (
message = "error message"
server_name = "testserver.internal"
logger_name = "test.logger"
)
func getTestLogger() *logrus.Logger {
l := logrus.New()
l.Out = ioutil.Discard
return l
}
// raven.Packet does not have a json directive for deserializing stacktrace
// so need to explicitly construct one for purpose of test
type resultPacket struct {
raven.Packet
Stacktrace raven.Stacktrace `json:"stacktrace"`
}
func WithTestDSN(t *testing.T, tf func(string, <-chan *resultPacket)) {
pch := make(chan *resultPacket, 1)
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
contentType := req.Header.Get("Content-Type")
var bodyReader io.Reader = req.Body
// underlying client will compress and encode payload above certain size
if contentType == "application/octet-stream" {
bodyReader = base64.NewDecoder(base64.StdEncoding, bodyReader)
bodyReader, _ = zlib.NewReader(bodyReader)
}
d := json.NewDecoder(bodyReader)
p := &resultPacket{}
err := d.Decode(p)
if err != nil {
t.Fatal(err.Error())
}
pch <- p
}))
defer s.Close()
fragments := strings.SplitN(s.URL, "://", 2)
dsn := fmt.Sprintf(
"%s://public:secret@%s/sentry/project-id",
fragments[0],
fragments[1],
)
tf(dsn, pch)
}
func TestSpecialFields(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
req, _ := http.NewRequest("GET", "url", nil)
logger.WithFields(logrus.Fields{
"server_name": server_name,
"logger": logger_name,
"http_request": req,
}).Error(message)
packet := <-pch
if packet.Logger != logger_name {
t.Errorf("logger should have been %s, was %s", logger_name, packet.Logger)
}
if packet.ServerName != server_name {
t.Errorf("server_name should have been %s, was %s", server_name, packet.ServerName)
}
})
}
func TestSentryHandler(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
if packet.Message != message {
t.Errorf("message should have been %s, was %s", message, packet.Message)
}
})
}
func TestSentryWithClient(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
client, _ := raven.New(dsn)
hook, err := NewWithClientSentryHook(client, []logrus.Level{
logrus.ErrorLevel,
})
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
if packet.Message != message {
t.Errorf("message should have been %s, was %s", message, packet.Message)
}
})
}
func TestSentryTags(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
tags := map[string]string{
"site": "test",
}
levels := []logrus.Level{
logrus.ErrorLevel,
}
hook, err := NewWithTagsSentryHook(dsn, tags, levels)
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
expected := raven.Tags{
raven.Tag{
Key: "site",
Value: "test",
},
}
if !reflect.DeepEqual(packet.Tags, expected) {
t.Errorf("message should have been %s, was %s", message, packet.Message)
}
})
}
func TestSentryStacktrace(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
logrus.InfoLevel,
})
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
logger.Error(message)
packet := <-pch
stacktraceSize := len(packet.Stacktrace.Frames)
if stacktraceSize != 0 {
t.Error("Stacktrace should be empty as it is not enabled")
}
hook.StacktraceConfiguration.Enable = true
logger.Error(message) // this is the call that the last frame of stacktrace should capture
expectedLineno := 195 //this should be the line number of the previous line
packet = <-pch
stacktraceSize = len(packet.Stacktrace.Frames)
if stacktraceSize == 0 {
t.Error("Stacktrace should not be empty")
}
lastFrame := packet.Stacktrace.Frames[stacktraceSize-1]
expectedSuffix := "logrus_sentry/sentry_test.go"
if !strings.HasSuffix(lastFrame.Filename, expectedSuffix) {
t.Errorf("File name should have ended with %s, was %s", expectedSuffix, lastFrame.Filename)
}
if lastFrame.Lineno != expectedLineno {
t.Errorf("Line number should have been %d, was %d", expectedLineno, lastFrame.Lineno)
}
if lastFrame.InApp {
t.Error("Frame should not be identified as in_app without prefixes")
}
hook.StacktraceConfiguration.InAppPrefixes = []string{"github.com/Sirupsen/logrus"}
hook.StacktraceConfiguration.Context = 2
hook.StacktraceConfiguration.Skip = 2
logger.Error(message)
packet = <-pch
stacktraceSize = len(packet.Stacktrace.Frames)
if stacktraceSize == 0 {
t.Error("Stacktrace should not be empty")
}
lastFrame = packet.Stacktrace.Frames[stacktraceSize-1]
expectedFilename := "github.com/Sirupsen/logrus/entry.go"
if lastFrame.Filename != expectedFilename {
t.Errorf("File name should have been %s, was %s", expectedFilename, lastFrame.Filename)
}
if !lastFrame.InApp {
t.Error("Frame should be identified as in_app")
}
})
}