Repository navigation
Expand file tree
/
Copy pathdebug.go
More file actions
181 lines (158 loc) · 5.21 KB
/
Copy pathdebug.go
File metadata and controls
181 lines (158 loc) · 5.21 KB
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
package api
// From https://golang.org/src/net/http/pprof/pprof.go
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"context"
"fmt"
"net/http"
"runtime"
"runtime/pprof"
"runtime/trace"
"strconv"
"time"
"github.com/ovh/cds/sdk"
"github.com/gorilla/mux"
"github.com/ovh/cds/engine/service"
)
func (api *API) getDebugProfilesHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
profiles := pprof.Profiles()
profileCounters := map[string]int{}
for _, p := range profiles {
if p != nil {
profileCounters[p.Name()] = p.Count()
}
}
return service.WriteJSON(w, profileCounters, http.StatusOK)
}
}
func (api *API) getDebugGoroutinesHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
grs, err := sdk.ListGoroutines()
if err != nil {
return sdk.WithStack(err)
}
type goroutineInfo struct {
State string
CreatedBy string
SourcePath string
Caller string
}
var result = make(map[string][]goroutineInfo)
for _, goroutine := range grs {
info := goroutineInfo{
State: goroutine.State,
}
if len(goroutine.CreatedBy.Calls) > 0 {
info.CreatedBy = goroutine.CreatedBy.Calls[0].Func.Complete
info.SourcePath = goroutine.CreatedBy.Calls[0].LocalSrcPath
}
if len(goroutine.Stack.Calls) > 0 {
info.Caller = goroutine.Stack.Calls[0].Func.Complete
}
result[goroutine.State] = append(result[goroutine.State], info)
}
return service.WriteJSON(w, result, http.StatusOK)
}
}
// getProfileHandler responds with the pprof-formatted profile named by the request.
func (api *API) getProfileHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
name := mux.Vars(r)["name"]
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
debug := service.FormInt(r, "debug")
p := pprof.Lookup(name)
if p == nil {
w.WriteHeader(404)
fmt.Fprintf(w, "Unknown profile: %s\n", name)
return nil
}
gc := service.FormInt(r, "gc")
if name == "heap" && gc > 0 {
runtime.GC()
}
p.WriteTo(w, debug)
return nil
}
}
func sleep(w http.ResponseWriter, d time.Duration) {
var clientGone <-chan bool
if cn, ok := w.(http.CloseNotifier); ok {
clientGone = cn.CloseNotify()
}
select {
case <-time.After(d):
case <-clientGone:
}
}
func durationExceedsWriteTimeout(r *http.Request, seconds float64) bool {
srv, ok := r.Context().Value(http.ServerContextKey).(*http.Server)
return ok && srv.WriteTimeout != 0 && seconds >= srv.WriteTimeout.Seconds()
}
// getTraceHandler responds with the execution trace in binary form.
// Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified.
func (api *API) getTraceHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
sec, err := strconv.ParseFloat(r.FormValue("seconds"), 64)
if sec <= 0 || err != nil {
sec = 1
}
if durationExceedsWriteTimeout(r, sec) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "profile duration exceeds server's WriteTimeout")
return nil
}
// Set Content Type assuming trace.Start will work,
// because if it does it starts writing.
w.Header().Set("Content-Type", "application/octet-stream")
if err := trace.Start(w); err != nil {
// trace.Start failed, so no writes yet.
// Can change header back to text content and send error code.
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not enable tracing: %s\n", err)
return nil
}
sleep(w, time.Duration(sec*float64(time.Second)))
trace.Stop()
return nil
}
}
// getCPUProfileHandler responds with the pprof-formatted cpu profile.
// The package initialization registers it as /debug/pprof/profile.
func (api *API) getCPUProfileHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
sec := service.FormInt64(r, "seconds")
if sec == 0 {
sec = 30
}
if durationExceedsWriteTimeout(r, float64(sec)) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "profile duration exceeds server's WriteTimeout")
return nil
}
// Set Content Type assuming StartCPUProfile will work,
// because if it does it starts writing.
w.Header().Set("Content-Type", "application/octet-stream")
if err := pprof.StartCPUProfile(w); err != nil {
// StartCPUProfile failed, so no writes yet.
// Can change header back to text content
// and send error code.
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
return nil
}
sleep(w, time.Duration(sec)*time.Second)
pprof.StopCPUProfile()
return nil
}
}