Skip to content

Commit f2b6a73

Browse files
committed
add pprof to dmsg-disc and dmsg-server
1 parent 5286345 commit f2b6a73

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

cmd/dmsg-discovery/commands/dmsg-discovery.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net"
1010
"net/http"
11+
"net/http/pprof"
1112
"os"
1213
"path/filepath"
1314
"strings"
@@ -48,12 +49,14 @@ var (
4849
authPassphrase string
4950
officialServers string
5051
dmsgServerType string
52+
pprofAddr string
5153
)
5254

5355
func init() {
5456
sf.Init(RootCmd, "dmsg_disc", "")
5557

5658
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9090", "address to bind to")
59+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
5760
RootCmd.Flags().StringVar(&authPassphrase, "auth", "", "auth passphrase as simple auth for official dmsg servers registration")
5861
RootCmd.Flags().StringVar(&officialServers, "official-servers", "", "list of official dmsg servers keys separated by comma")
5962
RootCmd.Flags().StringVar(&redisURL, "redis", store.DefaultURL, "connections string for a redis store")
@@ -98,6 +101,39 @@ skywire dmsg disc --sk $(tail -n1 dmsgd-config.json)`,
98101
log.WithError(err).Warn("No SecKey found. Skipping serving on dmsghttp.")
99102
}
100103

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

103139
ctx, cancel := cmdutil.SignalContext(context.Background(), log)

cmd/dmsg-server/commands/start/root.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"io"
88
"log"
99
"net/http"
10+
"net/http/pprof"
1011
"net/url"
1112
"os"
1213
"strconv"
14+
"time"
1315

1416
chi "github.com/go-chi/chi/v5"
1517
"github.com/go-chi/chi/v5/middleware"
@@ -29,10 +31,12 @@ import (
2931
var (
3032
sf cmdutil.ServiceFlags
3133
authPassphrase string
34+
pprofAddr string
3235
)
3336

3437
func init() {
3538
sf.Init(RootCmd, "dmsg_srv", dmsgserver.DefaultConfigPath)
39+
RootCmd.Flags().StringVar(&pprofAddr, "pprof", "", "address to bind pprof debug server (e.g. localhost:6060)\033[0m")
3640
RootCmd.Flags().StringVar(&authPassphrase, "auth", "", "auth passphrase as simple auth for official dmsg servers registration")
3741
}
3842

@@ -59,6 +63,39 @@ var RootCmd = &cobra.Command{
5963
}
6064
logging.SetLevel(logLvl)
6165

66+
if pprofAddr != "" {
67+
pprofMux := http.NewServeMux()
68+
69+
// Register the index (which links to everything else)
70+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
71+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
72+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
73+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
74+
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
75+
76+
// Register profile handlers using pprof.Handler
77+
for _, profile := range []string{"heap", "goroutine", "threadcreate", "block", "mutex", "allocs"} {
78+
pprofMux.Handle("/debug/pprof/"+profile, pprof.Handler(profile))
79+
}
80+
81+
go func() {
82+
log.Infof("Starting pprof server on %s", pprofAddr)
83+
server := &http.Server{
84+
Addr: pprofAddr,
85+
Handler: pprofMux,
86+
ReadHeaderTimeout: 10 * time.Second,
87+
ReadTimeout: 30 * time.Second,
88+
WriteTimeout: 30 * time.Second,
89+
IdleTimeout: 60 * time.Second,
90+
}
91+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
92+
log.Errorf("pprof server failed: %v", err)
93+
}
94+
}()
95+
96+
time.Sleep(100 * time.Millisecond)
97+
}
98+
6299
if conf.HTTPAddress == "" {
63100
u, err := url.Parse(conf.LocalAddress)
64101
if err != nil {

0 commit comments

Comments
 (0)