2017-11-09 15:12:04 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-11-20 10:04:03 +00:00
|
|
|
"net/http/pprof"
|
2017-11-09 15:12:04 +00:00
|
|
|
"runtime"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-11-09 15:12:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-08-31 06:24:08 +00:00
|
|
|
// TODO Goroutines2 -> Goroutines
|
2018-11-14 09:18:03 +00:00
|
|
|
expvar.Publish("Goroutines2", expvar.Func(goroutines))
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func goroutines() interface{} {
|
|
|
|
return runtime.NumGoroutine()
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// DebugHandler expose debug routes.
|
2017-11-09 15:12:04 +00:00
|
|
|
type DebugHandler struct{}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Append add debug routes on a router.
|
2018-11-14 09:18:03 +00:00
|
|
|
func (g DebugHandler) Append(router *mux.Router) {
|
2017-11-20 08:40:03 +00:00
|
|
|
router.Methods(http.MethodGet).Path("/debug/vars").
|
2017-11-09 15:12:04 +00:00
|
|
|
HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
fmt.Fprint(w, "{\n")
|
|
|
|
first := true
|
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
if !first {
|
|
|
|
fmt.Fprint(w, ",\n")
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
|
})
|
|
|
|
fmt.Fprint(w, "\n}\n")
|
|
|
|
})
|
2017-11-20 10:04:03 +00:00
|
|
|
|
2018-07-06 08:58:04 +00:00
|
|
|
runtime.SetBlockProfileRate(1)
|
|
|
|
runtime.SetMutexProfileFraction(5)
|
2017-11-20 10:04:03 +00:00
|
|
|
router.Methods(http.MethodGet).PathPrefix("/debug/pprof/cmdline").HandlerFunc(pprof.Cmdline)
|
|
|
|
router.Methods(http.MethodGet).PathPrefix("/debug/pprof/profile").HandlerFunc(pprof.Profile)
|
|
|
|
router.Methods(http.MethodGet).PathPrefix("/debug/pprof/symbol").HandlerFunc(pprof.Symbol)
|
|
|
|
router.Methods(http.MethodGet).PathPrefix("/debug/pprof/trace").HandlerFunc(pprof.Trace)
|
2017-12-05 09:50:03 +00:00
|
|
|
router.Methods(http.MethodGet).PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|