treemux 集成pprof以及statsviz
pprof 是golang内置的一个性能分析包,使用简单、方便,statsviz 是最近 开源的一个可视化的golang性能观察点
以下是说明treemux 如何集成这两个工具
准备pprof&&statsviz treemux handlerfunc
因为treemux与golang内置的handlerfun有差异所以需要进行下包装
- pprof 包装参考
很简单,一个通用注册handler
func pprofHandler(h http.HandlerFunc) treemux.HandlerFunc {
handler := http.HandlerFunc(h)
return func(w http.ResponseWriter, req treemux.Request) error {
handler.ServeHTTP(w, req.Request)
return nil
}
}
注册pprof handler
const (
defaultPrefix string = "/debug/pprof"
)
// RouterpprofRegister for treemux
func RouterpprofRegister(rg *treemux.TreeMux) {
prefixRouter := rg.NewGroup(defaultPrefix)
{
prefixRouter.GET("/", pprofHandler(pprof.Index))
prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))
prefixRouter.GET("/profile", pprofHandler(pprof.Profile))
prefixRouter.POST("/symbol", pprofHandler(pprof.Symbol))
prefixRouter.GET("/symbol", pprofHandler(pprof.Symbol))
prefixRouter.GET("/trace", pprofHandler(pprof.Trace))
prefixRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP))
prefixRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP))
prefixRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP))
prefixRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP))
prefixRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP))
prefixRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP))
}
}
- statsviz包装
const (
defaultPrefix string = "/debug/statsviz"
)
// RouterStatsvizRegister for treemux
func RouterStatsvizRegister(rg *treemux.TreeMux) {
prefixRouter := rg.NewGroup(defaultPrefix)
prefixRouter.GET("/", pprofHandler(statsviz.Index.ServeHTTP))
prefixRouter.GET("/*path", pprofHandler(statsviz.Index.ServeHTTP))
prefixRouter.GET("/ws", pprofHandler(statsviz.Ws))
}
treemux 集成使用
- go mod
module demoapp
go 1.14
require (
github.com/arl/statsviz v0.1.1
github.com/rongfengliang/treemux-pprof v0.0.0-20201017072014-d71b3788890e // indirect
github.com/rongfengliang/treemux-statsviz v0.0.0-20201017073923-c9cc38c0c032 // indirect
github.com/vmihailenco/treemux v0.1.0
)
- 代码
package main
import (
"fmt"
"net/http"
pprof "github.com/rongfengliang/treemux-pprof"
statsviz "github.com/rongfengliang/treemux-statsviz"
"github.com/vmihailenco/treemux"
)
func corsMiddleware(next treemux.HandlerFunc) treemux.HandlerFunc {
return func(w http.ResponseWriter, req treemux.Request) error {
if origin := req.Header.Get("Origin"); origin != "" {
h := w.Header()
h.Set("Access-Control-Allow-Origin", origin)
h.Set("Access-Control-Allow-Credentials", "true")
}
return next(w, req)
}
}
func main() {
router := treemux.New()
group := router.NewGroup("/api")
group.GET("/v1/:id", func(w http.ResponseWriter, req treemux.Request) error {
id := req.Param("id")
fmt.Fprintf(w, "GET /api/v1/%s", id)
fmt.Fprintf(w, "route: %s", req.Route())
return nil
})
group.GET("/*path", func(w http.ResponseWriter, req treemux.Request) error {
fmt.Println(w, "with catch-all route")
return nil
})
group.GET("/", func(w http.ResponseWriter, req treemux.Request) error {
fmt.Fprintf(w, " api default page")
return nil
})
router.GET("/*path", func(w http.ResponseWriter, req treemux.Request) error {
fmt.Fprintf(w, "with catch-all route")
return nil
})
router.GET("/", func(w http.ResponseWriter, req treemux.Request) error {
fmt.Fprintf(w, "default page")
return nil
})
//routepprofRegister(router)
pprof.RouterpprofRegister(router)
statsviz.RouterStatsvizRegister(router)
router.Use(corsMiddleware)
http.ListenAndServe(":8080", router)
}
- 效果
pprof
statsviz
说明
关于pprof 以及statsviz的treemux包装我已经提供了golang mod 可以使用引用使用
参考资料
https://github.com/rongfengliang/treemux-pprof
https://github.com/rongfengliang/treemux-statsviz
https://github.com/vmihailenco/treemux
https://github.com/arl/statsviz
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-10-17 Aquameta 基于postgresql的web 开发平台
2019-10-17 使用google autoservice 自动生成java spi 描述文件
2019-10-17 micronaut 学习 二 创建一个简单的服务
2019-10-17 micronaut 学习一 基本安装
2019-10-17 roughViz 一个可重用,功能强大的手绘图表组件
2018-10-17 使用just-api 进行接口测试
2016-10-17 HTTP Proxy Servlet 代理服务使用