Go 应用全方位监控探针,提供 pprof 性能分析、系统监控、进程监控、告警和健康检查功能。
/health 端点,支持 K8s 探针net/http/pprof 的简单 APIgo get cnb.cool/svn/pprof
适用于所有 Web 框架(Gin、Chi、Echo、Fiber 等):
package main
import (
"net/http"
goprobe "cnb.cool/svn/pprof"
)
func main() {
mux := http.NewServeMux()
// 挂载 GoProbe(包含 UI + 原生 pprof 端点)
mux.Handle("/debug/probe/", http.StripPrefix("/debug/probe", goprobe.Handler()))
// 你的其他路由...
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
http.ListenAndServe(":6060", mux)
}
import (
"github.com/gin-gonic/gin"
goprobe "cnb.cool/svn/pprof"
)
func main() {
r := gin.Default()
// 挂载 GoProbe
r.Any("/debug/probe/*any", gin.WrapH(goprobe.HandlerWithPrefix("/debug/probe")))
// 你的其他路由...
r.GET("/", func(c *gin.Context) {
c.String(200, "Hello World")
})
r.Run(":6060")
}
启动后访问:
// 自定义配置
opts := goprobe.Options{
HistorySize: 50, // 历史记录保留数量
StatsHistorySize: 600, // Go 运行时统计历史 (10分钟)
SysMonHistorySize: 600, // 系统监控历史 (10分钟)
StatsInterval: time.Second, // 采集间隔
SnapshotSize: 20, // 快照保留数量
Version: "1.0.0", // 服务版本
ServiceName: "my-service", // 服务名称
}
handler := goprobe.HandlerWithOptions(opts)
mux.Handle("/debug/probe/", http.StripPrefix("/debug/probe", handler))
返回 http.Handler,包含完整的 UI 和原生 pprof 端点:
// 标准 http.ServeMux
mux.Handle("/debug/probe/", http.StripPrefix("/debug/probe", goprobe.Handler()))
// Gin
r.Any("/debug/probe/*any", gin.WrapH(goprobe.HandlerWithPrefix("/debug/probe")))
// Chi
r.Mount("/debug/probe", goprobe.Handler())
// Echo
e.Any("/debug/probe/*", echo.WrapHandler(goprobe.HandlerWithPrefix("/debug/probe")))
自动处理路径前缀,适用于需要自定义挂载路径的场景:
// Gin 示例
r.Any("/custom/path/*any", gin.WrapH(goprobe.HandlerWithPrefix("/custom/path")))
使用自定义配置:
handler := goprobe.HandlerWithOptions(goprobe.Options{
HistorySize: 100,
ServiceName: "my-service",
})
mux.Handle("/debug/probe/", http.StripPrefix("/debug/probe", handler))
GoProbe 提供以下端点:
/ - Web UI 首页/api/types - 可用的 profile 类型/api/profile - 获取 profile 数据/api/flamegraph - 火焰图数据/api/stats - Go 运行时统计/api/stats/stream - SSE 实时统计流/api/sysmon - 系统监控数据/api/sysmon/stream - SSE 系统监控流/api/alerts/* - 告警配置和历史/api/snapshots/* - 快照管理/health - 健康检查/pprof/ - pprof 索引页/pprof/profile - CPU profile/pprof/heap - 堆内存 profile/pprof/goroutine - Goroutine profile/pprof/allocs - 内存分配 profile/pprof/block - 阻塞 profile/pprof/mutex - 互斥锁 profile/pprof/threadcreate - 线程创建 profile/pprof/cmdline - 命令行参数/pprof/symbol - 符号解析/pprof/trace - 执行追踪# CPU profiling (30秒)
go tool pprof http://localhost:6060/debug/probe/pprof/profile?seconds=30
# 堆内存分析
go tool pprof http://localhost:6060/debug/probe/pprof/heap
# Goroutine 分析
go tool pprof http://localhost:6060/debug/probe/pprof/goroutine
GoProbe 返回标准的 http.Handler,可以轻松集成到任何 Go Web 框架。
import (
"github.com/gin-gonic/gin"
goprobe "cnb.cool/svn/pprof"
)
func main() {
r := gin.Default()
// 使用 gin.WrapH
r.Any("/debug/probe/*any", gin.WrapH(goprobe.HandlerWithPrefix("/debug/probe")))
r.Run(":8080")
}
import (
"github.com/go-chi/chi/v5"
goprobe "cnb.cool/svn/pprof"
)
func main() {
r := chi.NewRouter()
// 直接挂载
r.Mount("/debug/probe", goprobe.Handler())
http.ListenAndServe(":8080", r)
}
import (
"github.com/labstack/echo/v4"
goprobe "cnb.cool/svn/pprof"
)
func main() {
e := echo.New()
// 使用 echo.WrapHandler
e.Any("/debug/probe/*", echo.WrapHandler(goprobe.HandlerWithPrefix("/debug/probe")))
e.Start(":8080")
}
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
goprobe "cnb.cool/svn/pprof"
)
func main() {
app := fiber.New()
// 使用 adaptor 转换
app.All("/debug/probe/*", adaptor.HTTPHandler(goprobe.HandlerWithPrefix("/debug/probe")))
app.Listen(":8080")
}
import (
"net/http"
goprobe "cnb.cool/svn/pprof"
)
func main() {
mux := http.NewServeMux()
// 方式 1: 使用 Register (推荐,同时注册 pprof 端点)
goprobe.Register(mux)
// 方式 2: 手动挂载 Handler
mux.Handle("/debug/probe/", http.StripPrefix("/debug/probe", goprobe.Handler()))
http.ListenAndServe(":8080", mux)
}
| 类型 | 描述 | 即时 |
|---|---|---|
| cpu | CPU 使用分析 | ❌ |
| heap | 堆内存分配 | ✅ |
| goroutine | Goroutine 堆栈 | ✅ |
| allocs | 内存分配采样 | ✅ |
| block | 阻塞分析 | ✅ |
| mutex | 互斥锁竞争 | ✅ |
| threadcreate | 线程创建 | ✅ |
| 端点 | 方法 | 描述 |
|---|---|---|
/debug/probe/ | GET | Web UI 入口 |
/debug/probe/api/types | GET | 获取可用类型 |
/debug/probe/api/profile | GET | 获取 profile (JSON) |
/debug/probe/api/flamegraph | GET | 获取火焰图数据 |
/debug/probe/api/history | GET | 列出历史记录 |
/debug/probe/api/diff | GET | 对比两个 profile |
/debug/probe/api/stats | GET | 获取 Go 运行时统计 |
/debug/probe/api/stats/stream | GET | SSE 实时统计流 |
/debug/probe/api/sysmon | GET | 获取系统监控数据 |
/debug/probe/api/sysmon/stream | GET | SSE 系统监控流 |
/debug/probe/api/alerts/config | GET/POST | 告警配置 |
/debug/probe/api/alerts/active | GET | 活跃告警 |
/debug/probe/api/alerts/test | POST | 测试 Webhook |
/debug/probe/api/snapshots | GET/POST | 快照列表/创建 |
/debug/probe/api/health | GET | 健康检查 |
| 端点 | 描述 |
|---|---|
/debug/pprof/ | pprof 索引页 |
/debug/pprof/profile | CPU profile |
/debug/pprof/heap | 堆内存 profile |
/debug/pprof/goroutine | Goroutine profile |
/debug/pprof/cmdline | 命令行参数 |
/debug/pprof/symbol | 符号化 |
/debug/pprof/trace | 执行追踪 |
实时监控系统和进程级指标:
系统级:
进程级:
支持多种阈值告警:
// Go 运行时阈值
- Goroutine 数量
- 堆内存大小
- GC 暂停时间
- Goroutine 泄漏检测
// 系统级阈值
- CPU 使用率
- 内存使用率
- 系统负载
- 磁盘 IO 速率
- 网络流量
// 进程级阈值
- 进程 CPU/内存
- 文件描述符数
- 连接数
Webhook 支持模板变量:
{
"msgtype": "markdown",
"markdown": {
"content": "## {{status}} {{level_cn}}\n**类型**: {{type}}\n**消息**: {{message}}\n**当前值**: {{value}}\n**阈值**: {{threshold}}\n**时间**: {{time}}"
}
}
curl http://localhost:6060/debug/probe/api/health
返回:
{
"status": "healthy",
"timestamp": "2024-01-01T12:00:00Z",
"uptime": "24h30m",
"version": "1.0.0",
"checks": {
"goroutines": { "status": "pass", "value": "50" },
"memory": { "status": "pass", "value": "128 MB" },
"gc": { "status": "pass", "value": "1.2 ms" }
}
}
保存完整的运行时状态,支持对比分析:
# 创建快照
curl -X POST http://localhost:6060/debug/probe/api/snapshots \
-H "Content-Type: application/json" \
-d '{"label": "优化前", "note": "版本 v1.0"}'
# 对比快照
curl "http://localhost:6060/debug/probe/api/snapshots/compare?id1=snap-1&id2=snap-2"
# CPU 分析
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
# 内存分析
go tool pprof http://localhost:6060/debug/pprof/heap
# Goroutine 分析
go tool pprof http://localhost:6060/debug/pprof/goroutine
MIT