Skip to content

Commit 02dfc91

Browse files
authored
Merge pull request #2090 from mrpalide/feat/running-pprof-on-all-services
Add pprof to all services
2 parents c1912d9 + 1631505 commit 02dfc91

File tree

17 files changed

+332
-27
lines changed

17 files changed

+332
-27
lines changed

cmd/address-resolver/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var (
5757
func init() {
5858
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9093", "address to bind to\033[0m")
5959
RootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to\033[0m")
60-
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m") // ADD THIS
60+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
6161
RootCmd.Flags().StringVar(&redisURL, "redis", "redis://localhost:6379", "connections string for a redis store\033[0m")
6262
RootCmd.Flags().IntVar(&redisPoolSize, "redis-pool-size", 10, "redis connection pool size\033[0m")
6363
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "info", "[info|error|warn|debug|trace|panic]\033[0m")

cmd/config-bootstrapper/commands/root.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import (
77
"fmt"
88
"io"
99
"log"
10+
"net/http"
11+
"net/http/pprof"
1012
"os"
1113
"path/filepath"
1214
"strings"
15+
"time"
1316

1417
"github.com/skycoin/dmsg/pkg/direct"
1518
"github.com/skycoin/dmsg/pkg/dmsg"
@@ -34,10 +37,12 @@ var (
3437
sk cipher.SecKey
3538
dmsgPort uint16
3639
dmsgServerType string
40+
pprofAddr string
3741
)
3842

3943
func init() {
4044
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9082", "address to bind to\033[0m")
45+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
4146
RootCmd.Flags().StringVar(&tag, "tag", "address_resolver", "logging tag\033[0m")
4247
RootCmd.Flags().StringVarP(&stunPath, "config", "c", "./config.json", "stun server list file location\033[0m")
4348
RootCmd.Flags().StringVarP(&domain, "domain", "d", "skywire.skycoin.com", "the domain of the endpoints\033[0m")
@@ -65,6 +70,40 @@ var RootCmd = &cobra.Command{
6570
}
6671

6772
logger := logging.MustGetLogger(tag)
73+
74+
if pprofAddr != "" {
75+
pprofMux := http.NewServeMux()
76+
77+
// Register the index (which links to everything else)
78+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
79+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
80+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
81+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
82+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
83+
84+
// Register profile handlers using pprof.Handler
85+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
86+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
87+
}
88+
89+
go func() {
90+
logger.Infof("Starting pprof server on %s", pprofAddr)
91+
server := &http.Server{
92+
Addr: pprofAddr,
93+
Handler: pprofMux,
94+
ReadHeaderTimeout: 10 * time.Second,
95+
ReadTimeout: 30 * time.Second,
96+
WriteTimeout: 30 * time.Second,
97+
IdleTimeout: 60 * time.Second,
98+
}
99+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
100+
logger.Errorf("pprof server failed: %v", err)
101+
}
102+
}()
103+
104+
time.Sleep(100 * time.Millisecond)
105+
}
106+
68107
config := readConfig(logger, stunPath)
69108

70109
pk, err := sk.PubKey()

cmd/geoip/commands/root.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"net"
1111
"net/http"
12+
"net/http/pprof"
1213
"net/netip"
1314
"os"
1415
"os/signal"
@@ -44,15 +45,17 @@ type lookupResult struct {
4445
}
4546

4647
var (
47-
addr string
48-
tag string
49-
dbPath string
50-
apiMode bool
51-
logLvl string
48+
addr string
49+
tag string
50+
dbPath string
51+
apiMode bool
52+
logLvl string
53+
pprofAddr string
5254
)
5355

5456
func init() {
5557
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":8080", "address to bind to\033[0m")
58+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
5659
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "info", "[info|error|warn|debug|trace|panic]\033[0m")
5760
RootCmd.Flags().StringVar(&tag, "tag", "geoip", "logging tag\033[0m")
5861
RootCmd.Flags().StringVar(&dbPath, "db", "", "Path to GeoLite2-City.mmdb database")
@@ -86,6 +89,39 @@ skywire svc geoip --api --addr ":9093" --db ./GeoLite2-City.mmdb
8689

8790
logging.SetLevel(lvl)
8891

92+
if pprofAddr != "" {
93+
pprofMux := http.NewServeMux()
94+
95+
// Register the index (which links to everything else)
96+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
97+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
98+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
99+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
100+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
101+
102+
// Register profile handlers using pprof.Handler
103+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
104+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
105+
}
106+
107+
go func() {
108+
logger.Infof("Starting pprof server on %s", pprofAddr)
109+
server := &http.Server{
110+
Addr: pprofAddr,
111+
Handler: pprofMux,
112+
ReadHeaderTimeout: 10 * time.Second,
113+
ReadTimeout: 30 * time.Second,
114+
WriteTimeout: 30 * time.Second,
115+
IdleTimeout: 60 * time.Second,
116+
}
117+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
118+
logger.Errorf("pprof server failed: %v", err)
119+
}
120+
}()
121+
122+
time.Sleep(100 * time.Millisecond)
123+
}
124+
89125
var db *geoip2.Reader
90126

91127
if dbPath != "" {

cmd/network-monitor/commands/root.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"context"
66
"fmt"
77
"log"
8+
"net/http"
9+
"net/http/pprof"
810
"os"
911
"path/filepath"
1012
"strings"
13+
"time"
1114

1215
"github.com/spf13/cobra"
1316

@@ -34,10 +37,12 @@ var (
3437
addr string
3538
tag string
3639
logLvl string
40+
pprofAddr string
3741
)
3842

3943
func init() {
4044
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9080", "address to bind to.\033[0m")
45+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
4146
RootCmd.Flags().StringVar(&sdURL, "sd-url", "http://sd.skycoin.com", "url to service discovery\033[0m")
4247
RootCmd.Flags().StringVar(&arURL, "ar-url", "http://ar.skywire.skycoin.com", "url to address resolver\033[0m")
4348
RootCmd.Flags().StringVar(&utURL, "ut-url", "http://ut.skywire.skycoin.com", "url to uptime tracker visor data.\033[0m")
@@ -84,6 +89,39 @@ var RootCmd = &cobra.Command{
8489

8590
logging.SetLevel(lvl)
8691

92+
if pprofAddr != "" {
93+
pprofMux := http.NewServeMux()
94+
95+
// Register the index (which links to everything else)
96+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
97+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
98+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
99+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
100+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
101+
102+
// Register profile handlers using pprof.Handler
103+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
104+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
105+
}
106+
107+
go func() {
108+
mLogger.Infof("Starting pprof server on %s", pprofAddr)
109+
server := &http.Server{
110+
Addr: pprofAddr,
111+
Handler: pprofMux,
112+
ReadHeaderTimeout: 10 * time.Second,
113+
ReadTimeout: 30 * time.Second,
114+
WriteTimeout: 30 * time.Second,
115+
IdleTimeout: 60 * time.Second,
116+
}
117+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
118+
mLogger.Errorf("pprof server failed: %v", err)
119+
}
120+
}()
121+
122+
time.Sleep(100 * time.Millisecond)
123+
}
124+
87125
var srvURLs api.ServicesURLs
88126
srvURLs.SD = sdURL
89127
srvURLs.AR = arURL

cmd/route-finder/commands/root.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"fmt"
77
"log"
8+
"net/http"
9+
"net/http/pprof"
810
"os"
911
"path/filepath"
1012
"strings"
@@ -43,11 +45,13 @@ var (
4345
dmsgServerType string
4446
pgMaxOpenConn int
4547
multiplexingLib string
48+
pprofAddr string
4649
)
4750

4851
func init() {
4952
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9092", "address to bind to\033[0m")
5053
RootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to\033[0m")
54+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
5155
RootCmd.Flags().StringVar(&pgHost, "pg-host", "localhost", "host of postgres\033[0m")
5256
RootCmd.Flags().StringVar(&pgPort, "pg-port", "5432", "port of postgres\033[0m")
5357
RootCmd.Flags().IntVar(&pgMaxOpenConn, "pg-max-open-conn", 60, "maximum open connection of db\033[0m")
@@ -92,6 +96,39 @@ PG_USER="postgres" PG_DATABASE="rf" PG_PASSWORD="" route-finder --addr ":9092"
9296

9397
logging.SetLevel(lvl)
9498

99+
if pprofAddr != "" {
100+
pprofMux := http.NewServeMux()
101+
102+
// Register the index (which links to everything else)
103+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
104+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
105+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
106+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
107+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
108+
109+
// Register profile handlers using pprof.Handler
110+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
111+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
112+
}
113+
114+
go func() {
115+
logger.Infof("Starting pprof server on %s", pprofAddr)
116+
server := &http.Server{
117+
Addr: pprofAddr,
118+
Handler: pprofMux,
119+
ReadHeaderTimeout: 10 * time.Second,
120+
ReadTimeout: 30 * time.Second,
121+
WriteTimeout: 30 * time.Second,
122+
IdleTimeout: 60 * time.Second,
123+
}
124+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
125+
logger.Errorf("pprof server failed: %v", err)
126+
}
127+
}()
128+
129+
time.Sleep(100 * time.Millisecond)
130+
}
131+
95132
var gormDB *gorm.DB
96133

97134
if !testing {

cmd/service-discovery/commands/root.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package commands
44
import (
55
"context"
66
"fmt"
7+
"net/http"
8+
"net/http/pprof"
79
"os"
810
"path/filepath"
911
"strings"
@@ -49,11 +51,13 @@ var (
4951
dmsgPort uint16
5052
dmsgServerType string
5153
geoipURL string
54+
pprofAddr string
5255
)
5356

5457
func init() {
5558
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9098", "address to bind to\033[0m")
5659
RootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to\033[0m")
60+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
5761
RootCmd.Flags().StringVarP(&redisURL, "redis", "r", "redis://localhost:6379", "connections string for a redis store\033[0m")
5862
RootCmd.Flags().StringVarP(&pgHost, "pg-host", "o", "localhost", "host of postgres\033[0m")
5963
RootCmd.Flags().StringVarP(&pgPort, "pg-port", "p", "5432", "port of postgres\033[0m")
@@ -94,6 +98,39 @@ PG_USER="postgres" PG_DATABASE="sd" PG_PASSWORD="" service-discovery --sk $(tail
9498
ctx, cancel := cmdutil.SignalContext(context.Background(), log)
9599
defer cancel()
96100

101+
if pprofAddr != "" {
102+
pprofMux := http.NewServeMux()
103+
104+
// Register the index (which links to everything else)
105+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
106+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
107+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
108+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
109+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
110+
111+
// Register profile handlers using pprof.Handler
112+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
113+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
114+
}
115+
116+
go func() {
117+
log.Infof("Starting pprof server on %s", pprofAddr)
118+
server := &http.Server{
119+
Addr: pprofAddr,
120+
Handler: pprofMux,
121+
ReadHeaderTimeout: 10 * time.Second,
122+
ReadTimeout: 30 * time.Second,
123+
WriteTimeout: 30 * time.Second,
124+
IdleTimeout: 60 * time.Second,
125+
}
126+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
127+
log.Errorf("pprof server failed: %v", err)
128+
}
129+
}()
130+
131+
time.Sleep(100 * time.Millisecond)
132+
}
133+
97134
var gormDB *gorm.DB
98135

99136
pgUser, pgPassword, pgDatabase := storeconfig.PostgresCredential()

0 commit comments

Comments
 (0)