⚙️ Attach pprof to API for debugging

This commit is contained in:
Ettore Di Giacinto
2022-02-20 16:08:24 +01:00
parent 719d7d125e
commit f51c7993ea
5 changed files with 19 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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())
}()

View File

@@ -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"))
},
}
}

View File

@@ -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)

View File

@@ -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"))
},
}
}