forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
109 lines (96 loc) · 3.17 KB
/
debug.go
File metadata and controls
109 lines (96 loc) · 3.17 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
package debugserver
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"strings"
"github.com/sourcegraph/sourcegraph/pkg/env"
"golang.org/x/net/trace"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr = env.Get("SRC_PROF_HTTP", ":6060", "net/http/pprof http bind address.")
)
func init() {
err := json.Unmarshal([]byte(env.Get("SRC_PROF_SERVICES", "[]", "list of net/http/pprof http bind address.")), &Services)
if err != nil {
panic("failed to JSON unmarshal SRC_PROF_SERVICES: " + err.Error())
}
if addr == "" {
// Look for our binname in the services list
name := filepath.Base(os.Args[0])
for _, svc := range Services {
if svc.Name == name {
addr = svc.Host
break
}
}
}
}
// Endpoint is a handler for the debug server. It will be displayed on the
// debug index page.
type Endpoint struct {
// Name is the name shown on the index page for the endpoint
Name string
// Path is passed to http.Mux.Handle as the pattern.
Path string
// Handler is the debug handler
Handler http.Handler
}
// Services is the list of registered services' debug addresses. Populated
// from SRC_PROF_MAP.
var Services []Service
// Service is a service's debug addr (host:port).
type Service struct {
// Name of the service. Always the binary name. example: "gitserver"
Name string
// Host is the host:port for the services SRC_PROF_HTTP. example:
// "127.0.0.1:6060"
Host string
}
// Start runs a debug server (pprof, prometheus, etc) if it is configured (via
// SRC_PROF_HTTP environment variable). It is blocking.
func Start(extra ...Endpoint) {
if addr == "" {
return
}
pp := http.NewServeMux()
index := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<a href="vars">Vars</a><br>
<a href="debug/pprof/">PProf</a><br>
<a href="metrics">Metrics</a><br>
<a href="debug/requests">Requests</a><br>
<a href="debug/events">Events</a><br>
`))
for _, e := range extra {
fmt.Fprintf(w, `<a href="%s">%s</a><br>`, strings.TrimPrefix(e.Path, "/"), e.Name)
}
w.Write([]byte(`
<br>
<form method="post" action="gc" style="display: inline;"><input type="submit" value="GC"></form>
<form method="post" action="freeosmemory" style="display: inline;"><input type="submit" value="Free OS Memory"></form>
`))
})
pp.Handle("/", index)
pp.Handle("/debug", index)
pp.Handle("/vars", http.HandlerFunc(expvarHandler))
pp.Handle("/gc", http.HandlerFunc(gcHandler))
pp.Handle("/freeosmemory", http.HandlerFunc(freeOSMemoryHandler))
pp.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
pp.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
pp.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
pp.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
pp.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
pp.Handle("/debug/requests", http.HandlerFunc(trace.Traces))
pp.Handle("/debug/events", http.HandlerFunc(trace.Events))
pp.Handle("/metrics", promhttp.Handler())
for _, e := range extra {
pp.Handle(e.Path, e.Handler)
}
log.Println("warning: could not start debug HTTP server:", http.ListenAndServe(addr, pp))
}