Skip to content

Commit

Permalink
added profile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
buckhx committed Mar 25, 2016
1 parent 7a0cf5d commit c5153be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func client(args []string) {
Value: "8080",
Usage: "Port to bind to",
},
cli.BoolFlag{
Name: "profile",
Usage: "Mount profiling endpoints",
},
}
app.Action = func(c *cli.Context) {
args := c.Args()
Expand All @@ -51,8 +55,12 @@ func client(args []string) {
if err != nil {
die(c, err.Error())
}
prof := c.Bool("profile")
if prof {
log.Println("Profiling available at /debug/pprof/")
}
port := fmt.Sprintf(":%s", c.String("port"))
err = geofence.ListenAndServe(port, fences)
err = geofence.ListenAndServe(port, fences, prof)
die(c, err.Error())
}
app.Run(args)
Expand Down
18 changes: 16 additions & 2 deletions geofence/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"net/http/pprof"
"strconv"

"github.com/buckhx/diglet/geo"
Expand All @@ -15,7 +15,7 @@ import (

var fences FenceIndex

func ListenAndServe(addr string, idx FenceIndex) error {
func ListenAndServe(addr string, idx FenceIndex, profile bool) error {
log.Printf("Fencing on address %s\n", addr)
defer log.Printf("Done Fencing\n")
fences = idx
Expand All @@ -26,6 +26,9 @@ func ListenAndServe(addr string, idx FenceIndex) error {
router.POST("/fence/:name/add", postAdd)
router.POST("/fence/:name/search", postSearch)
router.GET("/fence/:name/search", getSearch)
if profile {
attachProfiler(router)
}
return http.ListenAndServe(addr, router)
}

Expand Down Expand Up @@ -131,3 +134,14 @@ func getEngarde(w http.ResponseWriter, r *http.Request, params httprouter.Params
w.Header().Set("Content-Length", fmt.Sprint(len(response)))
fmt.Fprint(w, response)
}

func attachProfiler(router *httprouter.Router) {
router.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
router.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
router.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
router.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
router.Handler("GET", "/debug/pprof/heap", pprof.Handler("heap"))
router.Handler("GET", "/debug/pprof/block", pprof.Handler("block"))
router.Handler("GET", "/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handler("GET", "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
}

0 comments on commit c5153be

Please sign in to comment.