Skip to content

Commit

Permalink
add pprof (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris authored Jun 11, 2024
1 parent c4ef2a4 commit e315e3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/httpserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ var flags []cli.Flag = []cli.Flag{
Value: "your-project",
Usage: "add 'service' tag to logs",
},
&cli.BoolFlag{
Name: "pprof",
Value: false,
Usage: "enable pprof debug endpoint",
},
&cli.Int64Flag{
Name: "drain-seconds",
Value: 45,
Expand All @@ -63,6 +68,7 @@ func main() {
logDebug := cCtx.Bool("log-debug")
logUID := cCtx.Bool("log-uid")
logService := cCtx.String("log-service")
enablePprof := cCtx.Bool("pprof")
drainDuration := time.Duration(cCtx.Int64("drain-seconds")) * time.Second

log := common.SetupLogger(&common.LoggingOpts{
Expand All @@ -81,6 +87,7 @@ func main() {
ListenAddr: listenAddr,
MetricsAddr: metricsAddr,
Log: log,
EnablePprof: enablePprof,

DrainDuration: drainDuration,
GracefulShutdownDuration: 30 * time.Second,
Expand Down
7 changes: 7 additions & 0 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"github.com/flashbots/go-template/metrics"
"github.com/flashbots/go-utils/httplogger"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/atomic"
)

type HTTPServerConfig struct {
ListenAddr string
MetricsAddr string
EnablePprof bool
Log *slog.Logger

DrainDuration time.Duration
Expand Down Expand Up @@ -55,6 +57,11 @@ func New(cfg *HTTPServerConfig) (srv *Server, err error) {
mux.With(srv.httpLogger).Get("/drain", srv.handleDrain)
mux.With(srv.httpLogger).Get("/undrain", srv.handleUndrain)

if cfg.EnablePprof {
srv.log.Info("pprof API enabled")
mux.Mount("/debug", middleware.Profiler())
}

srv.srv = &http.Server{
Addr: cfg.ListenAddr,
Handler: mux,
Expand Down

0 comments on commit e315e3c

Please # to comment.