-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
121 lines (101 loc) · 3.19 KB
/
entry.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
package goraygun
import (
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
func (e *Entry) populate(err error, st []StackTraceElement, env string) {
e.OccurredOn = time.Now().Format("2006-01-02T15:04:05Z")
hn, _ := os.Hostname()
e.Details.MachineName = hn
e.Details.Client.Name = ClientName
e.Details.Client.Version = ClientVersion
e.Details.Client.ClientUrl = ClientRepo
e.Details.Error.StackTrace = st
e.Details.Error.ClassName = st[0].ClassName
e.Details.Error.Message = err.Error()
e.Details.Context.Identifier = uuid()
e.Details.Tags = []string{env}
return
}
func getMemStats() (m runtime.MemStats) {
runtime.ReadMemStats(&m)
return
}
type Entry struct {
OccurredOn string `json:"occurredOn"`
Details details `json:"details"`
}
type details struct {
MachineName string `json:"machineName"`
Version string `json:"version"`
Client clientDetails `json:"client"`
Error errorDetails `json:"error"`
Context contextDetails `json:"context"`
Environment environmentDetails `json:"environment"`
Request requestDetails `json:"request"`
Tags []string `json:"tags"`
}
type clientDetails struct {
Name string `json:"name"`
Version string `json:"version"`
ClientUrl string `json:"clientUrl"`
}
type errorDetails struct {
Data string `json:"data"`
ClassName string `json:"className"`
Message string `json:"message"`
StackTrace []StackTraceElement `json:"stackTrace"`
}
func uuid() string {
out, _ := exec.Command("uuidgen").Output()
return strings.TrimSpace(string(out))
}
type environmentDetails struct {
ProcessorCount int `json:"processorCount"`
OsVersion string `json:"osVersion"`
Cpu string `json:"cpu"`
PackageVersion string `json:"packageVersion"`
Architecture string `json:"architecture"`
TotalPhysicalMemory uint64 `json:"totalPhysicalMemory"`
AvailablePhysicalMemory uint64 `json:"availablePhysicalMemory"`
Locale string `json:"locale"`
}
func (ed *environmentDetails) populate() {
memstats := getMemStats()
ed.ProcessorCount = runtime.NumCPU()
ed.OsVersion = runtime.GOOS
ed.TotalPhysicalMemory = memstats.Sys
ed.AvailablePhysicalMemory = memstats.Sys - memstats.Alloc
}
type requestDetails struct {
HostName string `json:"hostName"`
Url string `json:"url"`
HttpMethod string `json:"httpMethod"`
IpAddress string `json:"iPAddress"`
Querystring string `json:"querystring"`
Form map[string]string `json:"form"`
Headers map[string]string `json:"headers"`
}
func (rd *requestDetails) Populate(r http.Request) {
rd.HostName = r.Host
rd.Url = r.URL.String()
rd.HttpMethod = r.Method
rd.IpAddress = r.RemoteAddr
rd.Querystring = r.URL.RawQuery
rd.Headers = joinChild(r.Header, ", ")
rd.Form = joinChild(r.Form, ", ")
}
func joinChild(m map[string][]string, sep string) map[string]string {
newMap := make(map[string]string)
for k, v := range m {
newMap[k] = strings.Join(v, sep)
}
return newMap
}
type contextDetails struct {
Identifier string `json:"identifier"`
}