# README
metrics性能监控
使用方法:
import "github.com/daheige/thinkgo/monitor"
1、在init()方法中添加如下代码
//注册监控指标
//web程序的性能监控,如果是job/rpc服务就不需要这两行
prometheus.MustRegister(WebRequestTotal)
prometheus.MustRegister(WebRequestDuration)
prometheus.MustRegister(monitor.CpuTemp)
prometheus.MustRegister(monitor.HdFailures)
2、在pprof中添加如下路由:
//性能报告监控和健康检测
//性能监控的端口只能在内网访问
var PProfPort = 2338
go func() {
//defer logger.Recover() //参考thinkgo/logger包
PProfAddress := fmt.Sprintf("0.0.0.0:%d", PProfPort)
log.Println("server pprof run on: ", PProfAddress)
httpMux := http.NewServeMux() //创建一个http ServeMux实例
httpMux.HandleFunc("/debug/pprof/", pprof.Index)
httpMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
httpMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
httpMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
httpMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
httpMux.HandleFunc("/check", routes.HealthCheck)
//metrics监控
httpMux.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(PProfAddress, httpMux); err != nil {
log.Println(err)
}
}()
也可以直接调用 PrometheusHandler 监控,包含了PProf性能监控
实战案例
https://github.com/daheige/hg-mux
# Functions
MonitorHandler 性能监控处理器 可以作为中间件对接口进行打点监控.
MonitorHandlerFunc 对于http原始的处理器函数,包装 handler function,不侵入业务逻辑 可以对单个接口做metrics监控.
# Variables
CpuTemp cpu情况.
No description provided by the author
WebRequestDuration web_request_duration_seconds, Histogram类型指标,bucket代表duration的分布区间.
WebRequestTotal 初始化 web_request_total, counter类型指标, 表示接收http请求总次数 设置两个标签 请求方法和 路径 对请求总次数在两个.