Gin Web 框架集成pprof

Yishto 2021-08-20 21:46:16
Categories: Tags:

项目地址: https://github.com/DeanThompson/ginpprof

Install

First install ginpprof to your GOPATH using go get:

1
2
go get github.com/DeanThompson/ginpprof

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
"github.com/gin-gonic/gin"

"github.com/DeanThompson/ginpprof"
)

func main() {
router := gin.Default()

router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})

// automatically add routers for net/http/pprof
// e.g. /debug/pprof, /debug/pprof/heap, etc.
ginpprof.Wrapper(router)

router.Run(":8080")
}

Start this server, and you will see such outputs:

1
2
3
4
5
6
7
8
9
10
11
[GIN-debug] GET   /ping                     --> main.func·001 (3 handlers)
[GIN-debug] GET /debug/pprof/ --> github.com/DeanThompson/ginpprof.func·001 (3 handlers)
[GIN-debug] GET /debug/pprof/heap --> github.com/DeanThompson/ginpprof.func·002 (3 handlers)
[GIN-debug] GET /debug/pprof/goroutine --> github.com/DeanThompson/ginpprof.func·003 (3 handlers)
[GIN-debug] GET /debug/pprof/block --> github.com/DeanThompson/ginpprof.func·004 (3 handlers)
[GIN-debug] GET /debug/pprof/threadcreate --> github.com/DeanThompson/ginpprof.func·005 (3 handlers)
[GIN-debug] GET /debug/pprof/cmdline --> github.com/DeanThompson/ginpprof.func·006 (3 handlers)
[GIN-debug] GET /debug/pprof/profile --> github.com/DeanThompson/ginpprof.func·007 (3 handlers)
[GIN-debug] GET /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.func·008 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080

Now visit http://127.0.0.1:8080/debug/pprof/ and you’ll see what you want.

Have Fun.