Skip to content

Commit

Permalink
Use original expvar handler for profiler middleware (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyPodgornyy authored Oct 19, 2023
1 parent 3954a76 commit 247397c
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions middleware/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package middleware

import (
"expvar"
"fmt"
"net/http"
"net/http/pprof"

Expand All @@ -11,13 +10,13 @@ import (

// Profiler is a convenient subrouter used for mounting net/http/pprof. ie.
//
// func MyService() http.Handler {
// r := chi.NewRouter()
// // ..middlewares
// r.Mount("/debug", middleware.Profiler())
// // ..routes
// return r
// }
// func MyService() http.Handler {
// r := chi.NewRouter()
// // ..middlewares
// r.Mount("/debug", middleware.Profiler())
// // ..routes
// return r
// }
func Profiler() http.Handler {
r := chi.NewRouter()
r.Use(NoCache)
Expand All @@ -34,7 +33,7 @@ func Profiler() http.Handler {
r.HandleFunc("/pprof/profile", pprof.Profile)
r.HandleFunc("/pprof/symbol", pprof.Symbol)
r.HandleFunc("/pprof/trace", pprof.Trace)
r.HandleFunc("/vars", expVars)
r.Handle("/vars", expvar.Handler())

r.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
Expand All @@ -45,18 +44,3 @@ func Profiler() http.Handler {

return r
}

// Replicated from expvar.go as not public.
func expVars(w http.ResponseWriter, r *http.Request) {
first := true
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\n")
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
}

0 comments on commit 247397c

Please sign in to comment.