Skip to content

Commit

Permalink
Merge pull request #85 from ethpandaops/feat/pprof
Browse files Browse the repository at this point in the history
feat: pprof
  • Loading branch information
Savid authored Mar 2, 2023
2 parents ec305d4 + 75dedf7 commit 5ab84a1
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Discovery requires a single `yaml` config file. An example file can be found [he
| --- | --- | --- | --- |
| logging | string | `warn` | Log level (`panic`, `fatal`, `warn`, `info`, `debug`, `trace`) |
| metricsAddr | string | `:9090` | The address the metrics server will listen on |
| pprofAddr | string | | The address the [pprof](https://github.com/google/pprof) server will listen on. When ommited, the pprof server will not be started |
| coordinator | object | | [Coordinator](./server.md#coordinator) configuration |
| coordinator.address | string | | The address of the [xatu server](./server.md) |
| coordinator.maxQueueSize | int | `51200` | The maximum queue size to buffer node records for delayed processing. If the queue gets full it drops the node records |
Expand Down Expand Up @@ -78,6 +79,7 @@ P2P configuration to statically set discovery node records (boot nodes).
```yaml
logging: info
metricsAddr: :9090
pprofAddr: :6060

coordinator:
address: localhost:8080
Expand All @@ -98,6 +100,7 @@ p2p:
```yaml
logging: info
metricsAddr: :9090
pprofAddr: :6060

coordinator:
address: localhost:8080
Expand Down
2 changes: 2 additions & 0 deletions docs/mimicry.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Mimicry requires a single `yaml` config file. An example file can be found [here
| --- | --- | --- | --- |
| logging | string | `warn` | Log level (`panic`, `fatal`, `warn`, `info`, `debug`, `trace`) |
| metricsAddr | string | `:9090` | The address the metrics server will listen on |
| pprofAddr | string | | The address the [pprof](https://github.com/google/pprof) server will listen on. When ommited, the pprof server will not be started |
| name | string | | Unique name of the mimicry |
| labels | object | | A key value map of labels to append to every mimicry event |
| ntpServer | string | `pool.ntp.org` | NTP server to calculate clock drift for events |
Expand Down Expand Up @@ -194,6 +195,7 @@ outputs:
```yaml
logging: "debug"
metricsAddr: ":9090"
pprofAddr: ":6060"

name: example-instance

Expand Down
2 changes: 2 additions & 0 deletions docs/sentry.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Sentry requires a single `yaml` config file. An example file can be found [here]
| --- | --- | --- | --- |
| logging | string | `warn` | Log level (`panic`, `fatal`, `warn`, `info`, `debug`, `trace`) |
| metricsAddr | string | `:9090` | The address the metrics server will listen on |
| pprofAddr | string | | The address the [pprof](https://github.com/google/pprof) server will listen on. When ommited, the pprof server will not be started |
| name | string | | Unique name of the sentry |
| labels | object | | A key value map of labels to append to every sentry event |
| ethereum.beaconNodeAddress | string | | [Ethereum consensus client](https://ethereum.org/en/developers/docs/nodes-and-clients/#consensus-clients) http server endpoint |
Expand Down Expand Up @@ -140,6 +141,7 @@ outputs:
```yaml
logging: "debug"
metricsAddr: ":9090"
pprofAddr: ":6060"

name: example-instance

Expand Down
2 changes: 2 additions & 0 deletions docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Server requires a single `yaml` config file. An example file can be found [here]
| --- | --- | --- | --- |
| logging | string | `warn` | Log level (`panic`, `fatal`, `warn`, `info`, `debug`, `trace`) |
| metricsAddr | string | `:9090` | The address the metrics server will listen on |
| pprofAddr | string | | The address the [pprof](https://github.com/google/pprof) server will listen on. When ommited, the pprof server will not be started |
| addr | string | `:8080` | The grpc address for [services](#services) |
| labels | object | | A key value map of labels to append to every sentry event |
| ntpServer | string | `pool.ntp.org` | NTP server to calculate clock drift for events |
Expand Down Expand Up @@ -143,6 +144,7 @@ services:
logging: "info"
addr: ":8080"
metricsAddr: ":9090"
pprofAddr: ":6060"

labels:
ethpandaops: rocks
Expand Down
1 change: 1 addition & 0 deletions example_discovery.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
logging: "info" # panic,fatal,warn,info,debug,trace
metricsAddr: ":9090"
# pprofAddr: ":6060" # optional. if supplied it enables pprof server

coordinator:
address: localhost:8080
Expand Down
1 change: 1 addition & 0 deletions example_mimicry.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
logging: "debug" # panic,fatal,warn,info,debug,trace
metricsAddr: ":9090"
# pprofAddr: ":6060" # optional. if supplied it enables pprof server

name: example-instance

Expand Down
1 change: 1 addition & 0 deletions example_sentry.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
logging: "debug" # panic,fatal,warn,info,debug,trace
metricsAddr: ":9090"
# pprofAddr: ":6060" # optional. if supplied it enables pprof server

name: example-instance

Expand Down
1 change: 1 addition & 0 deletions example_server.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
logging: "info" # panic,fatal,warn,info,debug,trace
addr: ":8080"
metricsAddr: ":9090"
# pprofAddr: ":6060" # optional. if supplied it enables pprof server

labels:
ethpandaops: rocks
Expand Down
5 changes: 3 additions & 2 deletions pkg/discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type Config struct {
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
PProfAddr *string `yaml:"pprofAddr"`

// P2P configuration
P2P p2p.Config `yaml:"p2p"`
Expand Down
26 changes: 26 additions & 0 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"syscall"
"time"

//nolint:gosec // only exposed if pprofAddr config is set
_ "net/http/pprof"

"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethpandaops/xatu/pkg/discovery/cache"
"github.com/ethpandaops/xatu/pkg/discovery/coordinator"
Expand Down Expand Up @@ -75,6 +78,12 @@ func (d *Discovery) Start(ctx context.Context) error {
return err
}

if d.Config.PProfAddr != nil {
if err := d.ServePProf(ctx); err != nil {
return err
}
}

d.log.
WithField("version", xatu.Full()).
WithField("id", d.id.String()).
Expand Down Expand Up @@ -149,6 +158,23 @@ func (d *Discovery) ServeMetrics(ctx context.Context) error {
return nil
}

func (d *Discovery) ServePProf(ctx context.Context) error {
pprofServer := &http.Server{
Addr: *d.Config.PProfAddr,
ReadHeaderTimeout: 120 * time.Second,
}

go func() {
d.log.Infof("Serving pprof at %s", *d.Config.PProfAddr)

if err := pprofServer.ListenAndServe(); err != nil {
d.log.Fatal(err)
}
}()

return nil
}

func (d *Discovery) startCrons(ctx context.Context) error {
c := gocron.NewScheduler(time.Local)

Expand Down
5 changes: 3 additions & 2 deletions pkg/mimicry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Config struct {
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
PProfAddr *string `yaml:"pprofAddr"`

// The name of the mimicry
Name string `yaml:"name"`
Expand Down
26 changes: 26 additions & 0 deletions pkg/mimicry/mimicry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"syscall"
"time"

//nolint:gosec // only exposed if pprofAddr config is set
_ "net/http/pprof"

"github.com/beevik/ntp"
"github.com/ethpandaops/xatu/pkg/mimicry/coordinator"
"github.com/ethpandaops/xatu/pkg/mimicry/p2p/handler"
Expand Down Expand Up @@ -77,6 +80,12 @@ func (m *Mimicry) Start(ctx context.Context) error {
return err
}

if m.Config.PProfAddr != nil {
if err := m.ServePProf(ctx); err != nil {
return err
}
}

m.log.
WithField("version", xatu.Full()).
WithField("id", m.id.String()).
Expand Down Expand Up @@ -132,6 +141,23 @@ func (m *Mimicry) ServeMetrics(ctx context.Context) error {
return nil
}

func (m *Mimicry) ServePProf(ctx context.Context) error {
pprofServer := &http.Server{
Addr: *m.Config.PProfAddr,
ReadHeaderTimeout: 120 * time.Second,
}

go func() {
m.log.Infof("Serving pprof at %s", *m.Config.PProfAddr)

if err := pprofServer.ListenAndServe(); err != nil {
m.log.Fatal(err)
}
}()

return nil
}

func (m *Mimicry) createNewClientMeta(ctx context.Context) (*xatu.ClientMeta, error) {
return &xatu.ClientMeta{
Name: m.Config.Name,
Expand Down
5 changes: 3 additions & 2 deletions pkg/sentry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Config struct {
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
LoggingLevel string `yaml:"logging" default:"info"`
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
PProfAddr *string `yaml:"pprofAddr"`

// The name of the sentry
Name string `yaml:"name"`
Expand Down
26 changes: 26 additions & 0 deletions pkg/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"syscall"
"time"

//nolint:gosec // only exposed if pprofAddr config is set
_ "net/http/pprof"

eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/phase0"
Expand Down Expand Up @@ -85,6 +88,12 @@ func (s *Sentry) Start(ctx context.Context) error {
return err
}

if s.Config.PProfAddr != nil {
if err := s.ServePProf(ctx); err != nil {
return err
}
}

s.log.
WithField("version", xatu.Full()).
WithField("id", s.id.String()).
Expand Down Expand Up @@ -284,6 +293,23 @@ func (s *Sentry) ServeMetrics(ctx context.Context) error {
return nil
}

func (s *Sentry) ServePProf(ctx context.Context) error {
pprofServer := &http.Server{
Addr: *s.Config.PProfAddr,
ReadHeaderTimeout: 120 * time.Second,
}

go func() {
s.log.Infof("Serving pprof at %s", *s.Config.PProfAddr)

if err := pprofServer.ListenAndServe(); err != nil {
s.log.Fatal(err)
}
}()

return nil
}

func (s *Sentry) createNewClientMeta(ctx context.Context) (*xatu.ClientMeta, error) {
var networkMeta *xatu.ClientMeta_Ethereum_Network

Expand Down
2 changes: 2 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Config struct {
Addr string `yaml:"addr" default:":8080"`
// MetricsAddr is the address to listen on for metrics.
MetricsAddr string `yaml:"metricsAddr" default:":9090"`
// PProfAddr is the address to listen on for pprof.
PProfAddr *string `yaml:"pprofAddr"`
// LoggingLevel is the logging level to use.
LoggingLevel string `yaml:"logging" default:"info"`

Expand Down
30 changes: 30 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"syscall"
"time"

//nolint:gosec // only exposed if pprofAddr config is set
_ "net/http/pprof"

"github.com/beevik/ntp"
"github.com/ethpandaops/xatu/pkg/server/geoip"
"github.com/ethpandaops/xatu/pkg/server/persistence"
Expand All @@ -34,6 +37,7 @@ type Xatu struct {

grpcServer *grpc.Server
metricsServer *http.Server
pprofServer *http.Server

persistence *persistence.Client
cache store.Cache
Expand Down Expand Up @@ -124,6 +128,15 @@ func (x *Xatu) Start(ctx context.Context) error {

return nil
})
g.Go(func() error {
if err := x.startPProf(ctx); err != nil {
if err != http.ErrServerClosed {
return err
}
}

return nil
})
g.Go(func() error {
if err := x.startGrpcServer(ctx); err != nil {
return err
Expand Down Expand Up @@ -178,6 +191,12 @@ func (x *Xatu) stop(ctx context.Context) error {
}
}

if x.pprofServer != nil {
if err := x.pprofServer.Shutdown(ctx); err != nil {
return err
}
}

if x.metricsServer != nil {
if err := x.metricsServer.Shutdown(ctx); err != nil {
return err
Expand Down Expand Up @@ -235,6 +254,17 @@ func (x *Xatu) startMetrics(ctx context.Context) error {
return x.metricsServer.ListenAndServe()
}

func (x *Xatu) startPProf(ctx context.Context) error {
x.log.WithField("addr", x.config.PProfAddr).Info("Starting pprof server")

x.pprofServer = &http.Server{
Addr: *x.config.PProfAddr,
ReadHeaderTimeout: 120 * time.Second,
}

return x.pprofServer.ListenAndServe()
}

func (x *Xatu) startCrons(ctx context.Context) error {
c := gocron.NewScheduler(time.Local)

Expand Down

0 comments on commit 5ab84a1

Please sign in to comment.