This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
482 lines (400 loc) · 11.3 KB
/
handler.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package pprofserver
import (
"bytes"
"context"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"os/exec"
"path"
"sort"
"strings"
"github.com/segmentio/events"
"github.com/segmentio/events/log"
"github.com/segmentio/objconv/json"
)
type Handler struct {
Prefix string
Registry Registry
Client *http.Client
}
func (h *Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
header := res.Header()
header.Set("Content-Language", "en")
header.Set("Server", "pprof-server")
switch req.Method {
case http.MethodGet, http.MethodHead:
default:
http.Error(res, "only GET and HEAD are allowed", http.StatusMethodNotAllowed)
return
}
switch path := req.URL.Path; {
case path == "/", path == "/services":
if h.Registry != nil && h.Registry.String() == "kubernetes" {
h.serveRedirect(res, req, "/pods/")
} else {
h.serveRedirect(res, req, "/services/")
}
case path == "/tree":
h.serveTree(res, req)
case path == "/flame":
h.serveFlame(res, req)
case path == "/services/":
h.serveListServices(res, req)
case strings.HasPrefix(path, "/services/"):
h.serveListTasks(res, req)
case strings.HasPrefix(path, "/service/"):
h.serveLookupService(res, req)
case path == "/pods/":
h.serveListPods(res, req)
case strings.HasPrefix(path, "/pods/"):
// We currently expose all the PODs in one page. To make it more scalable, we plan
// to implement a tree of pages per type of Kubernetes resource (sts, deployment, ...).
h.serveListContainers(res, req)
case strings.HasPrefix(path, "/pod/"):
h.serveLookupContainer(res, req)
default:
h.serveNotFound(res, req)
}
}
func (h *Handler) serveRedirect(res http.ResponseWriter, req *http.Request, url string) {
http.Redirect(res, req, url, http.StatusFound)
}
func (h *Handler) serveNotFound(res http.ResponseWriter, req *http.Request) {
http.NotFound(res, req)
}
func (h *Handler) serveListServices(res http.ResponseWriter, req *http.Request) {
var services []service
if h.Registry != nil {
names, err := h.Registry.ListServices(req.Context())
if err != nil {
events.Log("error listing services: %{error}s", err)
}
services = make([]service, 0, len(names))
for _, name := range names {
services = append(services, service{
Name: name,
Href: "/services/" + name,
})
}
}
render(res, req, listServices, services)
}
func (h *Handler) serveListTasks(res http.ResponseWriter, req *http.Request) {
var name = strings.TrimPrefix(path.Clean(req.URL.Path), "/services/")
var srv service
if h.Registry != nil {
srvRegistry, err := h.Registry.LookupService(req.Context(), name)
if err != nil {
events.Log("error listing tasks: %{error}s", err)
}
srv.Nodes = make([]node, 0, len(srvRegistry.Hosts))
for _, host := range srvRegistry.Hosts {
srv.Nodes = append(srv.Nodes, node{
Endpoint: fmt.Sprintf("%s %s", host.Addr, strings.Join(host.Tags, " - ")),
Href: "/service/" + host.Addr.String(),
})
}
}
srv.Name = name
srv.Href = "/services/" + name
render(res, req, listNodes, srv)
}
func (h *Handler) serveListPods(res http.ResponseWriter, req *http.Request) {
var services []service
if h.Registry != nil {
names, err := h.Registry.ListServices(req.Context())
if err != nil {
events.Log("error listing services: %{error}s", err)
}
services = make([]service, 0, len(names))
for _, name := range names {
services = append(services, service{
Name: name,
Href: "/pods/" + name,
})
}
}
render(res, req, listServices, services)
}
func (h *Handler) serveListContainers(res http.ResponseWriter, req *http.Request) {
var podname = strings.TrimPrefix(path.Clean(req.URL.Path), "/pods/")
var srv service
if h.Registry != nil {
srvRegistry, err := h.Registry.LookupService(req.Context(), podname)
if err != nil {
events.Log("error listing pods: %{error}s", err)
}
srv.Nodes = make([]node, 0, len(srvRegistry.Hosts))
for _, host := range srvRegistry.Hosts {
srv.Nodes = append(srv.Nodes, node{
Endpoint: fmt.Sprintf("%s %s", host.Addr, strings.Join(host.Tags, " - ")),
Href: "/pod/" + host.Addr.String(),
})
}
}
srv.Name = "kubernetes"
srv.Href = "/pods/" + podname
render(res, req, listNodes, srv)
}
func (h *Handler) serveLookupService(res http.ResponseWriter, req *http.Request) {
var ctx = req.Context()
var endpoint = strings.TrimPrefix(path.Clean(req.URL.Path), "/service/")
var n node
if h.Registry != nil {
p, err := h.fetchService(ctx, endpoint)
if err != nil {
events.Log("error fetching service profiles of %{service}s: %{error}s", endpoint, err)
} else {
n.Profiles = append(n.Profiles, p...)
}
}
sort.Slice(n.Profiles, func(i int, j int) bool {
p1 := n.Profiles[i]
p2 := n.Profiles[j]
if p1.Name != p2.Name {
return p1.Name < p2.Name
}
return p1.URL < p2.URL
})
render(res, req, lookupService, n)
}
func (h *Handler) serveLookupContainer(res http.ResponseWriter, req *http.Request) {
var ctx = req.Context()
var endpoint = strings.TrimPrefix(path.Clean(req.URL.Path), "/pod/")
var n node
if h.Registry != nil {
p, err := h.fetchService(ctx, endpoint)
if err != nil {
events.Log("error fetching service profiles of %{service}s: %{error}s", endpoint, err)
} else {
n.Profiles = append(n.Profiles, p...)
}
}
sort.Slice(n.Profiles, func(i int, j int) bool {
p1 := n.Profiles[i]
p2 := n.Profiles[j]
if p1.Name != p2.Name {
return p1.Name < p2.Name
}
return p1.URL < p2.URL
})
render(res, req, lookupService, n)
}
func (h *Handler) serveFlame(res http.ResponseWriter, req *http.Request) {
queryString := req.URL.Query()
serviceURL := queryString.Get("url")
queryString.Del("url")
if len(serviceURL) == 0 {
res.WriteHeader(http.StatusNotFound)
return
}
// Find the sample type (objects allocated, objects in use, etc)
sampleType := ""
for arg := range queryString {
if arg != "url" {
sampleType = arg
break
}
}
if err := renderFlamegraph(res, serviceURL, sampleType); err != nil {
fmt.Fprintln(res, "Unable to generate flame graph for this profile 🤯")
events.Log("error generating flamegraph: %{error}s", err)
}
}
func (h *Handler) serveTree(res http.ResponseWriter, req *http.Request) {
queryString := req.URL.Query()
serviceURL := queryString.Get("url")
queryString.Del("url")
if len(serviceURL) == 0 {
res.WriteHeader(http.StatusNotFound)
return
}
args := []string{
"tool",
"pprof",
"-svg",
"-symbolize",
"remote",
}
args = append(args, query2pprofArgs(queryString)...)
args = append(args, serviceURL)
buffer := &bytes.Buffer{}
buffer.Grow(32768)
events.Log("go " + strings.Join(args, " "))
pprof := exec.CommandContext(req.Context(), "go", args...)
pprof.Stdin = nil
pprof.Stdout = buffer
pprof.Stderr = log.NewWriter("", 0, events.DefaultHandler)
if pprof.Run() == nil {
buffer.WriteTo(res)
return
}
// failed to render a graph; fall back to serving the raw profile
h.serveRawProfile(res, req, serviceURL)
}
func query2pprofArgs(q url.Values) (args []string) {
for flag, values := range q {
if len(values) == 0 {
args = append(args, "-"+flag)
} else {
for _, value := range values {
args = append(args, "-"+flag, value)
}
}
}
return
}
func (h *Handler) serveRawProfile(w http.ResponseWriter, r *http.Request, url string) {
res, err := h.client().Get(url)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
events.Log("error querying %{url}s: %{error}s", url, err)
return
}
io.Copy(w, res.Body)
res.Body.Close()
}
func (h *Handler) fetchService(ctx context.Context, endpoint string) (prof []profile, err error) {
var req *http.Request
var res *http.Response
var prefix = h.prefix()
if !strings.Contains(endpoint, "://") {
endpoint = "http://" + endpoint
}
if req, err = http.NewRequest(http.MethodGet, endpoint+"/debug/pprof/", nil); err != nil {
return
}
if res, err = h.client().Do(req); err != nil {
return
}
defer res.Body.Close()
if prof, err = parsePprofHome(res.Body); err != nil {
return
}
// For some reason the default profiles aren't returned by the /debug/pprof/
// home page.
//
// Update: In Go 1.11 the profile and trace endpoints are now exposed by the
// index.
hasProfile, hasTrace := false, false
for i, p := range prof {
fullPath, query := splitPathQuery(p.URL)
name := path.Base(fullPath)
baseURL := endpoint
if !strings.HasPrefix(fullPath, "/") {
baseURL += prefix
}
// For heap profiles, inject the options for capturing the allocated objects
// or the allocated space.
if name == "heap" {
// strip debug=1 or it fails to render svg after Go 1.11, it seems to
// render fine in earlier versions.
p.URL, _ = splitPathQuery(p.URL)
prof[i].Name = p.Name + " (objects in use)"
prof[i].URL = baseURL + p.URL
prof[i].Params = "?inuse_objects&url=" + url.QueryEscape(prof[i].URL)
prof = append(prof,
profile{
Name: p.Name + " (space in use)",
URL: baseURL + p.URL,
Params: "?inuse_space&url=" + url.QueryEscape(prof[i].URL),
},
profile{
Name: p.Name + " (objects allocated)",
URL: baseURL + p.URL,
Params: "?alloc_objects&url=" + url.QueryEscape(prof[i].URL),
},
profile{
Name: p.Name + " (space allocated)",
URL: baseURL + p.URL,
Params: "?alloc_space&url=" + url.QueryEscape(prof[i].URL),
},
)
continue
}
if name == "profile" {
hasProfile = true
}
if name == "trace" {
hasTrace = true
}
if (name == "profile" || name == "trace") && query == "" {
query = "?seconds=5"
}
p.URL = fullPath + query
prof[i].URL = baseURL + p.URL
prof[i].Params = "?url=" + url.QueryEscape(prof[i].URL)
}
if !hasProfile {
profURL := endpoint + prefix + "profile?seconds=5"
prof = append(prof, profile{
Name: "profile",
URL: profURL,
Params: "?url=" + url.QueryEscape(profURL),
})
}
if !hasTrace {
profURL := endpoint + prefix + "trace?seconds=5"
prof = append(prof, profile{
Name: "trace",
URL: profURL,
Params: "?url=" + url.QueryEscape(profURL),
})
}
return
}
func (h *Handler) client() *http.Client {
if h.Client != nil {
return h.Client
}
return http.DefaultClient
}
func (h *Handler) prefix() string {
if len(h.Prefix) != 0 {
return h.Prefix
}
return "/debug/pprof/"
}
type service struct {
Name string `json:"name"`
Href string `json:"href"`
Nodes []node `json:"nodes,omitempty"`
}
type node struct {
Name string `json:"name"`
Endpoint string `json:"endpoint"`
Href string `json:"href"`
Profiles []profile `json:"profiles,omitempty"`
}
type profile struct {
Name string `json:"name"`
URL string `json:"url"`
Params string `json:"params"`
}
func render(res http.ResponseWriter, req *http.Request, tpl *template.Template, val interface{}) {
switch accept := strings.TrimSpace(req.Header.Get("Accept")); {
case strings.Contains(accept, "text/html"):
renderHTML(res, tpl, val)
default:
renderJSON(res, val)
}
}
func renderJSON(res http.ResponseWriter, val interface{}) {
res.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewPrettyEncoder(res).Encode(val)
}
func renderHTML(res http.ResponseWriter, tpl *template.Template, val interface{}) {
res.Header().Set("Content-Type", "text/html; charset=utf-8")
tpl.Execute(res, val)
}
func splitPathQuery(s string) (path string, query string) {
if i := strings.IndexByte(s, '?'); i >= 0 {
path, query = s[:i], s[i:]
} else {
path = s
}
return
}