-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentry_writer_test.go
153 lines (122 loc) · 3.89 KB
/
sentry_writer_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
package sentrywriter_test
import (
"sync"
"testing"
"time"
"github.com/getsentry/sentry-go"
"github.com/mec07/sentrywriter"
"gotest.tools/assert"
)
type mockClient struct {
sync.Mutex
messages []string
}
func (m *mockClient) Flush(timeout time.Duration) bool {
return true
}
func (m *mockClient) CaptureMessage(message string, hint *sentry.EventHint, scope sentry.EventModifier) *sentry.EventID {
m.Lock()
defer m.Unlock()
m.messages = append(m.messages, message)
return &sentry.NewEvent().EventID
}
func (m *mockClient) getMessages() []string {
m.Lock()
defer m.Unlock()
messages := make([]string, len(m.messages))
copy(messages, m.messages)
return messages
}
func TestSentryWriterWrite(t *testing.T) {
client := &mockClient{}
writer := sentrywriter.New(sentrywriter.LogLevel{"fatal", sentry.LevelFatal}).WithClient(client).WithUserID("userID").
WithLogLevel(sentrywriter.LogLevel{"error", sentry.LevelError}).WithBreadcrumbs(20)
log := `{"level":"error","message":"blah"}`
n, err := writer.Write([]byte(log))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(log), n)
messages := client.getMessages()
if len(messages) != 1 {
t.Fatalf("Expected 1 message, found: %d", len(messages))
}
assert.Equal(t, log, messages[0])
}
func TestSentryWriterWriteFiltersLogs(t *testing.T) {
client := &mockClient{}
writer := sentrywriter.New(sentrywriter.LogLevel{"fatal", sentry.LevelFatal}).WithClient(client).
WithLogLevel(sentrywriter.LogLevel{"error", sentry.LevelError})
log := `{"level":"info","message":"blah"}`
n, err := writer.Write([]byte(log))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(log), n)
messages := client.getMessages()
if len(messages) != 0 {
t.Fatalf("Expected 0 message, found: %d", len(messages))
}
}
// Although there isn't really a helpful observation to make about breadcrumbs
// we can still ensure nothing goes terribly wrong by logging without
// breadcrumbs turned on, then with breadcrumbs turned on.
func TestSentryWriterBreadcrumbsPaths(t *testing.T) {
// Add a log before turning on breadcrumbs
client := &mockClient{}
writer := sentrywriter.New(sentrywriter.LogLevel{"fatal", sentry.LevelFatal}).WithClient(client).
WithLogLevel(sentrywriter.LogLevel{"error", sentry.LevelError})
log := `{"level":"info","message":"blah"}`
n, err := writer.Write([]byte(log))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(log), n)
// Add a log after turning on breadcrumbs
writer.WithBreadcrumbs(20)
n, err = writer.Write([]byte(log))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(log), n)
// Add final log which should send event with breadcrumb included
errorLog := `{"level":"error","message":"blah"}`
n, err = writer.Write([]byte(errorLog))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(errorLog), n)
messages := client.getMessages()
if len(messages) != 1 {
t.Fatalf("Expected 1 message, found: %d", len(messages))
}
assert.Equal(t, errorLog, messages[0])
}
func TestSentryWriterNonJSONError(t *testing.T) {
client := &mockClient{}
writer := sentrywriter.New(sentrywriter.LogLevel{"error", sentry.LevelError}).WithClient(client).
WithBreadcrumbs(20)
log := `invalid json`
_, err := writer.Write([]byte(log))
if err == nil {
t.Fatal("expected an error")
}
}
func TestSentryWriterNoFilterByDefault(t *testing.T) {
// Do not add any filters
client := &mockClient{}
writer := sentrywriter.New().WithClient(client)
log := `just a random log which isn't json formatted`
// the non-json log can get through fine
n, err := writer.Write([]byte(log))
if err != nil {
t.Fatalf("writer.Write: %v", err)
}
assert.Equal(t, len(log), n)
// Now add filters
writer = writer.WithLogLevel(sentrywriter.LogLevel{"error", sentry.LevelError})
_, err = writer.Write([]byte(log))
if err == nil {
t.Fatal("expected an error")
}
}