forked from sourcegraph/appdash
/
vis.go
140 lines (124 loc) · 3.42 KB
/
vis.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
package traceapp
import (
"errors"
"fmt"
"net/url"
"time"
"sourcegraph.com/sourcegraph/appdash"
// Unmarshaling of events depends on the fact that they are registered with
// Appdash.
_ "sourcegraph.com/sourcegraph/appdash/httptrace"
_ "sourcegraph.com/sourcegraph/appdash/sqltrace"
)
var ErrTimelineItemValidation = errors.New("timeline item validation error")
type timelineItem struct {
Label string `json:"label"`
FullLabel string `json:"fullLabel"`
Times []*timelineItemTimespan `json:"times"`
Data map[string]string `json:"rawData"`
SpanID string `json:"spanID"`
ParentSpanID string `json:"parentSpanID"`
URL string `json:"url"`
Visible bool `json:"visible"`
}
func (tl *timelineItem) Valid() bool {
// d3 timeline chart depends on
// item.Label & item.FullLabel
if tl.Label == "" || tl.FullLabel == "" {
return false
}
return true
}
type timelineItemTimespan struct {
Label string `json:"label"`
Start int64 `json:"starting_time"` // msec since epoch
End int64 `json:"ending_time"` // msec since epoch
Duration int64 `json:"duration"`
}
func (a *App) d3timeline(t *appdash.Trace) ([]timelineItem, error) {
return a.d3timelineInner(t, 0)
}
func (a *App) d3timelineInner(t *appdash.Trace, depth int) ([]timelineItem, error) {
var items []timelineItem
var events []appdash.Event
if err := appdash.UnmarshalEvents(t.Span.Annotations, &events); err != nil {
return nil, err
}
var u *url.URL
if t.ID.Parent == 0 {
var err error
u, err = a.URLToTrace(t.ID.Trace)
if err != nil {
return nil, err
}
} else {
var err error
u, err = a.URLToTraceSpan(t.ID.Trace, t.ID.Span)
if err != nil {
return nil, err
}
}
item := timelineItem{
Label: t.Span.Name(),
FullLabel: t.Span.Name(),
Data: t.Annotations.StringMap(),
SpanID: t.Span.ID.Span.String(),
URL: u.String(),
}
if !item.Valid() {
return nil, ErrTimelineItemValidation
}
if t.Span.ID.Parent != 0 {
item.ParentSpanID = t.Span.ID.Parent.String()
}
if depth <= 1 {
item.Visible = true
}
for _, e := range events {
if e, ok := e.(appdash.TimespanEvent); ok {
start := e.Start().UnixNano() / int64(time.Millisecond)
end := e.End().UnixNano() / int64(time.Millisecond)
ts := timelineItemTimespan{
Start: start,
End: end,
}
if t.Span.ID.Parent == 0 {
ts.Label = e.Schema()
item.Times = append(item.Times, &ts)
} else {
if item.Times == nil {
item.Times = append(item.Times, &ts)
} else {
if item.Times[0].Start > start {
item.Times[0].Start = start
}
if item.Times[0].End < end {
item.Times[0].End = end
}
}
}
}
}
for _, ts := range item.Times {
msec := time.Duration(item.Times[0].End-item.Times[0].Start) * time.Millisecond
if msec > 0 {
ts.Label = fmt.Sprintf("%s (%s)", item.Label, msec)
ts.Duration = int64(msec)
}
}
if len(item.Times) == 0 {
// Items with a null times array will crash d3-timeline.js as it tries
// to iterate over it. This means the trace doesn't have a single
// TimespanEvent and is thus invalid.
return nil, nil
}
items = append(items, item)
for _, child := range t.Sub {
subItems, err := a.d3timelineInner(child, depth+1)
if err != nil {
return nil, err
}
items = append(items, subItems...)
}
return items, nil
}