Skip to content

Commit

Permalink
feat: Add debug option for pprof
Browse files Browse the repository at this point in the history
  • Loading branch information
konradasb committed Mar 24, 2024
1 parent 02cfa2c commit 230a687
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ basic_auth_enabled: true
# Map of basic authentication users. The key is the username and the value is the password. Valid only when `basic_auth_enabled` is true.
#
# Default: {}
#
basic_auth_users:
user1: password1
user2: password2

#
# Enable debug mode.
#
# Default: false
#
debug: false

#
# Metrics server configuration. This is used to expose Prometheus metrics on endpoint `/metrics`.
#
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/cbrgm/githubevents v1.13.0
github.com/containerd/log v0.1.0
github.com/gin-contrib/pprof v1.4.0
github.com/gin-contrib/requestid v0.0.6
github.com/gin-gonic/gin v1.9.1
github.com/google/go-github/v60 v60.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcP
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/pprof v1.4.0 h1:XxiBSf5jWZ5i16lNOPbMTVdgHBdhfGRD5PZ1LWazzvg=
github.com/gin-contrib/pprof v1.4.0/go.mod h1:RrehPJasUVBPK6yTUwOl8/NP6i0vbUgmxtis+Z5KE90=
github.com/gin-contrib/requestid v0.0.6 h1:mGcxTnHQ45F6QU5HQRgQUDsAfHprD3P7g2uZ4cSZo9o=
github.com/gin-contrib/requestid v0.0.6/go.mod h1:9i4vKATX/CdggbkY252dPVasgVucy/ggBeELXuQztm4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
2 changes: 2 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
GitHub *GitHubConfig `yaml:"github" validate:"required"`
Pools []*PoolConfig `yaml:"pools" validate:"required,min=1"`
LogLevel string `yaml:"log_level" validate:"required,oneof=debug info warn error fatal panic trace"`
Debug bool `yaml:"debug" validate:""`

path string
}
Expand Down Expand Up @@ -64,6 +65,7 @@ func DefaultConfig() *Config {
GitHub: &GitHubConfig{AppPrivateKey: "", AppID: 0, WebhookSecret: ""},
Pools: []*PoolConfig{},
LogLevel: "debug",
Debug: false,
}

return c
Expand Down
8 changes: 8 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/gin-contrib/pprof"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
"github.com/hostinger/fireactions"
Expand Down Expand Up @@ -97,6 +98,10 @@ func New(config *Config, opts ...Opt) (*Server, error) {
handler.GET("/healthz", getHealthzHandler())
handler.GET("/version", getVersionHandler())

if config.Debug {
pprof.Register(handler)
}

api := handler.Group("/api")
if config.BasicAuthEnabled {
api.Use(gin.BasicAuth(gin.Accounts(config.BasicAuthUsers)))
Expand All @@ -118,6 +123,9 @@ func New(config *Config, opts ...Opt) (*Server, error) {
// Run starts the server and blocks until the context is canceled.
func (s *Server) Run(ctx context.Context) error {
s.logger.Info().Str("version", fireactions.Version).Str("date", fireactions.Date).Str("commit", fireactions.Commit).Msgf("Starting server on %s", s.config.BindAddress)
if s.config.Debug {
s.logger.Warn().Msg("Debug mode enabled")
}

listener, err := net.Listen("tcp", s.config.BindAddress)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions server/testdata/config1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ basic_auth_users:
user1: password1
user2: password2

debug: false

metrics:
enabled: true
address: 127.0.0.1:8081
Expand Down

0 comments on commit 230a687

Please sign in to comment.