forked from sourcegraph/appdash
/
app.go
262 lines (228 loc) · 6.61 KB
/
app.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
257
258
259
260
261
262
// Package traceapp implements the Appdash web UI.
//
// The web UI can be effectively launched using the appdash command (see
// cmd/appdash) or via embedding this package within your app.
//
// Templates and other resources needed by this package to render the UI are
// built into the program using vfsgen, so you still get to have single
// binary deployment.
//
// For an example of embedding the Appdash web UI within your own application
// via the traceapp package, see the examples/cmd/webapp example.
package traceapp
import (
"encoding/json"
"errors"
htmpl "html/template"
"io/ioutil"
"net/http"
"net/url"
"path"
"sort"
"strings"
"sync"
"github.com/gorilla/mux"
"sourcegraph.com/sourcegraph/appdash"
static "sourcegraph.com/sourcegraph/appdash-data"
)
// App is an HTTP application handler that also exposes methods for
// constructing URL routes.
type App struct {
*Router
Store appdash.Store
Queryer appdash.Queryer
tmplLock sync.Mutex
tmpls map[string]*htmpl.Template
}
// New creates a new application handler. If r is nil, a new router is
// created.
func New(r *Router) *App {
if r == nil {
r = NewRouter(nil)
}
app := &App{Router: r}
r.r.Get(RootRoute).Handler(handlerFunc(app.serveRoot))
r.r.Get(TraceRoute).Handler(handlerFunc(app.serveTrace))
r.r.Get(TraceSpanRoute).Handler(handlerFunc(app.serveTrace))
r.r.Get(TraceProfileRoute).Handler(handlerFunc(app.serveTrace))
r.r.Get(TraceSpanProfileRoute).Handler(handlerFunc(app.serveTrace))
r.r.Get(TraceUploadRoute).Handler(handlerFunc(app.serveTraceUpload))
r.r.Get(TracesRoute).Handler(handlerFunc(app.serveTraces))
r.r.Get(DashboardRoute).Handler(handlerFunc(app.serveDashboard))
r.r.Get(DashboardDataRoute).Handler(handlerFunc(app.serveDashboardData))
r.r.Get(AggregateRoute).Handler(handlerFunc(app.serveAggregate))
// Static file serving.
r.r.Get(StaticRoute).Handler(http.StripPrefix("/static/", http.FileServer(static.Data)))
return app
}
// ServeHTTP implements http.Handler.
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.Router.r.ServeHTTP(w, r)
}
func (a *App) serveRoot(w http.ResponseWriter, r *http.Request) error {
return a.renderTemplate(w, r, "root.html", http.StatusOK, &struct {
TemplateCommon
}{})
}
func (a *App) serveTrace(w http.ResponseWriter, r *http.Request) error {
v := mux.Vars(r)
traceID, err := appdash.ParseID(v["Trace"])
if err != nil {
return err
}
trace, err := a.Store.Trace(traceID)
if err != nil {
return err
}
// Get sub-span if the Span route var is present.
if spanIDStr := v["Span"]; spanIDStr != "" {
spanID, err := appdash.ParseID(spanIDStr)
if err != nil {
return err
}
trace = trace.FindSpan(spanID)
if trace == nil {
return errors.New("could not find the specified trace span")
}
}
// We could use a separate handler for this, but as we need the above to
// determine the correct trace (or therein sub-trace), we just handle any
// JSON profile requests here.
if path.Base(r.URL.Path) == "profile" {
return a.profile(trace, w)
}
visData, err := a.d3timeline(trace)
if err != nil {
return err
}
// Determine the profile URL.
var profile *url.URL
if trace.ID.Parent == 0 {
profile, err = a.Router.URLToTraceProfile(trace.Span.ID.Trace)
} else {
profile, err = a.Router.URLToTraceSpanProfile(trace.Span.ID.Trace, trace.Span.ID.Span)
}
if err != nil {
return err
}
return a.renderTemplate(w, r, "trace.html", http.StatusOK, &struct {
TemplateCommon
Trace *appdash.Trace
VisData []timelineItem
ProfileURL string
}{
Trace: trace,
VisData: visData,
ProfileURL: profile.String(),
})
}
func (a *App) serveTraces(w http.ResponseWriter, r *http.Request) error {
traces, err := a.Queryer.Traces()
if err != nil {
return err
}
// Parse the query for a comma-separated list of traces that we should only
// show (all others are hidden).
var showJust []appdash.ID
if show := r.URL.Query().Get("show"); len(show) > 0 {
for _, idStr := range strings.Split(show, ",") {
id, err := appdash.ParseID(idStr)
if err == nil {
showJust = append(showJust, id)
}
}
}
// Sort the traces by ID to ensure that the display order doesn't change upon
// multiple page reloads if Queryer.Traces is e.g. backed by a map (which has
// a random iteration order).
sort.Sort(tracesByID(traces))
return a.renderTemplate(w, r, "traces.html", http.StatusOK, &struct {
TemplateCommon
Traces []*appdash.Trace
Visible func(*appdash.Trace) bool
}{
Traces: traces,
Visible: func(t *appdash.Trace) bool {
// Hide the traces that contain aggregate events (that's all they have, so
// they are not very useful to users).
if t.IsAggregate() {
return false
}
if len(showJust) > 0 {
// Showing just a few traces.
for _, id := range showJust {
if id == t.Span.ID.Trace {
return true
}
}
return false
}
return true
},
})
}
func (a *App) serveAggregate(w http.ResponseWriter, r *http.Request) error {
// By default we display all traces.
traces, err := a.Queryer.Traces()
if err != nil {
return err
}
q := r.URL.Query()
// If they specified a comma-separated list of specific trace IDs that they
// are interested in, then we only show those.
selection := q.Get("selection")
if len(selection) > 0 {
var selected []*appdash.Trace
for _, idStr := range strings.Split(selection, ",") {
id, err := appdash.ParseID(idStr)
if err != nil {
return err
}
for _, t := range traces {
if t.Span.ID.Trace == id {
selected = append(selected, t)
}
}
}
traces = selected
}
// Perform the aggregation and render the data.
aggregated, err := a.aggregate(traces, parseAggMode(q.Get("view-mode")))
if err != nil {
return err
}
return a.renderTemplate(w, r, "aggregate.html", http.StatusOK, &struct {
TemplateCommon
Aggregated []*aggItem
}{
Aggregated: aggregated,
})
}
func (a *App) serveTraceUpload(w http.ResponseWriter, r *http.Request) error {
// Read the uploaded JSON trace data.
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
// Unmarshal the trace.
var traces []*appdash.Trace
err = json.Unmarshal(data, &traces)
if err != nil {
return err
}
// Collect the unmarshaled traces, ignoring any previously existing ones (i.e.
// ones that would collide / be merged together).
for _, trace := range traces {
_, err = a.Store.Trace(trace.Span.ID.Trace)
if err != appdash.ErrTraceNotFound {
// The trace collides with an existing trace, ignore it.
continue
}
// Collect the trace (store it for later viewing).
if err = collectTrace(a.Store, trace); err != nil {
return err
}
}
return nil
}