pushgateway 替代
为啥要替代?
因为pushgateway不好用。
思路
定义一个webServer
- port1:用于提交监控数据
- port2:用于prometheus配置target
缺点说明:这个不是及时推送到prometheus,依然是被动提供数据
port1
用来上传各个脚本采集的数据,临时传到webServer上。
数据结构,按照prometheus的数据结构来定义,
metrics_name{tag_name="tag_value"} filed_value
#当然也可以像这样加上说明:
#HELP metrics_name some word to explain
#TYPE metrics_name counter\Gauge\...
metrics_name{tag_name="tag_value"} field_value
port2
汇总port1搜集到的数据汇总起来,用于prometheus配置target。
至于数据怎么存、怎么汇总就看具体需求了。举个小栗子
func Start() {
//for prometheus target
http.HandleFunc("/metrics", metrics)
//upload metrics
http.HandleFunc("/upload", upload())
fs := http.FileServer(http.Dir(uploadPath))
http.Handle("/files/", http.StripPrefix("/files", fs))
//设置监听的端口
err := http.ListenAndServe(":8045", nil)
//异常
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func metrics(w http.ResponseWriter, r *http.Request) {
fmt.Println("metrics.txt")
w.Header().Set("Access-Control-Allow-Origin", "*")
content, _ := ioutil.ReadFile("./metrics.txt")
w.Write(content)
}
func upload() http.HandlerFunc {
//这里数据汇总,存到metrics.txt
}