Skip to content

Commit 3003fb5

Browse files
committed
feat: enable pprof
1 parent 46b5fb4 commit 3003fb5

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

cmd/serve.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
)
1313

1414
const (
15-
categoryChaos = "chaos"
1615
categoryAuthrpc = "authrpc"
16+
categoryChaos = "chaos"
1717
categoryFlashblocks = "flashblocks"
18-
categoryRPC = "rpc"
1918
categoryMetrics = "metrics"
19+
categoryPprof = "pprof"
20+
categoryRPC = "rpc"
2021
)
2122

2223
func CommandServe(cfg *config.Config) *cli.Command {
@@ -542,11 +543,22 @@ func CommandServe(cfg *config.Config) *cli.Command {
542543
},
543544
}
544545

546+
pprofFlags := []cli.Flag{ // --pprof-xxx
547+
&cli.StringFlag{ // --pprof-listen-address
548+
Category: strings.ToUpper(categoryPprof),
549+
Destination: &cfg.Pprof.ListenAddress,
550+
EnvVars: []string{envPrefix + strings.ToUpper(categoryPprof) + "_LISTEN_ADDRESS"},
551+
Name: categoryPprof + "-listen-address",
552+
Usage: "`host:port` for pprof server",
553+
},
554+
}
555+
545556
flags := slices.Concat(
546557
authrpcFlags,
547558
flashblocksFlags,
548-
rpcFlags,
549559
metricsFlags,
560+
pprofFlags,
561+
rpcFlags,
550562
)
551563

552564
return &cli.Command{

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ type Config struct {
1212

1313
Log *Log `yaml:"log"`
1414
Metrics *Metrics `yaml:"metrics"`
15+
Pprof *Pprof `yaml:"pprof"`
1516
Version string `yaml:"-"`
1617
}
1718

1819
func New(version string) *Config {
1920
return &Config{
2021
Log: &Log{},
2122
Metrics: &Metrics{},
23+
Pprof: &Pprof{},
2224
Version: version,
2325

2426
Authrpc: &Authrpc{HttpProxy: &HttpProxy{

config/pprof.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package config
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net"
7+
)
8+
9+
var (
10+
errPprofInvalidListenAddress = errors.New("invalid pprof listen address")
11+
)
12+
13+
type Pprof struct {
14+
ListenAddress string `yaml:"listen_address"`
15+
}
16+
17+
func (cfg *Pprof) Pprof() error {
18+
if cfg.ListenAddress != "" {
19+
if _, err := net.ResolveTCPAddr("tcp", cfg.ListenAddress); err != nil {
20+
return fmt.Errorf("%w: %s: %w",
21+
errPprofInvalidListenAddress, cfg.ListenAddress, err,
22+
)
23+
}
24+
}
25+
26+
return nil
27+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ OPTIONS:
8484
8585
--metrics-listen-address host:port host:port for metrics server (default: "0.0.0.0:6785") [$BPROXY_METRICS_LISTEN_ADDRESS]
8686
87+
PPROF
88+
89+
--pprof-listen-address host:port host:port for pprof server [$BPROXY_PPROF_LISTEN_ADDRESS]
90+
8791
RPC
8892
8993
--rpc-backend url url of rpc backend (default: "http://127.0.0.1:18545") [$BPROXY_RPC_BACKEND]

server/server.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"net/http"
7+
"net/http/pprof"
78
"os"
89
"os/signal"
910
"syscall"
@@ -64,17 +65,31 @@ func New(cfg *config.Config) (*Server, error) {
6465
s.rpc = rpc
6566
}
6667

67-
mux := http.NewServeMux()
68-
mux.Handle("/", promhttp.Handler())
69-
mux.Handle("/metrics", promhttp.Handler())
70-
71-
s.metrics = &http.Server{
72-
Addr: cfg.Metrics.ListenAddress,
73-
Handler: mux,
74-
MaxHeaderBytes: 1024,
75-
ReadHeaderTimeout: 30 * time.Second,
76-
ReadTimeout: 30 * time.Second,
77-
WriteTimeout: 30 * time.Second,
68+
{
69+
mux := http.NewServeMux()
70+
mux.Handle("/", promhttp.Handler())
71+
mux.Handle("/metrics", promhttp.Handler())
72+
73+
s.metrics = &http.Server{
74+
Addr: cfg.Metrics.ListenAddress,
75+
Handler: mux,
76+
MaxHeaderBytes: 1024,
77+
ReadHeaderTimeout: 30 * time.Second,
78+
ReadTimeout: 30 * time.Second,
79+
WriteTimeout: 30 * time.Second,
80+
}
81+
}
82+
83+
if cfg.Pprof.ListenAddress != "" {
84+
go func() {
85+
mux := http.NewServeMux()
86+
mux.HandleFunc("/debug/pprof/", pprof.Index)
87+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
88+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
89+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
90+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
91+
_ = http.ListenAndServe(cfg.Pprof.ListenAddress, mux)
92+
}()
7893
}
7994

8095
return s, nil

0 commit comments

Comments
 (0)