lxg 4 роки тому
батько
коміт
3d300cddba
2 змінених файлів з 20 додано та 0 видалено
  1. 7 0
      options.go
  2. 13 0
      service.go

+ 7 - 0
options.go

@@ -25,6 +25,7 @@ type (
 		Address                string            //绑定地址
 		TSDBUrl                string            //TSDB数据上报URL
 		MetricCallback         ReadMetricFunc    //读取系统数据的回调
+		EnableHttpPProf        bool              //启用HTTP调试工具
 		Context                context.Context
 		shortName              string
 	}
@@ -57,6 +58,12 @@ func WithPort(port int) Option {
 	}
 }
 
+func WithHttpDebug() Option {
+	return func(o *Options) {
+		o.EnableHttpPProf = true
+	}
+}
+
 func WithRegistry(r registry.Registry) Option {
 	return func(o *Options) {
 		o.registry = r

+ 13 - 0
service.go

@@ -5,6 +5,8 @@ import (
 	"crypto/md5"
 	"encoding/hex"
 	"net"
+	hp "net/http"
+	"net/http/pprof"
 	"os"
 	"os/signal"
 	"runtime"
@@ -231,6 +233,17 @@ func (svr *Service) startHttpServe() (err error) {
 				"uptime":  time.Now().Sub(svr.upTime).String(),
 			})
 		})
+		if svr.opts.EnableHttpPProf {
+			svr.httpSvr.Handler("GET", "/debug/pprof/", hp.HandlerFunc(pprof.Index))
+			svr.httpSvr.Handler("GET", "/debug/pprof/goroutine", hp.HandlerFunc(pprof.Index))
+			svr.httpSvr.Handler("GET", "/debug/pprof/heap", hp.HandlerFunc(pprof.Index))
+			svr.httpSvr.Handler("GET", "/debug/pprof/mutex", hp.HandlerFunc(pprof.Index))
+			svr.httpSvr.Handler("GET", "/debug/pprof/threadcreate", hp.HandlerFunc(pprof.Index))
+			svr.httpSvr.Handler("GET", "/debug/pprof/cmdline", hp.HandlerFunc(pprof.Cmdline))
+			svr.httpSvr.Handler("GET", "/debug/pprof/profile", hp.HandlerFunc(pprof.Profile))
+			svr.httpSvr.Handler("GET", "/debug/pprof/symbol", hp.HandlerFunc(pprof.Symbol))
+			svr.httpSvr.Handler("GET", "/debug/pprof/trace", hp.HandlerFunc(pprof.Trace))
+		}
 		log.Infof("attach http server success")
 	} else {
 		log.Warnf("attach http listener error: %s", err.Error())