|
8 | 8 | "log" |
9 | 9 | "net" |
10 | 10 | "net/http" |
| 11 | + "net/http/pprof" |
11 | 12 | "os" |
12 | 13 | "path/filepath" |
13 | 14 | "strings" |
@@ -48,12 +49,14 @@ var ( |
48 | 49 | authPassphrase string |
49 | 50 | officialServers string |
50 | 51 | dmsgServerType string |
| 52 | + pprofAddr string |
51 | 53 | ) |
52 | 54 |
|
53 | 55 | func init() { |
54 | 56 | sf.Init(RootCmd, "dmsg_disc", "") |
55 | 57 |
|
56 | 58 | 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") |
57 | 60 | RootCmd.Flags().StringVar(&authPassphrase, "auth", "", "auth passphrase as simple auth for official dmsg servers registration") |
58 | 61 | RootCmd.Flags().StringVar(&officialServers, "official-servers", "", "list of official dmsg servers keys separated by comma") |
59 | 62 | 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)`, |
98 | 101 | log.WithError(err).Warn("No SecKey found. Skipping serving on dmsghttp.") |
99 | 102 | } |
100 | 103 |
|
| 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 | + |
101 | 137 | metricsutil.ServeHTTPMetrics(log, sf.MetricsAddr) |
102 | 138 |
|
103 | 139 | ctx, cancel := cmdutil.SignalContext(context.Background(), log) |
|
0 commit comments