-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_to_bug_test.go
293 lines (264 loc) · 7.97 KB
/
log_to_bug_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package slogbugsnag
import (
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"sync/atomic"
"testing"
"time"
"github.com/bugsnag/bugsnag-go/v2"
"github.com/bugsnag/bugsnag-go/v2/device"
)
func init() {
device.GetRuntimeVersions() // TODO: fix issue in vendored bugsnag's library
bugsnag.Configure(bugsnag.Configuration{
APIKey: "1234567890abcdef1234567890abcdef", // Should be set by env var, 32 characters
Endpoints: bugsnag.Endpoints{ // Only need to replace in unit tests
Notify: "replaceme",
Sessions: "replaceme",
},
ReleaseStage: "test", // Should be set by env var
AppType: "library", // Should be hardcoded in main (cron/service/job/worker-pool/router/etc)
AppVersion: "0.0.1", // version or date + git-commit-sha? Should be set through build tags or env var
// AutoCaptureSessions: true, // Default
// Hostname: "", // device.GetHostname(), // Default
NotifyReleaseStages: []string{"production", "test"}, // Which stages to notify in (ie: not development). Can be hardcoded. Default nil.
ProjectPackages: []string{"main", "github.com/veqryn/**"}, // Defaults to "main*". Allows SourceRoot trimming and also trims the package prefix as well.
// SourceRoot: "/Users/myuser", // Defaults to GOPATH or GOROOT. Is trimmed off callstack before project packages are trimmed. Should set with env var: BUGSNAG_SOURCE_ROOT
ParamsFilters: []string{"password", "secret", "authorization", "cookie", "access_token", "apiKey"}, // Redaction filter for metadata keys
// PanicHandler: defaultPanicHandler, // Default
// Logger: log.New(os.Stdout, log.Prefix(), log.Flags()), // Default. Should be set in main or hardcoded to slog
// Transport: http.DefaultTransport, // Default
// Synchronous: false, // Default. Should be set on individual calls to notify.
})
}
type nested struct {
Foo string
Bar struct {
Baz string
Qux struct {
Nuu bool
}
}
}
type bugsnagPayload struct {
// ApiKey string `json:"apiKey"`
Events []bugsnagEvent `json:"events"`
// Notifier struct {
// Name string `json:"name"`
// URL string `json:"url"`
// Version string `json:"version"`
// } `json:"notifier"`
}
type bugsnagEvent struct {
// App struct {
// ReleaseStage string `json:"releaseStage"`
// Type string `json:"type"`
// Version string `json:"version"`
// } `json:"app"`
Context string `json:"context"`
// Device struct {
// Hostname string `json:"hostname"`
// OsName string `json:"osName"`
// RuntimeVersions struct {
// Go string `json:"go"`
// } `json:"runtimeVersions"`
// } `json:"device"`
Request struct {
HTTPMethod string `json:"httpMethod"`
URL string `json:"url"`
} `json:"request"`
Exceptions []struct {
ErrorClass string `json:"errorClass"`
Message string `json:"message"`
// Stacktrace []struct {
// Method string `json:"method"`
// File string `json:"file"`
// LineNumber int `json:"lineNumber"`
// InProject bool `json:"inProject,omitempty"`
// } `json:"stacktrace"`
} `json:"exceptions"`
MetaData map[string]map[string]any `json:"metaData"`
// PayloadVersion string `json:"payloadVersion"`
// Session struct {
// StartedAt time.Time `json:"startedAt"`
// ID string `json:"id"`
// Events struct {
// Handled int `json:"handled"`
// Unhandled int `json:"unhandled"`
// } `json:"events"`
// } `json:"session"`
Severity string `json:"severity"`
// SeverityReason struct {
// Type string `json:"type"`
// } `json:"severityReason"`
Unhandled bool `json:"unhandled"`
User struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
} `json:"user"`
}
func TestLogToBug(t *testing.T) {
t.Parallel()
// Set expectation payload
expectation := bugsnagPayload{
Events: []bugsnagEvent{{
Context: "main message",
Request: struct {
HTTPMethod string `json:"httpMethod"`
URL string `json:"url"`
}{
HTTPMethod: "POST",
URL: "http://myserver.com/api/someroute",
},
Exceptions: []struct {
ErrorClass string `json:"errorClass"`
Message string `json:"message"`
}{{
ErrorClass: "*errors.errorString",
Message: "terrible error",
}},
MetaData: map[string]map[string]any{
"log": {
"time": "2023-09-29T13:00:59Z",
"level": "ERROR",
"source": "replaceme",
"msg": "main message",
"err": "terrible error",
"bool": true,
"myid": "67890",
"myuser": map[string]any{
"id": "12345",
},
},
"group1": {
"float": 123.456,
"myname": "john",
"struct": map[string]any{
"Foo": "foo1",
"Bar": map[string]any{
"Baz": "baz1",
"Qux": map[string]any{
"Nuu": false,
},
},
},
},
"group2": {
"dur": "6m0s",
"time": "2023-09-29T13:00:59Z",
"password": "[FILTERED]",
"myemail": "[email protected]",
},
},
Severity: "error",
Unhandled: true,
User: struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}{
ID: "12345",
Name: "john",
Email: "[email protected]",
},
}},
}
// Create a real but temporary server
receivedCall := atomic.Bool{}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fmt.Println("IN TEST SERVER")
if r.Body != nil {
defer r.Body.Close()
b, err := io.ReadAll(r.Body)
if err != nil {
t.Error("Unable to read body:", err)
}
// t.Log("RECEIVED:", string(b))
var payload bugsnagPayload
err = json.Unmarshal(b, &payload)
if err != nil {
t.Error("Unable to unmarshal json to bugsnag payload")
}
// Replace source field since it changes
t.Log(payload.Events[0].MetaData["log"]["source"])
expectation.Events[0].MetaData["log"]["source"] = payload.Events[0].MetaData["log"]["source"]
if !reflect.DeepEqual(payload, expectation) {
t.Errorf("%#+v\n", payload)
}
receivedCall.Store(true)
}
w.WriteHeader(http.StatusOK)
}))
defer svr.Close()
// Set the bugsnag config to send all communication to the test server
notifier := bugsnag.New(bugsnag.Configuration{
Endpoints: bugsnag.Endpoints{
Notify: svr.URL,
Sessions: svr.URL,
},
})
// Temporary handler
h := Handler{
unhandledLevel: slog.LevelError,
notifiers: &NotifierWorkers{notifier: notifier},
}
// Set up the log contents
pc, _, _, _ := runtime.Caller(1)
req, err := http.NewRequest("POST", "http://myserver.com/api/someroute", nil)
if err != nil {
t.Fatal("Unable to create fake http request")
}
ctx := bugsnag.StartSession(context.Background())
ctx = bugsnag.AttachRequestData(ctx, req)
id := ID("67890")
user := bugsnag.User{Id: "12345"}
name := Name("john")
email := Email("[email protected]")
errForBugsnag := errors.New("terrible error")
attrs := []slog.Attr{
slog.Any("err", errForBugsnag),
slog.Bool("bool", true),
slog.Any("myid", id),
slog.Any("myuser", user),
slog.Group("group1",
slog.Float64("float", 123.456),
slog.Any("myname", name),
slog.Any("struct", nested{
Foo: "foo1",
Bar: struct {
Baz string
Qux struct{ Nuu bool }
}{
Baz: "baz1",
Qux: struct{ Nuu bool }{
Nuu: false,
},
},
}),
slog.Group("group2",
slog.Time("time", defaultTime),
slog.Duration("dur", 6*time.Minute),
slog.String("password", "abc123"),
slog.Any("myemail", email),
),
),
}
// Call log to bug
bug := h.logToBug(ctx, defaultTime, slog.LevelError, "main message", pc, attrs)
// Send the bug to our fake bugsnag server to verify the content
err = h.notifiers.notifier.NotifySync(bug.err, true, bug.rawData...)
if err != nil {
t.Error("Unable to notify with bug")
}
if !receivedCall.Load() {
t.Error("Test server did not receive call")
}
}