monitor

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 4 Imported by: 9

README

metrics性能监控

使用方法:
import "github.com/daheige/thinkgo/monitor"

1、在init()方法中添加如下代码
//注册监控指标
prometheus.MustRegister(monitor.WebRequestTotal)
prometheus.MustRegister(monitor.WebRequestDuration)
prometheus.MustRegister(monitor.CpuTemp)
prometheus.MustRegister(monitor.HdFailures)

2、在pprof中添加如下路由:
//性能报告监控和健康检测
//性能监控的端口port+1000,只能在内网访问
go func() {
	//defer logger.Recover() //参考thinkgo/logger包

	pprof_address := fmt.Sprintf("0.0.0.0:%d", port+1000)
	log.Println("server pprof run on: ", pprof_address)

	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(pprof_address, httpMux); err != nil {
		log.Println(err)
	}
}()

实战案例

https://github.com/daheige/hg-mux

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
	Name: "cpu_temperature_celsius",
	Help: "Current temperature of the CPU",
})
View Source
var HdFailures = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Name: "hd_errors_total",
		Help: "Number of hard-disk errors",
	},
	[]string{"device"},
)
View Source
var WebRequestDuration = prometheus.NewHistogramVec(
	prometheus.HistogramOpts{
		Name:    "web_request_duration_seconds",
		Help:    "web request duration distribution",
		Buckets: []float64{0.1, 0.3, 0.5, 0.7, 0.9, 1},
	},
	[]string{"method", "endpoint"},
)

web_request_duration_seconds,Histogram类型指标,bucket代表duration的分布区间

View Source
var WebRequestTotal = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Name: "web_reqeust_total",
		Help: "Number of hello requests in total",
	},
	[]string{"method", "endpoint"},
)

初始化 web_reqeust_total, counter类型指标, 表示接收http请求总次数

Functions

func Check

func Check(w http.ResponseWriter, r *http.Request)

模拟查询

func MonitorHandler

func MonitorHandler(h http.Handler) http.Handler

MonitorHandler 性能监控处理器 可以作为中间件对接口进行打点监控

func MonitorHandlerFunc

func MonitorHandlerFunc(h http.HandlerFunc) http.HandlerFunc

MonitorHandlerFunc 对于http原始的处理器函数,包装 handler function,不侵入业务逻辑 可以对单个接口做metrics监控

Types

This section is empty.

Jump to

Keyboard shortcuts

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