Former-commit-id: 752a788263f8bd9219690b3b63b1183546fb91e5
This commit is contained in:
Gerasimos Maropoulos 2018-04-25 13:10:27 +03:00
parent b22151d4b7
commit 59f138963b

View File

@ -2,7 +2,9 @@
package pprof package pprof
import ( import (
"html/template"
"net/http/pprof" "net/http/pprof"
rpprof "runtime/pprof"
"strings" "strings"
"github.com/kataras/iris/context" "github.com/kataras/iris/context"
@ -12,7 +14,6 @@ import (
// New returns a new pprof (profile, cmdline, symbol, goroutine, heap, threadcreate, debug/block) Middleware. // 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:path}' // Note: Route MUST have the last named parameter wildcard named '{action:path}'
func New() context.Handler { func New() context.Handler {
indexHandler := handlerconv.FromStd(pprof.Index)
cmdlineHandler := handlerconv.FromStd(pprof.Cmdline) cmdlineHandler := handlerconv.FromStd(pprof.Cmdline)
profileHandler := handlerconv.FromStd(pprof.Profile) profileHandler := handlerconv.FromStd(pprof.Profile)
symbolHandler := handlerconv.FromStd(pprof.Symbol) symbolHandler := handlerconv.FromStd(pprof.Symbol)
@ -23,25 +24,54 @@ func New() context.Handler {
return func(ctx context.Context) { return func(ctx context.Context) {
ctx.ContentType("text/html") ctx.ContentType("text/html")
actionPathParameter := ctx.Values().GetString("action") action := ctx.Params().Get("action")
if len(actionPathParameter) > 1 { if action != "" {
if strings.Contains(actionPathParameter, "cmdline") { if strings.Contains(action, "cmdline") {
cmdlineHandler((ctx)) cmdlineHandler((ctx))
} else if strings.Contains(actionPathParameter, "profile") { } else if strings.Contains(action, "profile") {
profileHandler(ctx) profileHandler(ctx)
} else if strings.Contains(actionPathParameter, "symbol") { } else if strings.Contains(action, "symbol") {
symbolHandler(ctx) symbolHandler(ctx)
} else if strings.Contains(actionPathParameter, "goroutine") { } else if strings.Contains(action, "goroutine") {
goroutineHandler(ctx) goroutineHandler(ctx)
} else if strings.Contains(actionPathParameter, "heap") { } else if strings.Contains(action, "heap") {
heapHandler(ctx) heapHandler(ctx)
} else if strings.Contains(actionPathParameter, "threadcreate") { } else if strings.Contains(action, "threadcreate") {
threadcreateHandler(ctx) threadcreateHandler(ctx)
} else if strings.Contains(actionPathParameter, "debug/block") { } else if strings.Contains(action, "debug/block") {
debugBlockHandler(ctx) debugBlockHandler(ctx)
} }
} else { return
indexHandler(ctx) }
profiles := rpprof.Profiles()
data := map[string]interface{}{
"Profiles": profiles,
"Path": ctx.RequestPath(false),
}
if err := indexTmpl.Execute(ctx, data); err != nil {
ctx.Application().Logger().Error(err)
} }
} }
} }
var indexTmpl = template.Must(template.New("index").Parse(`<html>
<head>
<title>/{{.Path}}</title>
</head>
<body>
{{.Path}}<br>
<br>
profiles:<br>
<table>
{{$path := .Path}}
{{range .Profiles}}
<tr><td align=right>{{.Count}}<td><a href="{{$path}}/{{.Name}}?debug=1">{{.Name}}</a>
{{end}}
</table>
<br>
<a href="{{$path}}/goroutine?debug=2">full goroutine stack dump</a><br>
</body>
</html>
`))