pprof

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2022 License: Apache-2.0 Imports: 4 Imported by: 71

README

pprof (This is a community driven project)

English | 中文

pprof middleware for Hertz framework, inspired by pprof. This project would not have been possible without the support from the CloudWeGo community and previous work done by the gin community.

  • Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.

Install

go get github.com/hertz-contrib/pprof

Usage

Example
func main() {
    h := server.Default()
    
    pprof.Register(h)
    
    h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
    ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
    })
    
    h.Spin()
}
change default path prefix
import (
    "context"
    "github.com/cloudwego/hertz/pkg/app"
    "github.com/cloudwego/hertz/pkg/app/server"
    "github.com/cloudwego/hertz/pkg/common/utils"
    "github.com/cloudwego/hertz/pkg/protocol/consts"
    "hertz-contrib-beiye/pprof"
)

func main() {
    h := server.Default()
    
    // default is "debug/pprof"
    pprof.Register(h, "dev/pprof")
    
    h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
        ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
    })
    
    h.Spin()
}

custom router group
import (
    "context"
    "github.com/cloudwego/hertz/pkg/app"
    "github.com/cloudwego/hertz/pkg/app/server"
    "hertz-contrib-beiye/pprof"
    "net/http"
)

func main() {
    h := server.Default()
    
    pprof.Register(h)

    adminGroup := h.Group("/admin")
    adminGroup.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
    ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
    })
    
    pprof.RouteRegister(adminGroup, "pprof")
    
    h.Spin()
}

Use the pprof tool

Then use the pprof tool to look at the heap profile:

go tool pprof http://localhost:8888/debug/pprof/heap

Or to look at a 30-second CPU profile:

go tool pprof http://localhost:8888/debug/pprof/profile

Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program:

go tool pprof http://localhost:8888/debug/pprof/block

Or to collect a 5-second execution trace:

wget http://localhost:8888/debug/pprof/trace?seconds=5

License

This project is under the Apache License 2.0. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	// DefaultPrefix url prefix of pprof
	DefaultPrefix = "/debug/pprof"
)

Variables

This section is empty.

Functions

func Register

func Register(r *server.Hertz, prefixOptions ...string)

Register the standard HandlerFuncs from the net/http/pprof package with the provided hertz.Hertz. prefixOptions is a optional. If not prefixOptions, the default path prefix is used, otherwise first prefixOptions will be path prefix.

func RouteRegister

func RouteRegister(rg *route.RouterGroup, prefixOptions ...string)

RouteRegister the standard HandlerFuncs from the net/http/pprof package with the provided hertz.RouterGroup. prefixOptions is a optional. If not prefixOptions, the default path prefix is used, otherwise first prefixOptions will be path prefix.

Types

This section is empty.

Directories

Path Synopsis
example
custom_prefix command
default command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL