forked from lazada/goprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
365 lines (335 loc) · 10.1 KB
/
web.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
package goprof
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/kardianos/osext"
)
const writtenProfilesRawTemplate = `<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Profiling tools</title>
</head>
<body>
{{ if .Message }}<p>{{ .Message }}</p>{{ end }}
{{ if .CurrentProfile }}
<p>Writing {{ .CurrentProfile.Prof }} profile to {{ .CurrentProfile.Dir }} <a href="toggle?enable=0">Stop</a>. Started <span id="started-ago"></span>.</p>
<script>
startedAgo = {{ .ProfileStartedSecondsAgo }};
updateStartedAgoUI = function() {
sec = startedAgo % 60;
min = Math.floor(startedAgo / 60);
durationStr = "";
if (min > 0) {durationStr+=min + "min";}
if (sec > 0) {durationStr+=" " + sec + "sec";}
if (durationStr === "") {durationStr="just now";}else{durationStr+=" ago";}
document.getElementById("started-ago").innerHTML = durationStr;
}
updateStartedAgoUI();
window.setInterval(function(){
updateStartedAgoUI();
startedAgo++;
}, 1000);
</script>
{{ else }}
<p>Start profiling:
<a href="toggle?enable=1&profile=all">all</a>
<a href="toggle?enable=1&profile=cpu">cpu</a>
<a href="toggle?enable=1&profile=heap">heap (allocations since last gc)</a>
<a href="toggle?enable=1&profile=trace">trace</a>
<a href="toggle?enable=1&profile=goroutine">goroutine</a>
<a href="toggle?enable=1&profile=threadcreate">threadcreate</a>
<a href="toggle?enable=1&profile=block">block</a>
</p>
{{ end }}
<p>
Written profiles:
<ul>
{{ range .WrittenProfiles }}
<li><a href="{{ download .Dir }}">
{{ .Prof }}
{{ if .Prof.OneOff }}
({{.Start}})
{{ else }}
(lasted for {{.Duration}} since {{.Start}})
{{ end }}
</a>
{{ else }}
<li>none
{{ end }}
</ul>
</p>
</body>
</html>`
const showWebScriptTpl = `#!/bin/bash
cd $(dirname $0)
go tool pprof -web {{bin}} {{profile}}`
type ProfileListResponse struct {
OK bool `json:"ok"`
Items []prof `json:"items"`
}
type SimpleResponse struct {
OK bool `json:"ok"`
ErrorMessage string `json:"error_message,omitempty"`
}
var (
writtenProfilesTemplate = template.Must(template.New("profiles").Funcs(template.FuncMap{
"download": formatDownloadURL,
}).Parse(writtenProfilesRawTemplate))
)
func formatDownloadURL(path string) string {
return fmt.Sprintf("download/%s.tgz?path=%s", filepath.Base(path), path)
}
func isJsonRequest(r *http.Request) bool {
if r.URL.Query().Get("json") == "1" {
return true
}
values := make(map[string]interface{})
for _, val := range strings.Split(r.Header.Get("accept"), ",") {
values[val] = struct{}{}
}
_, ok := values["application/json"]
return ok
}
func fatalError(w http.ResponseWriter, r *http.Request, errorMessage string) {
w.WriteHeader(http.StatusBadRequest)
if isJsonRequest(r) {
w.Header().Add("Content-Type", "application/json")
encoder := json.NewEncoder(w)
res := SimpleResponse{
OK: false,
ErrorMessage: errorMessage,
}
encoder.Encode(res)
} else {
http.Error(w, errorMessage, http.StatusBadRequest)
}
}
func flashError(w http.ResponseWriter, r *http.Request, errorMessage string) {
w.WriteHeader(http.StatusBadRequest)
if isJsonRequest(r) {
w.Header().Add("Content-Type", "application/json")
encoder := json.NewEncoder(w)
encoder.Encode(SimpleResponse{
OK: false,
ErrorMessage: errorMessage,
})
} else {
renderPage(w, errorMessage)
}
}
func success(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
if isJsonRequest(r) {
encoder := json.NewEncoder(w)
encoder.Encode(SimpleResponse{
OK: true,
})
} else {
w.Header().Add("Content-Type", "text/html")
renderPage(w, "")
}
}
// handler for toggling profiling. Expects mandatory parameter 'enable' which should be either '0' or '1'
func toggleProfiling(w http.ResponseWriter, r *http.Request) {
ourProfilingStateGuard.Lock()
defer ourProfilingStateGuard.Unlock()
query := r.URL.Query()
enableParam := query.Get("enable")
if enableParam != "0" && enableParam != "1" {
fatalError(w, r, fmt.Sprintf("Bad value for mandatory 'enable' param: '%v'. Please, use 0 or 1.", enableParam))
return
}
enableProfiling := enableParam == "1"
var dir string
var err error
if enableProfiling {
profile := profName(query.Get("profile"))
dir, err = startProfiling(profile)
} else {
dir = stopProfiling()
}
if err != nil {
flashError(w, r, fmt.Sprintf("Failed to toggle profiling (enable=%v): %v", enableProfiling, err))
return
}
if enableProfiling {
success(w, r)
return
}
if dir == "" {
flashError(w, r,"Seems profiling already stopped")
return
}
success(w, r)
}
// handler for downloading written profile files and binary as a single tar.gz archive
// Expects 'path' parameter to point to existing directory with profiles
// If any file is not found (binary or any of profiles) it returns an error
func downloadProfile(w http.ResponseWriter, r *http.Request) {
// check mandatory param
profilesDir := r.URL.Query().Get("path")
if profilesDir == "" {
fatalError(w, r, "No such profile (param 'path' is mandatory)")
return
}
// check that we aren't writing the profile at the moment
ourProfilingStateGuard.RLock()
defer ourProfilingStateGuard.RUnlock()
if ourCurrentProfile != nil && ourCurrentProfile.Dir == profilesDir {
flashError(w, r, "We write the requested profile at the moment. Stop it first, then you will be able to download it")
return
}
// check that the param is an accessible directory
fileInfo, err := os.Stat(profilesDir)
if err != nil {
fatalError(w, r, fmt.Sprintf("Cannot stat '%v': %v", profilesDir, err))
return
}
if !fileInfo.IsDir() {
fatalError(w, r, fmt.Sprintf("Expecting '%v' to be a directory, but it is not", profilesDir))
return
}
// pack archive and send it to the client
archive, err := packProfiles(profilesDir)
if err != nil {
fatalError(w, r, fmt.Sprintf("Failed to pack profiles: %v", err))
return
}
_, err = io.Copy(w, archive)
if err != nil {
fatalError(w, r, fmt.Sprintf( "Failed serve archive: %v", err))
return
}
}
func packProfiles(profilesDir string) (*bytes.Buffer, error) {
archiveBytes := &bytes.Buffer{}
gz := gzip.NewWriter(archiveBytes)
defer gz.Close()
archive := tar.NewWriter(gz)
defer archive.Close()
binary, err := osext.Executable()
if err != nil {
return nil, err
}
if err := writeFile(archive, binary); err != nil {
return nil, err
}
children, err := ioutil.ReadDir(profilesDir)
if err != nil {
return nil, fmt.Errorf("failed to ls '%v': %v", profilesDir, err)
}
for _, child := range children {
childName := filepath.Join(profilesDir, child.Name())
if err := writeFile(archive, childName); err != nil {
return nil, fmt.Errorf("failed to write %v: %v", childName, err)
}
}
dirname := filepath.Base(profilesDir)
if !strings.HasPrefix("prof-all", dirname) && !strings.HasPrefix("prof-trace", dirname) && len(children) == 1 {
binName := filepath.Base(binary)
profileName := children[0].Name()
withBinary := strings.Replace(showWebScriptTpl, "{{bin}}", binName, -1)
scriptSrc := strings.Replace(withBinary, "{{profile}}", profileName, -1)
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %v", err)
}
tmpFile, err := os.Create(filepath.Join(tmpDir, "show-web"))
if err != nil {
return nil, fmt.Errorf("failed to create file in temp dir %v: %v", tmpDir, err)
}
defer os.RemoveAll(tmpDir)
err = tmpFile.Chmod(0777)
if err != nil {
return nil, fmt.Errorf("failed to chmod temp file %v: %v", tmpFile, err)
}
_, err = tmpFile.WriteString(scriptSrc)
if err != nil {
return nil, fmt.Errorf("failed to write temp file %v: %v", tmpFile, err)
}
if err := writeFile(archive, tmpFile.Name()); err != nil {
return nil, fmt.Errorf("failed to write %v: %v", tmpFile.Name(), err)
}
}
return archiveBytes, nil
}
// write a single file into the provided archive
func writeFile(archive *tar.Writer, filePath string) error {
fileInfo, err := os.Stat(filePath)
if err != nil {
return err
}
header, err := tar.FileInfoHeader(fileInfo, "")
if err != nil {
return err
}
if err := archive.WriteHeader(header); err != nil {
return err
}
file, err := os.Open(filePath)
if err != nil {
return err
}
if _, err := io.Copy(archive, file); err != nil {
return err
}
return nil
}
// showWrittenProfiles renders page with list of all written profiles
func showWrittenProfiles(w http.ResponseWriter, r *http.Request) {
ourProfilingStateGuard.RLock()
defer ourProfilingStateGuard.RUnlock()
if isJsonRequest(r) {
resp := ProfileListResponse{
OK: true,
Items: ourWrittenProfiles,
}
w.Header().Add("Content-Type", "application/json")
encoder := json.NewEncoder(w)
encoder.Encode(resp)
} else {
renderPage(w, "")
}
}
func renderPage(w http.ResponseWriter, msg string) {
templateData := struct {
WrittenProfiles []prof
CurrentProfile *prof
Message string
ProfileStartedSecondsAgo int
}{ourWrittenProfiles, ourCurrentProfile, msg, 0}
if ourCurrentProfile != nil {
templateData.ProfileStartedSecondsAgo = int(time.Since(ourCurrentProfile.Start).Seconds())
}
err := writtenProfilesTemplate.Execute(w, templateData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed to render template: %v\n", err)
}
}
// ListenAndServe starts server on provided address for toggling profiling and downloading results
func ListenAndServe(address string) error {
return http.ListenAndServe(address, NewHandler())
}
// NewHandler creates http handler for the whole profiling tools application
// If you want to use it aside of other handlers, don't miss http.StripPrefix wrapping like
// mux.Handle("/pprof/", http.StripPrefix("/pprof", goprof.NewHandler()))
func NewHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", showWrittenProfiles)
mux.HandleFunc("/toggle", toggleProfiling)
mux.HandleFunc("/download/", downloadProfile)
return mux
}