forked from sourcegraph/appdash
/
trace.go
138 lines (127 loc) · 3.13 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
package appdash
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
)
// A Trace is a tree of spans.
type Trace struct {
Span // Root span
Sub []*Trace // Children
}
// String returns the Trace as a formatted string.
func (t *Trace) String() string {
b, err := json.MarshalIndent(t, "", " ")
if err != nil {
panic(err)
}
return string(b)
}
// FindSpan recursively searches for a span whose Span ID is spanID in
// t and its descendants. If no such span is found, nil is returned.
func (t *Trace) FindSpan(spanID ID) *Trace {
if t.ID.Span == spanID {
return t
}
for _, sub := range t.Sub {
if s := sub.FindSpan(spanID); s != nil {
return s
}
}
return nil
}
// TreeString returns the Trace as a formatted string that visually
// represents the trace's tree.
func (t *Trace) TreeString() string {
var buf bytes.Buffer
t.treeString(&buf, 0)
return buf.String()
}
// IsAggregate tells if the trace contains any AggregateEvents (it is therefor
// said to be a set of aggregated traces).
func (t *Trace) IsAggregate() bool {
aggSchema := schemaPrefix + AggregateEvent{}.Schema()
var walk func(t *Trace) bool
walk = func(t *Trace) bool {
for _, ann := range t.Annotations {
if ann.Key == aggSchema {
return true
}
}
for _, sub := range t.Sub {
if walk(sub) {
return true
}
}
return false
}
return walk(t)
}
// Aggregated returns the aggregate event (or nil if none is found) along with
// all of the TimespanEvents found in this trace.
func (t *Trace) Aggregated() (*AggregateEvent, []TimespanEvent, error) {
var (
agg *AggregateEvent
timespans []TimespanEvent
walk func(t *Trace) error
)
walk = func(t *Trace) error {
var evs []Event
err := UnmarshalEvents(t.Annotations, &evs)
if err != nil {
return err
}
for _, ev := range evs {
if a, ok := ev.(AggregateEvent); ok {
agg = &a
} else if t, ok := ev.(TimespanEvent); ok {
timespans = append(timespans, t)
}
}
for _, sub := range t.Sub {
if err := walk(sub); err != nil {
return err
}
}
return nil
}
if err := walk(t); err != nil {
return nil, nil, err
}
return agg, timespans, nil
}
func (t *Trace) treeString(w io.Writer, depth int) {
const indent1 = " "
indent := strings.Repeat(indent1, depth)
if depth == 0 {
fmt.Fprintf(w, "+ Trace %x\n", uint64(t.Span.ID.Trace))
} else {
if depth == 1 {
fmt.Fprint(w, "|")
} else {
fmt.Fprint(w, "|", indent[len(indent1):])
}
fmt.Fprintf(w, "%s+ Span %x", strings.Repeat("-", len(indent1)), uint64(t.Span.ID.Span))
if t.Span.ID.Parent != 0 {
fmt.Fprintf(w, " (parent %x)", uint64(t.Span.ID.Parent))
}
fmt.Fprintln(w)
}
for _, a := range t.Span.Annotations {
if depth == 0 {
fmt.Fprint(w, "| ")
} else {
fmt.Fprint(w, "|", indent[1:], " | ")
}
fmt.Fprintf(w, "%s = %s\n", a.Key, a.Value)
}
for _, sub := range t.Sub {
sub.treeString(w, depth+1)
}
}
type tracesByIDSpan []*Trace
func (t tracesByIDSpan) Len() int { return len(t) }
func (t tracesByIDSpan) Less(i, j int) bool { return t[i].Span.ID.Span < t[j].Span.ID.Span }
func (t tracesByIDSpan) Swap(i, j int) { t[i], t[j] = t[j], t[i] }