-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual.go
256 lines (222 loc) · 4.85 KB
/
virtual.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
package xo
import (
"path/filepath"
"sort"
"time"
"github.com/getsentry/sentry-go"
"go.opentelemetry.io/otel/sdk/trace"
)
// VEvent is a virtual span event.
type VEvent struct {
Name string
Time time.Time
Attributes M
}
// VSpan is a virtual span.
type VSpan struct {
ID string
Trace string
Parent string
Name string
Start time.Time
End time.Time
Duration time.Duration
Attributes M
Events []VEvent
}
// VNode is a virtual trace node.
type VNode struct {
Span VSpan
Parent *VNode
Children []*VNode
Depth int
}
// VFrame is a virtual exception frame.
type VFrame struct {
Func string
Module string
File string
Path string
Line int
}
// VException is a virtual report exception.
type VException struct {
Type string
Value string
Module string
Frames []VFrame
}
// VReport is a virtual report.
type VReport struct {
ID string
Level string
Time time.Time
Context M
Tags M
Exceptions []VException
}
// ConvertSpan will convert a raw span to a virtual span.
func ConvertSpan(data trace.ReadOnlySpan) VSpan {
// collect events
var events []VEvent
for _, event := range data.Events() {
events = append(events, VEvent{
Name: event.Name,
Time: event.Time,
Attributes: kvToMap(event.Attributes),
})
}
// get parent
parent := data.Parent().SpanID().String()
if !data.Parent().SpanID().IsValid() || data.Parent().IsRemote() {
parent = ""
}
// add span
return VSpan{
ID: data.SpanContext().SpanID().String(),
Trace: data.SpanContext().TraceID().String(),
Parent: parent,
Name: data.Name(),
Start: data.StartTime(),
End: data.EndTime(),
Duration: data.EndTime().Sub(data.StartTime()),
Attributes: kvToMap(data.Attributes()),
Events: events,
}
}
// ConvertReport will convert a raw event to virtual report.
func ConvertReport(event *sentry.Event) VReport {
// prepare report
report := VReport{
ID: string(event.EventID),
Level: string(event.Level),
Time: event.Timestamp,
}
// add context
if len(event.Contexts) > 0 {
report.Context = M{}
for name, ctx := range event.Contexts {
report.Context[name] = ctx
}
}
// add tags
if len(event.Tags) > 0 {
report.Tags = map[string]interface{}{}
for key, value := range event.Tags {
report.Tags[key] = value
}
}
// add exceptions
for _, exc := range event.Exception {
// prepare exception
exception := VException{
Type: exc.Type,
Value: exc.Value,
Module: exc.Module,
}
// add frames
if exc.Stacktrace != nil {
for _, frame := range exc.Stacktrace.Frames {
// get file and path
file := frame.Filename
path := frame.AbsPath
if file == "" && path != "" {
file = filepath.Base(path)
} else if file != "" && path == "" {
path = file
file = filepath.Base(file)
} else {
file = filepath.Base(file)
}
// add frame
exception.Frames = append(exception.Frames, VFrame{
Func: frame.Function,
Module: frame.Module,
File: file,
Path: path,
Line: frame.Lineno,
})
}
}
// add exception
report.Exceptions = append(report.Exceptions, exception)
}
return report
}
// BuildTraces will assemble traces from a list of spans.
func BuildTraces(list []VSpan) []*VNode {
// prepare nodes
var roots []*VNode
nodes := map[string]*VNode{}
for _, span := range list {
// create node
node := &VNode{
Span: span,
}
// add root if no parent
if span.Parent == "" {
roots = append(roots, node)
}
// add node
nodes[span.ID] = node
}
// link nodes
for _, node := range nodes {
if node.Span.Parent != "" {
parent := nodes[node.Span.Parent]
if parent != nil {
node.Parent = parent
parent.Children = append(parent.Children, node)
}
}
}
// sort traces
SortNodes(roots)
// set depth
for _, node := range nodes {
depth := &node.Depth
for node.Parent != nil {
node = node.Parent
*depth++
}
}
return roots
}
// WalkTrace will walk the specified trace.
func WalkTrace(node *VNode, fn func(node *VNode) bool) bool {
// yield node
if !fn(node) {
return false
}
// yield children
for _, child := range node.Children {
if !WalkTrace(child, fn) {
return false
}
}
return true
}
// SortNodes will sort the specified nodes.
func SortNodes(nodes []*VNode) {
// sort children
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Span.Start.Before(nodes[j].Span.Start)
})
// sort children
for _, node := range nodes {
SortNodes(node.Children)
}
}
// VSink provides a virtual string buffer.
type VSink struct {
String string
}
// Write implements the io.Writer interface.
func (s *VSink) Write(p []byte) (n int, err error) {
s.String += string(p)
return len(p), nil
}
// Close implements the io.Closer interface.
func (s *VSink) Close() error {
return nil
}