Add a simple pprof middleware for newcomers

Former-commit-id: f533df7e84b7e174d4cb861ff50d81772550feb0
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-03-05 03:36:00 +02:00
parent b961bedab0
commit 7e11032042
3 changed files with 84 additions and 1 deletions

View File

@ -190,7 +190,7 @@ func registerRoute(route iris.RouteInfo, gorillaRouter *mux.Router, context iris
subdomain := route.Subdomain() subdomain := route.Subdomain()
if subdomain != "" { if subdomain != "" {
if subdomain == "*." { if subdomain == iris.DynamicSubdomainIndicator {
// it's an iris wildcard subdomain // it's an iris wildcard subdomain
// so register it as wildcard on gorilla mux too // so register it as wildcard on gorilla mux too
subdomain = "{subdomain}." subdomain = "{subdomain}."

View File

@ -0,0 +1,24 @@
package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/middleware/pprof"
)
func main() {
app := iris.New()
app.Adapt(iris.DevLogger())
app.Adapt(httprouter.New())
app.Get("/", func(ctx *iris.Context) {
ctx.HTML(iris.StatusOK, "<h1> Please click <a href='/debug/pprof'>here</a>")
})
app.Get("/debug/pprof/*action", pprof.New())
// ___________
// Note:
// if you prefer gorillamux adaptor, then
// the wildcard for gorilla mux (as you already know) is '{action:.*}' instead of '*action'.
app.Listen(":8080")
}

59
middleware/pprof/pprof.go Normal file
View File

@ -0,0 +1,59 @@
// Package pprof usage: app.Get(iris.RouteWildcardPath("/debug/pprof", "action"), pprof.New())
// for specific router adaptors follow these optional route syntax:
// 'adaptors/httprouter':
//
// app := iris.New()
// app.Adapt(httprouter.New())
// app.Get("/debug/pprof/*action", pprof.New())
//
// 'adaptors/gorillamux':
//
// app := iris.New()
// app.Adapt(gorillamux.New())
// app.Get("/debug/pprof/{action:.*}", pprof.New())
package pprof
import (
"net/http/pprof"
"strings"
"gopkg.in/kataras/iris.v6"
)
// New returns a new pprof (profile, cmdline, symbol, goroutine, heap, threadcreate, debug/block) Middleware.
// Note: Route MUST have the last named parameter wildcard named '*action'
func New() iris.HandlerFunc {
indexHandler := iris.ToHandler(pprof.Index)
cmdlineHandler := iris.ToHandler(pprof.Cmdline)
profileHandler := iris.ToHandler(pprof.Profile)
symbolHandler := iris.ToHandler(pprof.Symbol)
goroutineHandler := iris.ToHandler(pprof.Handler("goroutine"))
heapHandler := iris.ToHandler(pprof.Handler("heap"))
threadcreateHandler := iris.ToHandler(pprof.Handler("threadcreate"))
debugBlockHandler := iris.ToHandler(pprof.Handler("block"))
return iris.HandlerFunc(func(ctx *iris.Context) {
ctx.SetContentType("text/html; charset=" + ctx.Framework().Config.Charset)
action := ctx.Param("action")
if len(action) > 1 {
if strings.Contains(action, "cmdline") {
cmdlineHandler.Serve((ctx))
} else if strings.Contains(action, "profile") {
profileHandler.Serve(ctx)
} else if strings.Contains(action, "symbol") {
symbolHandler.Serve(ctx)
} else if strings.Contains(action, "goroutine") {
goroutineHandler.Serve(ctx)
} else if strings.Contains(action, "heap") {
heapHandler.Serve(ctx)
} else if strings.Contains(action, "threadcreate") {
threadcreateHandler.Serve(ctx)
} else if strings.Contains(action, "debug/block") {
debugBlockHandler.Serve(ctx)
}
} else {
indexHandler.Serve(ctx)
}
})
}