Ai
4 Star 19 Fork 32

少林码僧/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pusher.go 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
webphp 提交于 2022-06-19 15:29 +08:00 . mysql
package prome
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"log"
"net/http"
"os"
"time"
)
type stdLogger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}
const (
DefaultPushInterval = 60 * time.Second
)
var (
pushScheduler *PushScheduler
PromeStdLogger stdLogger
DefaultBuckets = []float64{5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800, 1000, 1500, 2000, 2500, 3000, 4000, 5000, 6000, 7000, 8000, 10000, 20000, 30000}
)
type PushScheduler struct {
interval time.Duration
pusher *push.Pusher
jobName string
}
func init() {
PromeStdLogger = log.New(os.Stdout, "[Prome] ", log.LstdFlags|log.Lshortfile)
}
func (ps *PushScheduler) run() {
t := time.NewTicker(ps.interval)
for {
select {
case <-t.C:
if err := ps.pusher.Add(); err != nil {
PromeStdLogger.Print(ps.jobName, err)
}
}
}
}
func InitPromethues(Url string, pushInterval time.Duration, jobName string, client *http.Client, cs ...prometheus.Collector) {
if pushInterval == 0 {
pushInterval = DefaultPushInterval
}
registry := prometheus.NewRegistry()
registry.MustRegister(cs...)
pusher := push.New(Url, jobName).Gatherer(registry)
if client != nil {
pusher.Client(client)
}
pushScheduler = &PushScheduler{
interval: pushInterval,
pusher: pusher,
jobName: jobName,
}
go pushScheduler.run()
}
func GetHostName() string {
name, err := os.Hostname()
if err != nil {
PromeStdLogger.Print(err)
}
return name
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/phper95/pkg.git
git@gitee.com:phper95/pkg.git
phper95
pkg
pkg
38d933c4cdfa

搜索帮助