Skip to content

Commit

Permalink
feat(debug): optional pprof
Browse files Browse the repository at this point in the history
  • Loading branch information
kpetremann committed Oct 22, 2024
1 parent 23228f9 commit e93ba62
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/data-aggregation-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func run() error {
triggerNewBuild := dispatchSingleRequest(newBuildRequest)

go job.StartBuildLoop(&deviceRepo, &reports, triggerNewBuild)
if err := router.NewManager(&deviceRepo, &reports, newBuildRequest).ListenAndServe(ctx, config.Cfg.API.ListenAddress, config.Cfg.API.ListenPort); err != nil {
if err := router.NewManager(&deviceRepo, &reports, newBuildRequest).ListenAndServe(ctx, config.Cfg.API.ListenAddress, config.Cfg.API.ListenPort, config.Cfg.Debug.Pprof.Enabled); err != nil {
return fmt.Errorf("webserver error: %w", err)
}

Expand Down
13 changes: 12 additions & 1 deletion internal/api/router/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewManager(deviceRepo DevicesRepository, reports *report.Repository, restar
}

// ListenAndServe starts to serve Web API requests.
func (m *Manager) ListenAndServe(ctx context.Context, address string, port int) error {
func (m *Manager) ListenAndServe(ctx context.Context, address string, port int, enablepprof bool) error {
defer func() {
close(m.newBuildRequest)
log.Warn().Msg("Shutdown.")
Expand All @@ -67,6 +68,16 @@ func (m *Manager) ListenAndServe(ctx context.Context, address string, port int)
router.GET("/v1/report/last/successful", withAuth.Wrap(m.getLastSuccessfulReport))
router.POST("/v1/build/trigger", withAuth.Wrap(m.triggerBuild))

if enablepprof {
router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/allocs", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/goroutine", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/heap", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
}

listenSocket := fmt.Sprint(address, ":", port)
log.Info().Msgf("Start webserver - listening on %s", listenSocket)

Expand Down
7 changes: 7 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type Config struct {
Interval time.Duration
AllDevicesMustBuild bool
}
Debug struct {
Pprof struct {
Enabled bool
}
}
}

type AuthConfig struct {
Expand Down Expand Up @@ -91,6 +96,8 @@ func setDefaults() {
viper.SetDefault("Authentication.LDAP.WorkersCount", defaultLDAPWorkersCount)
viper.SetDefault("Authentication.LDAP.Timeout", defaultLDAPTimeout)
viper.SetDefault("Authentication.LDAP.MaxConnectionLifetime", time.Minute)

viper.SetDefault("Debug.Pprof.Enabled", false)
}

func LoadConfig() error {
Expand Down

0 comments on commit e93ba62

Please sign in to comment.