Skip to content

Commit

Permalink
Move debug server to a different port and routes
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
  • Loading branch information
jimthematrix committed Jul 5, 2023
1 parent 73d1904 commit a2da88a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 19 deletions.
66 changes: 66 additions & 0 deletions cmd/debugrouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

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

"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
)

type debugRouter struct {
router *httprouter.Router
}

func NewDebugRouter() *debugRouter {
r := httprouter.New()
cors.Default().Handler(r)
return &debugRouter{
router: r,
}
}

func (d *debugRouter) addRoutes() {
d.router.GET("/debug/pprof/cmdline", d.cmdline)
d.router.GET("/debug/pprof/profile", d.profile)
d.router.GET("/debug/pprof/symbol", d.symbol)
d.router.GET("/debug/pprof/trace", d.trace)
d.router.GET("/debug/pprof/goroutines", d.goroutines)
d.router.GET("/debug/pprof/", d.index)
}

func (d *debugRouter) cmdline(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)
pprof.Cmdline(res, req)
}

func (d *debugRouter) profile(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)
pprof.Profile(res, req)
}

func (d *debugRouter) symbol(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Symbol(res, req)
}

func (d *debugRouter) trace(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Trace(res, req)
}

func (d *debugRouter) index(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Index(res, req)
}

func (d *debugRouter) goroutines(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

_ = runtimepprof.Lookup("goroutine").WriteTo(res, 1)
}
21 changes: 15 additions & 6 deletions cmd/fabconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -94,15 +95,23 @@ func newRootCmd() (*cobra.Command, *conf.RESTGatewayConf) {

initLogging(rootConfig.DebugLevel)

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if rootConfig.DebugPort > 0 {
router := NewDebugRouter()
router.addRoutes()
debugServer := &http.Server{Addr: fmt.Sprintf("127.0.0.1:%d", rootConfig.DebugPort), Handler: router.router, ReadHeaderTimeout: 30 * time.Second}
go func() {
log.Debugf("Debug HTTP endpoint listening on localhost:%d: %s", rootConfig.DebugPort, http.ListenAndServe(fmt.Sprintf("localhost:%d", rootConfig.DebugPort), nil))
_ = debugServer.ListenAndServe()
}()
}
log.Infof("Debug HTTP endpoint listening on 127.0.0.1:%d", rootConfig.DebugPort)

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
defer func() {
log.Infof("Shutting down the debug server")
_ = debugServer.Close()
}()
}
err := startServer(restGatewayConf, restGateway)
if err != nil {
return err
Expand All @@ -112,7 +121,7 @@ func newRootCmd() (*cobra.Command, *conf.RESTGatewayConf) {
}

rootCmd.Flags().IntVarP(&rootConfig.DebugLevel, "debug", "d", 1, "0=error, 1=info, 2=debug")
rootCmd.Flags().IntVarP(&rootConfig.DebugPort, "debugPort", "Z", 6060, "Port for pprof HTTP endpoints (localhost only)")
rootCmd.Flags().IntVarP(&rootConfig.DebugPort, "debugPort", "Z", 0, "Port for pprof HTTP endpoints (localhost only)")
rootCmd.Flags().BoolVarP(&rootConfig.PrintYAML, "print-yaml-confg", "Y", false, "Print YAML config snippet and exit")
rootCmd.Flags().StringVarP(&rootConfig.Filename, "configfile", "f", "", "Configuration file, must be one of .yml, .yaml, or .json")
conf.CobraInit(rootCmd, restGatewayConf)
Expand Down
18 changes: 18 additions & 0 deletions cmd/fabconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,21 @@ func TestKafkaSuccess(t *testing.T) {
assert.Equal([]string{"broker1", "broker2"}, restGatewayConf.Kafka.Brokers)
test.Teardown(tmpdir)
}

func TestDebugServer(t *testing.T) {
assert := assert.New(t)

tmpdir, _ := test.Setup()
rootCmd, _ := newRootCmd()
rootCmd.RunE = runNothing
args := []string{
"-f", path.Join(tmpdir, "config.json"),
"--debugPort", "6060",
}
rootCmd.SetArgs(args)
os.Unsetenv("FC_HTTP_PORT")
err := rootCmd.Execute()
assert.NoError(err)
assert.Equal(6060, rootConfig.DebugPort)
test.Teardown(tmpdir)
}
7 changes: 0 additions & 7 deletions internal/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"runtime/pprof"
"strings"

"github.com/hyperledger/firefly-fabconnect/internal/auth"
Expand Down Expand Up @@ -102,7 +101,6 @@ func (r *router) addRoutes() {

r.httpRouter.GET("/ws", r.wsHandler)
r.httpRouter.GET("/status", r.statusHandler)
r.httpRouter.POST("/pprof", r.dumpGoRoutines)
}

func (r *router) newAccessTokenContextHandler() http.Handler {
Expand Down Expand Up @@ -448,11 +446,6 @@ func (r *router) resetSubscription(res http.ResponseWriter, req *http.Request, p
marshalAndReply(res, req, result)
}

func (r *router) dumpGoRoutines(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)
_ = pprof.Lookup("goroutine").WriteTo(res, 1)
}

func restAsyncReply(res http.ResponseWriter, req *http.Request, asyncResponse *messages.AsyncSentMsg) {
resBytes, _ := json.Marshal(asyncResponse)
status := 202 // accepted
Expand Down
12 changes: 6 additions & 6 deletions mocks/fabric/client/rpc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a2da88a

Please sign in to comment.