调用链路我大概描述下
- postman 调用 /ping 接口
- 标签为 ping_request_count 的prometheus sdk 侧 内部的 counter 计数器+1
- prometheus 拉取配置的job的metrics 接口 xxxx:8090
- 在peometheus 侧 通过
ping_request_count{job="super_ping"}这个标签查询
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus" //prometheues官方库
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
// ping counter 计数器
var pingCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ping_request_count",
Help: "this is a ping counter",
})
// 更新 ping 处理程序以使用pingCounter.Inc() 增加计数器的计数
func ping(w http.ResponseWriter, r *http.Request) {
pingCounter.Inc()
fmt.Fprintf(w, "pong")
}
func main() {
prometheus.MustRegister(pingCounter)
http.HandleFunc("/ping", ping)
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":8090", nil)
if err != nil {
return
}
}
prometheus.yaml
global:
scrape_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "super_ping"
static_configs:
- targets: ["localhost:8090"]



被折叠的 条评论
为什么被折叠?



