Skip to content

Commit

Permalink
Add pprof http server to daemons (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing authored May 2, 2024
1 parent 18a315d commit 29f116f
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ var (
Help: "address and port bfgd prometheus listens on",
Print: config.PrintAll,
},
"BFG_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port bfgd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
}
)

Expand Down
6 changes: 6 additions & 0 deletions cmd/bssd/bssd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var (
Help: "address and port bssd prometheus listens on",
Print: config.PrintAll,
},
"BSS_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port bssd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
}
)

Expand Down
8 changes: 7 additions & 1 deletion cmd/popmd/popmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ var (
"POPM_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port bssd prometheus listens on",
Help: "address and port popm prometheus listens on",
Print: config.PrintAll,
},
"POPM_PPROF_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port popm pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"POPM_REMINE_THRESHOLD": config.Config{
Expand Down
6 changes: 6 additions & 0 deletions cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ var (
Help: "address and port tbcd prometheus listens on",
Print: config.PrintAll,
},
"TBC_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port tbcd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
}
)

Expand Down
23 changes: 22 additions & 1 deletion service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/hemilabs/heminetwork/hemi/electrumx"
"github.com/hemilabs/heminetwork/hemi/pop"
"github.com/hemilabs/heminetwork/service/deucalion"
"github.com/hemilabs/heminetwork/service/pprof"
)

// XXX this code needs to be a bit smarter when syncing bitcoin. We should
Expand Down Expand Up @@ -104,6 +105,7 @@ type Config struct {
LogLevel string
PgURI string
PrometheusListenAddress string
PprofListenAddress string
PublicKeyAuth bool
}

Expand Down Expand Up @@ -1443,13 +1445,32 @@ func (s *Server) Run(pctx context.Context) error {
log.Infof("public RPC Server shutdown cleanly")
}()

// pprof
if s.cfg.PprofListenAddress != "" {
p, err := pprof.NewServer(&pprof.Config{
ListenAddress: s.cfg.PprofListenAddress,
})
if err != nil {
return fmt.Errorf("create pprof server: %w", err)
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := p.Run(ctx); !errors.Is(err, context.Canceled) {
log.Errorf("pprof server terminated with error: %v", err)
return
}
log.Infof("pprof server clean shutdown")
}()
}

// Prometheus
if s.cfg.PrometheusListenAddress != "" {
d, err := deucalion.New(&deucalion.Config{
ListenAddress: s.cfg.PrometheusListenAddress,
})
if err != nil {
return fmt.Errorf("create server: %w", err)
return fmt.Errorf("create prometheus server: %w", err)
}
cs := []prometheus.Collector{
s.cmdsProcessed, // XXX should we make two counters? priv/pub
Expand Down
21 changes: 21 additions & 0 deletions service/bss/bss.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hemilabs/heminetwork/ethereum"
"github.com/hemilabs/heminetwork/hemi"
"github.com/hemilabs/heminetwork/service/deucalion"
"github.com/hemilabs/heminetwork/service/pprof"
)

const (
Expand Down Expand Up @@ -61,6 +62,7 @@ type Config struct {
ListenAddress string
LogLevel string
PrometheusListenAddress string
PprofListenAddress string
}

type Server struct {
Expand Down Expand Up @@ -746,6 +748,25 @@ func (s *Server) Run(parrentCtx context.Context) error {
s.wg.Add(1)
go s.bfg(ctx) // Attempt to talk to bfg

// pprof
if s.cfg.PprofListenAddress != "" {
p, err := pprof.NewServer(&pprof.Config{
ListenAddress: s.cfg.PprofListenAddress,
})
if err != nil {
return fmt.Errorf("create pprof server: %w", err)
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := p.Run(ctx); !errors.Is(err, context.Canceled) {
log.Errorf("pprof server terminated with error: %v", err)
return
}
log.Infof("pprof server clean shutdown")
}()
}

// Prometheus
if s.cfg.PrometheusListenAddress != "" {
d, err := deucalion.New(&deucalion.Config{
Expand Down
22 changes: 22 additions & 0 deletions service/popm/popm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/hemilabs/heminetwork/bitcoin"
"github.com/hemilabs/heminetwork/hemi"
"github.com/hemilabs/heminetwork/hemi/pop"
"github.com/hemilabs/heminetwork/service/pprof"
)

// XXX we should debate if we can make pop miner fully transient. It feels like
Expand Down Expand Up @@ -72,6 +73,8 @@ type Config struct {

PrometheusListenAddress string

PprofListenAddress string

RetryMineThreshold uint

StaticFee uint
Expand Down Expand Up @@ -847,6 +850,25 @@ func (m *Miner) Run(pctx context.Context) error {
}
}

// pprof
if m.cfg.PprofListenAddress != "" {
p, err := pprof.NewServer(&pprof.Config{
ListenAddress: m.cfg.PprofListenAddress,
})
if err != nil {
return fmt.Errorf("create pprof server: %w", err)
}
m.wg.Add(1)
go func() {
defer m.wg.Done()
if err := p.Run(ctx); !errors.Is(err, context.Canceled) {
log.Errorf("pprof server terminated with error: %v", err)
return
}
log.Infof("pprof server clean shutdown")
}()
}

log.Infof("Starting PoP miner with BTC address %v (public key %x)",
m.btcAddress.EncodeAddress(), m.btcPublicKey.SerializeCompressed())

Expand Down
73 changes: 73 additions & 0 deletions service/pprof/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

package pprof

import (
"context"
"errors"
"net"
"net/http"
"net/http/pprof"
"sync/atomic"

"github.com/juju/loggo"
)

var log = loggo.GetLogger("pprof")

type Config struct {
ListenAddress string
}

type Server struct {
cfg *Config
running atomic.Bool
}

func NewServer(cfg *Config) (*Server, error) {
return &Server{cfg: cfg}, nil
}

func (s *Server) Run(ctx context.Context) error {
if !s.running.CompareAndSwap(false, true) {
return errors.New("already running")
}
defer s.running.CompareAndSwap(true, false)

if s.cfg.ListenAddress == "" {
return errors.New("listen address is required")
}

pprofMux := http.NewServeMux()
pprofMux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
pprofMux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
pprofMux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
pprofMux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
pprofMux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))

pprofHttpServer := &http.Server{
Addr: s.cfg.ListenAddress,
Handler: pprofMux,
BaseContext: func(net.Listener) context.Context { return ctx },
}

httpErrCh := make(chan error)
go func() {
log.Infof("pprof listening: %s", s.cfg.ListenAddress)
httpErrCh <- pprofHttpServer.ListenAndServe()
}()
defer func() {
if err := pprofHttpServer.Shutdown(ctx); err != nil {
log.Errorf("pprof http server exit: %v", err)
}
}()

select {
case <-ctx.Done():
return ctx.Err()
case err := <-httpErrCh:
return err
}
}
21 changes: 21 additions & 0 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/hemilabs/heminetwork/database/tbcd"
"github.com/hemilabs/heminetwork/database/tbcd/level"
"github.com/hemilabs/heminetwork/service/deucalion"
"github.com/hemilabs/heminetwork/service/pprof"
"github.com/hemilabs/heminetwork/ttl"
)

Expand Down Expand Up @@ -162,6 +163,7 @@ type Config struct {
MaxCachedTxs int
Network string
PrometheusListenAddress string
PprofListenAddress string
}

func NewDefaultConfig() *Config {
Expand Down Expand Up @@ -1730,6 +1732,25 @@ func (s *Server) Run(pctx context.Context) error {
log.Infof("RPC server shutdown cleanly")
}()

// pprof
if s.cfg.PprofListenAddress != "" {
p, err := pprof.NewServer(&pprof.Config{
ListenAddress: s.cfg.PprofListenAddress,
})
if err != nil {
return fmt.Errorf("create pprof server: %w", err)
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := p.Run(ctx); !errors.Is(err, context.Canceled) {
log.Errorf("pprof server terminated with error: %v", err)
return
}
log.Infof("pprof server clean shutdown")
}()
}

// Prometheus
if s.cfg.PrometheusListenAddress != "" {
d, err := deucalion.New(&deucalion.Config{
Expand Down

0 comments on commit 29f116f

Please sign in to comment.