Browse Source

:gear: Attach pprof to API for debugging

Ettore Di Giacinto 3 years ago
parent
commit
f51c7993ea
5 changed files with 19 additions and 5 deletions
  1. 5 1
      api/api.go
  2. 1 1
      api/api_test.go
  3. 4 1
      cmd/api.go
  4. 5 1
      cmd/main.go
  5. 4 1
      cmd/proxy.go

+ 5 - 1
api/api.go

@@ -22,6 +22,7 @@ import (
 	"io/fs"
 	"net"
 	"net/http"
+	_ "net/http/pprof"
 	"strings"
 	"time"
 
@@ -62,7 +63,7 @@ const (
 	PeerstoreURL  = "/api/peerstore"
 )
 
-func API(ctx context.Context, l string, defaultInterval, timeout time.Duration, e *node.Node) error {
+func API(ctx context.Context, l string, defaultInterval, timeout time.Duration, e *node.Node, debugMode bool) error {
 
 	ledger, _ := e.Ledger()
 
@@ -77,6 +78,9 @@ func API(ctx context.Context, l string, defaultInterval, timeout time.Duration,
 	}
 
 	assetHandler := http.FileServer(getFileSystem())
+	if debugMode {
+		ec.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
+	}
 
 	// Get data from ledger
 	ec.GET(FileURL, func(c echo.Context) error {

+ 1 - 1
api/api_test.go

@@ -54,7 +54,7 @@ var _ = Describe("API", func() {
 			e.Start(ctx)
 
 			go func() {
-				err := API(ctx, fmt.Sprintf("unix://%s", socket), 10*time.Second, 20*time.Second, e)
+				err := API(ctx, fmt.Sprintf("unix://%s", socket), 10*time.Second, 20*time.Second, e, false)
 				Expect(err).ToNot(HaveOccurred())
 			}()
 

+ 4 - 1
cmd/api.go

@@ -32,6 +32,9 @@ func API() cli.Command {
 A simple UI interface is available to display network data.`,
 		UsageText: "edgevpn api",
 		Flags: append(CommonFlags,
+			&cli.BoolFlag{
+				Name: "debug",
+			},
 			&cli.StringFlag{
 				Name:  "listen",
 				Value: ":8080",
@@ -53,7 +56,7 @@ A simple UI interface is available to display network data.`,
 				return err
 			}
 
-			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e)
+			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
 		},
 	}
 }

+ 5 - 1
cmd/main.go

@@ -57,6 +57,10 @@ func MainFlags() []cli.Flag {
 			Name:  "b",
 			Usage: "Encodes the new config in base64, so it can be used as a token",
 		},
+		&cli.BoolFlag{
+			Name:  "debug",
+			Usage: "Starts API with pprof attached",
+		},
 		&cli.BoolFlag{
 			Name:   "api",
 			Usage:  "Starts also the API daemon locally for inspecting the network status",
@@ -223,7 +227,7 @@ func Main() func(c *cli.Context) error {
 		}
 
 		if c.Bool("api") {
-			go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e)
+			go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
 		}
 
 		return e.Start(ctx)

+ 4 - 1
cmd/proxy.go

@@ -38,6 +38,9 @@ func Proxy() cli.Command {
 				Usage:  "Listening address",
 				EnvVar: "PROXYLISTEN",
 			},
+			&cli.BoolFlag{
+				Name: "debug",
+			},
 			&cli.IntFlag{
 				Name:   "interval",
 				Usage:  "proxy announce time interval",
@@ -71,7 +74,7 @@ func Proxy() cli.Command {
 				return err
 			}
 
-			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e)
+			return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
 		},
 	}
 }