/
trace.go
158 lines (126 loc) · 2.85 KB
/
trace.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
package trace
import (
"regexp"
"runtime"
)
const (
callStackMaxSize int = 1024
goRoutineRegexp string = `^goroutine (\d*) \[(.*)\]:$`
mKey string = "method"
rKey string = "reference"
idKey string = "id"
statusKey string = "status"
stackKey string = "stack"
goRKey string = "goroutine-"
)
type stacktrace struct {
buf []byte
split [][]byte
stacks []goRoutine
out map[string]interface{}
}
type callStack struct {
method string
reference string
}
type goRoutine struct {
id string
status string
stack []callStack
}
// New function will return the current callstack in a map[string]interface{} format,
// to be added in an event's metadata
func New(all bool) map[string]interface{} {
return newCallStack().
getCallStack(all).
splitCallStack().
parseCallStack().
mapCallStack().
asMap()
}
func newCallStack() *stacktrace {
return &stacktrace{}
}
func (s *stacktrace) getCallStack(all bool) *stacktrace {
buf := make([]byte, callStackMaxSize)
n := runtime.Stack(buf, all)
s.buf = buf[0:n]
return s
}
func (s *stacktrace) splitCallStack() *stacktrace {
if s.buf == nil || len(s.buf) == 0 {
s.getCallStack(false)
}
var line []byte
for i := 0; i < len(s.buf); i++ {
if s.buf[i] != 10 {
line = append(line, s.buf[i])
} else {
s.split = append(s.split, line)
line = []byte{}
}
}
if len(line) != 0 {
s.split = append(s.split, line)
}
return s
}
func (s *stacktrace) parseCallStack() *stacktrace {
if s.split == nil || len(s.split) == 0 {
s.splitCallStack()
}
cs := goRoutine{}
regxGoRoutine := regexp.MustCompile(goRoutineRegexp)
for i := 0; i < len(s.split); i++ {
if regxGoRoutine.Match(s.split[i]) {
if cs.id != "" || cs.status != "" {
s.stacks = append(s.stacks, cs)
cs = goRoutine{}
}
match := regxGoRoutine.FindStringSubmatch(string(s.split[i]))
cs.id = match[1]
cs.status = match[2]
} else {
if i+1 < len(s.split) && len(s.split[i+1]) > 0 && s.split[i+1][0] == 9 {
s := callStack{
method: string(s.split[i]),
reference: string(s.split[i+1][1:]),
}
cs.stack = append(cs.stack, s)
}
}
}
if cs.id != "" || cs.status != "" {
s.stacks = append(s.stacks, cs)
}
return s
}
func (s *stacktrace) mapCallStack() *stacktrace {
if s.stacks == nil || len(s.stacks) == 0 {
s.parseCallStack()
}
field := map[string]interface{}{}
for _, s := range s.stacks {
stackMap := []map[string]interface{}{}
for _, v := range s.stack {
f := map[string]interface{}{
mKey: v.method,
rKey: v.reference,
}
stackMap = append(stackMap, f)
}
field[goRKey+s.id] = map[string]interface{}{
idKey: s.id,
statusKey: s.status,
stackKey: stackMap,
}
}
s.out = field
return s
}
func (s *stacktrace) asMap() map[string]interface{} {
if s.out == nil || len(s.out) <= 0 {
s.mapCallStack()
}
return s.out
}