Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add performance profiler #141

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
core/**/main
core/**/bin
core/**/client

# Output of pprof files
**/*.prof
4 changes: 4 additions & 0 deletions core/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ServerConfig struct {
TimeoutIdle time.Duration

// Misc settings.
// Enable /debug/pprof endpoints.
Profiler bool `env:"PROFILER"`
UseEnvironment bool
DemoMode bool `env:"DEMO_MODE"`

Expand Down Expand Up @@ -64,6 +66,7 @@ const (
DefaultLoggerLevel = "info"

// Misc constants.
DefaultProfiler = false
DefaultDemoMode = false
)

Expand All @@ -84,6 +87,7 @@ func NewServerConfig(useEnv bool, version string, commit string) (*ServerConfig,
TimeoutRead: DefaultTimeoutRead,
TimeoutWrite: DefaultTimeoutWrite,
TimeoutIdle: DefaultTimeoutIdle,
Profiler: DefaultProfiler,
UseEnvironment: useEnv,
DemoMode: DefaultDemoMode,
Version: version,
Expand Down
12 changes: 12 additions & 0 deletions core/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -67,6 +68,7 @@ func (s *StartCommand) ParseFlags(args []string) error {
fs.StringVar(&s.AnalyticsDB.Host, "analyticsdb", s.AnalyticsDB.Host, "Path to analytics database.")

// Misc settings.
fs.BoolVar(&s.Server.Profiler, "profiler", s.Server.Profiler, "Enable debug profiling.")
fs.BoolVar(&s.Server.UseEnvironment, "env", false, "Opt-in to allow environment variables to be used for configuration. Flags will still override environment variables.")
fs.BoolVar(&s.Server.DemoMode, "demo", s.Server.DemoMode, "Enable demo mode restricting all POST/PATCH/DELETE actions (except login).")

Expand Down Expand Up @@ -150,6 +152,16 @@ func (s *StartCommand) Run(ctx context.Context) error {
mux.Handle("/openapi.yaml", http.FileServer(http.FS(generate.OpenAPIDocument)))
mux.Handle("/api/", http.StripPrefix("/api", apiHandler))

// Start CPU profiling if enabled.
if s.Server.Profiler {
log.Warn().Msg("Enabling debug profiler...")
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

// SPA client.
err = services.SetupAssetHandler(mux, service.RuntimeConfig)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ APP_DATABASE_HOST = '/db/app.db'
PORT = '8080'
LEVEL = 'debug'
DEMO_MODE = 'true'
PROFILER = 'true'

[[mounts]]
source = 'medama_db'
Expand Down
Loading