diff --git a/pkg/cache/metrics.go b/pkg/cache/metrics.go index 9203b9bc3..649df52fd 100644 --- a/pkg/cache/metrics.go +++ b/pkg/cache/metrics.go @@ -4,6 +4,7 @@ import "github.com/bilibili/kratos/pkg/stat/metric" const _metricNamespace = "cache" +// be used in tool/kratos-gen-bts var ( MetricHits = metric.NewCounterVec(&metric.CounterVecOpts{ Namespace: _metricNamespace, diff --git a/pkg/cache/redis/pool.go b/pkg/cache/redis/pool.go index ad1beef35..9da107919 100644 --- a/pkg/cache/redis/pool.go +++ b/pkg/cache/redis/pool.go @@ -193,7 +193,9 @@ func (pc *pooledConnection) Do(commandName string, args ...interface{}) (reply i ci := LookupCommandInfo(commandName) pc.state = (pc.state | ci.Set) &^ ci.Clear reply, err = pc.c.Do(commandName, args...) - pc.p.statfunc(pc.p.c.Name, pc.p.c.Addr, commandName, now, err)() + if pc.p.statfunc != nil { + pc.p.statfunc(pc.p.c.Name, pc.p.c.Addr, commandName, now, err)() + } return } @@ -217,7 +219,9 @@ func (pc *pooledConnection) Receive() (reply interface{}, err error) { if len(pc.cmds) > 0 { cmd := pc.cmds[0] pc.cmds = pc.cmds[1:] - pc.p.statfunc(pc.p.c.Name, pc.p.c.Addr, cmd, pc.now, err)() + if pc.p.statfunc != nil { + pc.p.statfunc(pc.p.c.Name, pc.p.c.Addr, cmd, pc.now, err)() + } } return } diff --git a/pkg/naming/discovery/discovery.go b/pkg/naming/discovery/discovery.go index 635229ce4..d5c24f9f9 100644 --- a/pkg/naming/discovery/discovery.go +++ b/pkg/naming/discovery/discovery.go @@ -370,7 +370,9 @@ func (d *Discovery) register(ctx context.Context, ins *naming.Instance) (err err uri := fmt.Sprintf(_registerURL, d.pickNode()) params := d.newParams(c) params.Set("appid", ins.AppID) - params.Set("addrs", strings.Join(ins.Addrs, ",")) + for _, addr := range ins.Addrs { + params.Add("addrs", addr) + } params.Set("version", ins.Version) params.Set("status", _statusUP) params.Set("metadata", string(metadata)) diff --git a/pkg/net/http/blademaster/perf.go b/pkg/net/http/blademaster/perf.go index a74e22ecc..ad6c1cbb9 100644 --- a/pkg/net/http/blademaster/perf.go +++ b/pkg/net/http/blademaster/perf.go @@ -19,28 +19,45 @@ var ( func init() { v := os.Getenv("HTTP_PERF") - if v == "" { - v = "tcp://0.0.0.0:2333" - } flag.StringVar(&_perfDSN, "http.perf", v, "listen http perf dsn, or use HTTP_PERF env variable.") } -func startPerf() { +func startPerf(engine *Engine) { _perfOnce.Do(func() { - mux := http.NewServeMux() - 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) + if os.Getenv("HTTP_PERF") == "" { + prefixRouter := engine.Group("/debug/pprof") + { + prefixRouter.GET("/", pprofHandler(pprof.Index)) + prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline)) + prefixRouter.GET("/profile", pprofHandler(pprof.Profile)) + prefixRouter.POST("/symbol", pprofHandler(pprof.Symbol)) + prefixRouter.GET("/symbol", pprofHandler(pprof.Symbol)) + prefixRouter.GET("/trace", pprofHandler(pprof.Trace)) + prefixRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP)) + prefixRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP)) + prefixRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP)) + prefixRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP)) + prefixRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP)) + prefixRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP)) + } + return + } go func() { d, err := dsn.Parse(_perfDSN) if err != nil { panic(errors.Errorf("blademaster: http perf dsn must be tcp://$host:port, %s:error(%v)", _perfDSN, err)) } - if err := http.ListenAndServe(d.Host, mux); err != nil { + if err := http.ListenAndServe(d.Host, nil); err != nil { panic(errors.Errorf("blademaster: listen %s: error(%v)", d.Host, err)) } }() }) } + +func pprofHandler(h http.HandlerFunc) HandlerFunc { + handler := http.HandlerFunc(h) + return func(c *Context) { + handler.ServeHTTP(c.Writer, c.Request) + } +} diff --git a/pkg/net/http/blademaster/server.go b/pkg/net/http/blademaster/server.go index 29b47f2eb..6fa12ac56 100644 --- a/pkg/net/http/blademaster/server.go +++ b/pkg/net/http/blademaster/server.go @@ -195,7 +195,7 @@ func NewServer(conf *ServerConfig) *Engine { c.Bytes(405, "text/plain", []byte(http.StatusText(405))) c.Abort() }) - startPerf() + startPerf(engine) return engine } diff --git a/pkg/net/rpc/warden/server.go b/pkg/net/rpc/warden/server.go index 60656cd22..f741505e9 100644 --- a/pkg/net/rpc/warden/server.go +++ b/pkg/net/rpc/warden/server.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" "google.golang.org/grpc" + _ "google.golang.org/grpc/encoding/gzip" // NOTE: use grpc gzip by header grpc-accept-encoding "google.golang.org/grpc/keepalive" "google.golang.org/grpc/metadata" "google.golang.org/grpc/peer"