forked from sourcegraph/appdash
/
app.go
329 lines (292 loc) · 8.68 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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 (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
htmpl "html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"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
Aggregator appdash.Aggregator
tmplLock sync.Mutex
tmpls map[string]*htmpl.Template
Log *log.Logger
baseURL *url.URL
}
// New creates a new application handler. If r is nil, a new router is
// created.
//
// The given base URL is the absolute base URL under which traceapp is being
// served, e.g., "https://appdash.mysite.com" or "https://mysite.com/appdash".
// The base URL must contain a scheme and host, or else an error will be
// returned.
func New(r *Router, base *url.URL) (*App, error) {
if r == nil {
r = NewRouter(nil)
}
// Validate the base URL and use the root path if none was specified.
cpy := *base
base = &cpy
if base.Scheme == "" || base.Host == "" {
return nil, fmt.Errorf("appdash: base URL must contain both scheme and host, found %q", base.String())
}
if base.Path == "" {
base.Path = "/"
}
app := &App{
Router: r,
Log: log.New(os.Stderr, "appdash: ", log.LstdFlags),
baseURL: base,
}
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, nil
}
// 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)
if permalink := r.URL.Query().Get("permalink"); permalink != "" {
// If the user specified a permalink, then decode it directly into a
// trace structure and place it into storage for viewing.
gz, err := gzip.NewReader(base64.NewDecoder(base64.RawURLEncoding, strings.NewReader(permalink)))
if err != nil {
return err
}
var upload *appdash.Trace
if err := json.NewDecoder(gz).Decode(&upload); err != nil {
return err
}
if err := a.uploadTraces(upload); err != nil {
return err
}
}
// Look in the store for the trace.
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)
}
// Do not show d3 timeline chart when timeline item fields are invalid.
// So we avoid JS code breaking due missing values.
var showTimelineChart bool = true
visData, err := a.d3timeline(trace)
switch err {
case errTimelineItemValidation:
showTimelineChart = false
case nil:
break
default:
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
}
// The JSON trace is the human-readable trace form for exporting.
jsonTrace, err := json.MarshalIndent([]*appdash.Trace{trace}, "", " ")
if err != nil {
return err
}
// The permalink of the trace is literally the JSON encoded trace gzipped & base64 encoded.
var buf bytes.Buffer
gz := gzip.NewWriter(base64.NewEncoder(base64.RawURLEncoding, &buf))
err = json.NewEncoder(gz).Encode(trace)
if err != nil {
return err
}
if err := gz.Close(); err != nil {
return err
}
permalink, err := a.URLToTrace(trace.ID.Trace)
if err != nil {
return err
}
permalink.RawQuery = "permalink=" + buf.String()
return a.renderTemplate(w, r, "trace.html", http.StatusOK, &struct {
TemplateCommon
Trace *appdash.Trace
ShowTimelineChart bool
VisData []timelineItem
ProfileURL string
Permalink string
JSONTrace string
}{
Trace: trace,
ShowTimelineChart: showTimelineChart,
VisData: visData,
ProfileURL: profile.String(),
Permalink: permalink.String(),
JSONTrace: string(jsonTrace),
})
}
func (a *App) serveTraces(w http.ResponseWriter, r *http.Request) error {
// 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)
}
}
}
traces, err := a.Queryer.Traces(appdash.TracesOpts{
TraceIDs: showJust,
})
if err != nil {
return err
}
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 {
return true
},
})
}
func (a *App) serveAggregate(w http.ResponseWriter, r *http.Request) error {
// By default we display all traces.
traces, err := a.Queryer.Traces(appdash.TracesOpts{})
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
}
return a.uploadTraces(traces...)
}
// uploadTraces uploads literal traces into the storage system for later viewing.
func (a *App) uploadTraces(traces ...*appdash.Trace) error {
// 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
}