Prometheus 支持¶
如何获取、推送指标数据¶
pairec 内置了两个 prometheus 的指标拉取接口。如果要通过 prometheus 监控 pairec 服务,接口参考这里。
如果需要将指标数据推送到 prometheus gateway,则需要通过设置 pairec 配置来开启功能。
配置示例如下:
{
...
"PrometheusConfig": {
"Enable": true,
"PushGatewayURL": "https://your_pushgateway_url",
"PushIntervalSecs": 15
},
...
}
当 Enable 设置为 true,pairec 就会按 PushIntervalSecs 指定的时间间隔(单位秒)向 PushGatewayURL 指定的地址推送指标数据。
注册自定义指标¶
在没有额外操作的情况下,通过上文的方法可以获取和推送 pairec 预定义的指标数据。
如果需要获取和推送自定义指标,那么需要在代码中注册自定义指标,示例代码如下:
package main
import (
"log"
"time"
"github.com/prometheus/client_golang/prometheus"
"gitlab.alibaba-inc.com/pai_biz_arch/pairec"
"gitlab.alibaba-inc.com/pai_biz_arch/pairec/service/metrics"
)
func main() {
m := prometheus.NewCounter(prometheus.CounterOpts{
Name: "abc",
})
err := metrics.CustomRegister.Register(m)
if err != nil {
log.Fatalln(err)
}
go func() {
for {
time.Sleep(time.Second * 5)
m.Inc()
}
}()
// ...
pairec.Run()
}
在 CustomRegister 注册自定义指标后,就能从 /custom_metrics 接口获取到自定义指标的数据了。
如果开启了推送配置,自定义指标数据也会被推送到 prometheus gateway。
示例代码中简单起见只是开启了一个协程定时设置指标数据,实际情况中可以在接口 controller 逻辑中或任意其他位置设置指标数据。