Skip to content

Commit 443606d

Browse files
authored
feat(pprof): custom pprof (#30)
1 parent 0e0e264 commit 443606d

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

pkg/pprof/pprof.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package pprof
2+
3+
import (
4+
"net/http"
5+
"net/http/pprof"
6+
"strings"
7+
8+
"github.com/yomorun/yomo/pkg/util"
9+
10+
"github.com/yomorun/yomo/pkg/env"
11+
)
12+
13+
type pprofConf struct {
14+
Enabled bool
15+
PathPrefix string
16+
Endpoint string
17+
}
18+
19+
const (
20+
pprofEnabled = "YOMO_PPROF_ENABLED"
21+
pathPrefix = "YOMO_PPROF_PATH_PREFIX"
22+
endpoint = "YOMO_PPROF_ENDPOINT"
23+
)
24+
25+
func newEdgeConf() pprofConf {
26+
conf := pprofConf{}
27+
conf.Enabled = env.GetBool(pprofEnabled, false)
28+
conf.PathPrefix = env.GetString(pathPrefix, "/debug/pprof/")
29+
conf.Endpoint = env.GetString(endpoint, "0.0.0.0:6060")
30+
return conf
31+
}
32+
33+
var logger = util.GetLogger("yomo::pprof")
34+
35+
func Run() {
36+
conf := newEdgeConf()
37+
if conf.Enabled == false {
38+
return
39+
}
40+
41+
mux := http.NewServeMux()
42+
pathPrefix := conf.PathPrefix
43+
mux.HandleFunc(pathPrefix,
44+
func(w http.ResponseWriter, r *http.Request) {
45+
name := strings.TrimPrefix(r.URL.Path, pathPrefix)
46+
if name != "" {
47+
pprof.Handler(name).ServeHTTP(w, r)
48+
return
49+
}
50+
pprof.Index(w, r)
51+
})
52+
mux.HandleFunc(pathPrefix+"cmdline", pprof.Cmdline)
53+
mux.HandleFunc(pathPrefix+"profile", pprof.Profile)
54+
mux.HandleFunc(pathPrefix+"symbol", pprof.Symbol)
55+
mux.HandleFunc(pathPrefix+"trace", pprof.Trace)
56+
57+
server := http.Server{
58+
Addr: conf.Endpoint,
59+
Handler: mux,
60+
}
61+
62+
logger.Infof("PProf server start... http://%s%s\n", conf.Endpoint, conf.PathPrefix)
63+
if err := server.ListenAndServe(); err != nil {
64+
if err == http.ErrServerClosed {
65+
logger.Errorf("PProf server closed.")
66+
} else {
67+
logger.Errorf("PProf server error: %v", err)
68+
}
69+
}
70+
}

pkg/util/quic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (w YomoFrameworkStreamWriter) Write(b []byte) (c int, e error) {
7878
return sum + num, nil
7979
}
8080

81-
logger.Debugf("Plugin.Handle result: %s", result) //debug:
81+
logger.Debugf("Plugin.Handle result: %v", result) //debug:
8282
if result == nil {
8383
continue
8484
}

pkg/yomo/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"time"
88

9+
"github.com/yomorun/yomo/pkg/pprof"
10+
911
"github.com/yomorun/yomo/pkg/plugin"
1012
"github.com/yomorun/yomo/pkg/util"
1113

@@ -18,6 +20,9 @@ var logger = util.GetLogger("yomo::run")
1820
func Run(plugin plugin.YomoObjectPlugin, endpoint string) {
1921
logger.Infof("plugin service [%s] start... [%s]", plugin.Name(), endpoint)
2022

23+
// pprof
24+
go pprof.Run()
25+
2126
// activation service
2227
framework.NewServer(endpoint, plugin)
2328
}

0 commit comments

Comments
 (0)