Skip to content

Commit

Permalink
Move profiler to internal route (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Sep 21, 2023
1 parent e97497e commit 2c23094
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
18 changes: 7 additions & 11 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func Run(
history History,
redisConnection *redis.Redis,
saveIntercal time.Duration,
profileRoutes bool,
) error {
var connectionCount *connectionCount
if redisConnection != nil {
Expand All @@ -54,10 +53,7 @@ func Run(
HandleInternalAutoupdate(mux, auth, history, autoupdate)
HandleShowConnectionCount(mux, autoupdate, auth, connectionCount)
HandleHistoryInformation(mux, auth, history)

if profileRoutes {
HandleProfile(mux)
}
HandleProfile(mux)

srv := &http.Server{
Addr: addr,
Expand Down Expand Up @@ -358,12 +354,12 @@ func HandleHealth(mux *http.ServeMux) {

// HandleProfile adds routes for profiling.
func HandleProfile(mux *http.ServeMux) {
mux.Handle(prefixPublic+"/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle(prefixPublic+"/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle(prefixPublic+"/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle(prefixPublic+"/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle(prefixPublic+"/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle(prefixPublic+"/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
mux.Handle(prefixInternal+"/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle(prefixInternal+"/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle(prefixInternal+"/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle(prefixInternal+"/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle(prefixInternal+"/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle(prefixInternal+"/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}

func authMiddleware(next http.Handler, auth Authenticater) http.Handler {
Expand Down
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var (
envMetricInterval = environment.NewVariable("METRIC_INTERVAL", "5m", "Time in how often the metrics are gathered. Zero disables the metrics.")
envMetricSaveInterval = environment.NewVariable("METRIC_SAVE_INTERVAL", "5m", "Interval, how often the metric should be saved to redis. Redis will ignore entries, that are twice at old then the save interval.")
envDisableConnectionCount = environment.NewVariable("DISABLE_CONNECTION_COUNT", "false", "Do not count connections.")
envEnableProfileRoutes = environment.NewVariable("ENABLE_PROFILE_ROUTES", "false", "Activate development routes for profiling.")
)

var cli struct {
Expand Down Expand Up @@ -185,16 +184,14 @@ func initService(lookup environment.Environmenter) (func(context.Context) error,
metricStorage = nil
}

profileRoutes, _ := strconv.ParseBool(envEnableProfileRoutes.Value(lookup))

service := func(ctx context.Context) error {
for _, bg := range backgroundTasks {
go bg(ctx, oserror.Handle)
}

// Start http server.
fmt.Printf("Listen on %s\n", listenAddr)
return http.Run(ctx, listenAddr, authService, auService, historyService, metricStorage, metricSaveInterval, profileRoutes)
return http.Run(ctx, listenAddr, authService, auService, historyService, metricStorage, metricSaveInterval)
}

return service, nil
Expand Down

0 comments on commit 2c23094

Please sign in to comment.