-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
415 lines (354 loc) · 9.94 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package bt
import (
"bytes"
cryptorand "crypto/rand"
"encoding/json"
"fmt"
"io"
"log"
mathrand "math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/google/uuid"
)
const (
VersionMajor = 1
VersionMinor = 0
VersionPatch = 0
)
var (
Version = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
windowsGUIDCommand = []string{"reg", "query", "\"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Cryptography\"", "/v", "MachineGuid"}
linuxGUIDCommand = []string{"sh", "-c", "( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :"}
freebsdGUIDCommand = []string{"sh", "-c", "kenv -q smbios.system.uuid || sysctl -n kern.hostuuid"}
darwinGUIDCommand = []string{"sh", "-c", "ioreg -rd1 -c IOPlatformExpertDevice | grep IOPlatformUUID | awk -F'= \"' '{print $2}' | tr -d '\"' | tr -d '\n'"}
windowsCPUCommand = []string{"wmic", "CPU", "get", "NAME"}
linuxCPUCommand = []string{"sh", "-c", "lscpu | grep \"Model name\" | awk -F':' '{print $2}' | sed 's/^[[:space:]]*//'"}
darwinCPUCommand = []string{"sh", "-c", "sysctl -n machdep.cpu.brand_string | tr -d '\n'"}
freebsdCPUCommand = []string{"sh", "-c", "sysctl -n hw.model"}
linuxOSVersionCommand = []string{"sh", "-c", "cat /etc/os-release | grep VERSION= | awk -F'=\"' '{print $2}' | tr -d '\"'"}
darwinOSVersionCommand = []string{"sh", "-c", "sw_vers | grep ProductVersion | awk -F':' '{print $2}' | tr -d '\t' | tr -d '\n'"}
freebsdOSVersionCommand = []string{"sh", "-c", "cat /etc/os-release | grep VERSION= | awk -F'=\"' '{print $2}' | tr -d '\"'"}
)
type OptionsStruct struct {
Endpoint string
Token string
// SendEnvVars gathers and sends all environment variables with every report if true. Default false.
SendEnvVars bool
CaptureAllGoroutines bool
TabWidth int
ContextLineCount int
Attributes map[string]interface{}
DebugBacktrace bool
}
var Options OptionsStruct
var rng *mathrand.Rand
type reportPayload struct {
stack []byte
attributes map[string]interface{}
annotations map[string]interface{}
timestamp int64
classifier string
}
var queue = make(chan interface{}, 50)
var doneChan = make(chan struct{})
var blockChan = make(chan struct{})
func init() {
var err error
var seedBytes [8]byte
_, err = cryptorand.Read(seedBytes[:])
if err != nil {
panic(err)
}
seed :=
(int64(seedBytes[0]) << 0) |
(int64(seedBytes[1]) << 1) |
(int64(seedBytes[2]) << 2) |
(int64(seedBytes[3]) << 3) |
(int64(seedBytes[4]) << 4) |
(int64(seedBytes[5]) << 5) |
(int64(seedBytes[6]) << 6) |
(int64(seedBytes[7]) << 7)
randSource := mathrand.NewSource(seed)
rng = mathrand.New(randSource)
setDefaultAttributes()
go sendWorkerMain()
}
func setDefaultAttributes() {
if Options.Attributes == nil {
Options.Attributes = make(map[string]interface{})
}
hostName, _ := os.Hostname()
Options.Attributes["backtrace.version"] = Version
Options.Attributes["backtrace.agent"] = "backtrace-go"
Options.Attributes["hostname"] = hostName
Options.Attributes["uname.sysname"] = runtime.GOOS
Options.Attributes["cpu.arch"] = runtime.GOARCH
Options.Attributes["process.id"] = os.Getpid()
Options.Attributes["application.session"] = uuid.New()
Options.Attributes["application"] = filepath.Base(os.Args[0])
guiCommand := []string{}
cpuCommand := []string{}
osCommand := []string{}
switch runtime.GOOS {
case "windows":
guiCommand = windowsGUIDCommand
cpuCommand = windowsCPUCommand
case "linux":
guiCommand = linuxGUIDCommand
cpuCommand = linuxCPUCommand
osCommand = linuxOSVersionCommand
case "darwin":
guiCommand = darwinGUIDCommand
cpuCommand = darwinCPUCommand
osCommand = darwinOSVersionCommand
case "freebsd":
guiCommand = freebsdGUIDCommand
cpuCommand = freebsdCPUCommand
osCommand = freebsdOSVersionCommand
}
if len(guiCommand) > 0 {
if output := execCommand(guiCommand); output != "" {
if runtime.GOOS == "windows" {
// windows gives:
// HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography
// MachineGuid REG_SZ {XXXX-XXXX-XXXX-XXXX-XXXX}
if splitOutput := strings.Split(output, "{"); len(splitOutput) > 1 {
output = strings.TrimSuffix(splitOutput[1], "}")
}
}
Options.Attributes["guid"] = output
}
}
if len(cpuCommand) > 0 {
if output := execCommand(cpuCommand); output != "" {
if runtime.GOOS == "windows" {
// windows gives:
//NAME
//Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
if splitOutput := strings.Split(output, "\n"); len(splitOutput) > 1 {
output = splitOutput[1]
}
}
Options.Attributes["cpu.brand"] = output
}
}
if len(osCommand) > 0 {
if output := execCommand(osCommand); output != "" {
Options.Attributes["uname.version"] = output
}
}
}
// first value in array is command to exec, rest are arguments.
// e.g. []string{"sh", "-c", "sysctl -n foo_bar | grep foo_bar | tr -d "foo_var" "}
func execCommand(commands []string) string {
out, err := exec.Command(commands[0], commands[1:]...).Output()
if err != nil {
if Options.DebugBacktrace {
log.Println(err)
}
}
return string(out)
}
func Report(object interface{}, extraAttributes map[string]interface{}) {
if extraAttributes == nil {
extraAttributes = map[string]interface{}{}
}
if extraAttributes["report_type"] == nil {
extraAttributes["report_type"] = "error"
}
switch value := object.(type) {
case nil:
return
case error:
sendReportString(value.Error(), "error", extraAttributes)
default:
sendReportString(fmt.Sprint(value), "message", extraAttributes)
}
}
func sendReportString(msg string, classifier string, extraAttributes map[string]interface{}) {
if !checkOptions() {
return
}
timestamp := time.Now().Unix()
attributes := map[string]interface{}{}
for k, v := range Options.Attributes {
attributes[k] = v
}
attributes["error.message"] = msg
for k, v := range extraAttributes {
attributes[k] = v
}
annotations := map[string]interface{}{}
if Options.SendEnvVars {
annotations["Environment Variables"] = getEnvVars()
}
payload := &reportPayload{
stack: stack(Options.CaptureAllGoroutines),
attributes: attributes,
annotations: annotations,
timestamp: timestamp,
classifier: classifier,
}
queue <- payload
}
func ReportPanic(extraAttributes map[string]interface{}) {
if !checkOptions() {
return
}
err := recover()
if err == nil {
return
}
if extraAttributes == nil {
extraAttributes = map[string]interface{}{}
}
extraAttributes["report_type"] = "panic"
Report(err, extraAttributes)
finishSendingReports(false)
panic(err)
}
func ReportAndRecoverPanic(extraAttributes map[string]interface{}) {
if !checkOptions() {
return
}
if extraAttributes == nil {
extraAttributes = map[string]interface{}{}
}
extraAttributes["report_type"] = "panic"
Report(recover(), extraAttributes)
}
func stack(all bool) []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, all)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}
func getEnvVars() map[string]string {
lines := os.Environ()
result := map[string]string{}
for _, line := range lines {
kv := strings.Split(line, "=")
result[kv[0]] = kv[1]
}
return result
}
func checkOptions() bool {
if len(Options.Endpoint) == 0 {
if !Options.DebugBacktrace {
return false
}
panic("must set bt.Options.Endpoint")
}
if !strings.HasPrefix(Options.Endpoint, "https://submit.backtrace.io") {
if len(Options.Token) == 0 {
if !Options.DebugBacktrace {
return false
}
panic("must set bt.Options.Token")
}
}
return true
}
func createUuid() string {
var uuidBytes [16]byte
_, _ = rng.Read(uuidBytes[:]) // This function is documented to never fail.
return fmt.Sprintf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuidBytes[0], uuidBytes[1], uuidBytes[2], uuidBytes[3],
uuidBytes[4], uuidBytes[5],
uuidBytes[6], uuidBytes[7],
uuidBytes[8], uuidBytes[9],
uuidBytes[10], uuidBytes[11], uuidBytes[12], uuidBytes[13], uuidBytes[14], uuidBytes[15])
}
func sendWorkerMain() {
for {
select {
case queueItem := <-queue:
switch value := queueItem.(type) {
case nil:
doneChan <- struct{}{}
return
case *reportPayload:
processAndSend(value)
default:
panic("invalid queue item")
}
case <-blockChan:
doneChan <- struct{}{}
}
}
}
func FinishSendingReports() {
finishSendingReports(true)
}
func finishSendingReports(kill bool) {
if kill {
queue <- nil
} else {
blockChan <- struct{}{}
}
<-doneChan
}
func processAndSend(payload *reportPayload) {
threads, sourceCode := ParseThreadsFromStack(payload.stack)
report := map[string]interface{}{}
report["uuid"] = createUuid()
report["timestamp"] = payload.timestamp
report["lang"] = "go"
report["langVersion"] = runtime.Version()
report["agent"] = "backtrace-go"
report["agentVersion"] = Version
report["attributes"] = payload.attributes
report["annotations"] = payload.annotations
report["threads"] = threads
report["mainThread"] = "0"
report["sourceCode"] = sourceCode
report["classifiers"] = []string{payload.classifier}
if runtime.GOOS == "linux" {
readMemProcInfo()
}
fullUrl := Options.Endpoint
if len(Options.Token) != 0 { // if token is set that means its old URL.
fullUrl = fmt.Sprintf("%s/post?format=json&token=%s", Options.Endpoint, url.QueryEscape(Options.Token))
}
if Options.DebugBacktrace {
fmt.Fprintf(os.Stderr, "POST %s\n", fullUrl)
var err error
jsonBytes, err := json.MarshalIndent(report, "", " ")
if err != nil {
panic(err)
}
fmt.Fprintf(os.Stderr, "%s\n", string(jsonBytes))
}
jsonBytes, err := json.Marshal(report)
if err != nil {
if Options.DebugBacktrace {
panic(err)
}
return
}
resp, err := http.Post(fullUrl, "application/json", bytes.NewReader(jsonBytes))
if err != nil {
if Options.DebugBacktrace {
panic(err)
}
return
}
defer resp.Body.Close()
if _, err = io.ReadAll(resp.Body); err != nil {
if Options.DebugBacktrace {
panic(err)
}
return
}
}