prometheus使用记录
exporter counter demo
// add promhttp
beego.Handler("/metrics", promhttp.Handler())
// 或者
handler := http.NewServeMux()
handler.Handle("/metrics", promhttp.Handler())
// 不带label的counter使用方式
// 声明metric
var (
codeCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "http_request_total_code",
Help: "total request code controller",
},
)
)
// 注册metrics
func init() {
prometheus.MustRegister(codeCounter)
}
// 在需要的地方进行调用
codeCounter.Inc()
// 带label的counter的使用方式
// 声明metric
var (
openfalconAlarmCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "openfalcon_alarm_total",
Help: "Total number of openfalcon alarms",
},
[]string{"level"},
)
)
// 注册metrics
func init() {
prometheus.MustRegister(openfalconAlarmCounterVec)
}
// 在需要的地方进行调用
openfalconAlarmCounterVec.WithLabelValues("0").Inc()
refer
prometheus package - github.com/prometheus/client_golang/prometheus - Go Packages