每期一个小窍门:全世界最简单的go exporter 例子

25 篇文章 0 订阅
2 篇文章 0 订阅

调用链路我大概描述下

  1. postman 调用 /ping 接口
  2. 标签为 ping_request_count 的prometheus sdk 侧 内部的 counter 计数器+1
  3. prometheus 拉取配置的job的metrics 接口 xxxx:8090
  4. 在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"]

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静谧之心

感谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值