File size: 874 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package app

import (
	"net"
	"net/http"
	"net/http/pprof"
	"runtime"

	"github.com/spf13/viper"
)

func initPprofServer() error {
	addr := viper.GetString("listen-pprof")
	if addr == "" {
		return nil
	}

	l, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	mux := http.NewServeMux()

	// Register pprof handlers (matches init() of net/http/pprof package)
	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

	runtime.SetBlockProfileRate(viper.GetInt("pprof-block-profile-rate"))
	runtime.SetMutexProfileFraction(viper.GetInt("pprof-mutex-profile-fraction"))

	srv := http.Server{
		Handler: mux,
	}
	go func() { _ = srv.Serve(l) }()
	return nil
}