Ai
5 Star 6 Fork 4

zstackio/zstack-vyos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
prometheus.go 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
package plugin
import (
"fmt"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
glog "log"
"net/http"
"zstack-vyos/server"
"zstack-vyos/utils"
)
const (
PROMETHEUS_METRIC_PATH = "/metrics"
)
type MetricCollector interface {
// Get new metrics and expose them via prometheus registry.
Update(ch chan<- prom.Metric) error
Describe(ch chan<- *prom.Desc) error
}
type prometheusServer struct {
collectors []MetricCollector
}
var promServer = &prometheusServer{
collectors: make([]MetricCollector, 0),
}
func (p *prometheusServer) Describe(ch chan<- *prom.Desc) {
for _, cl := range p.collectors {
err := cl.Describe(ch)
utils.LogError(err)
}
}
func (p *prometheusServer) Collect(ch chan<- prom.Metric) {
for _, cl := range p.collectors {
err := cl.Update(ch)
utils.LogError(err)
}
}
func RegisterPrometheusCollector(collector MetricCollector) {
promServer.collectors = append(promServer.collectors, collector)
}
type errorLogWriter struct{}
func (errorLogWriter) Write(b []byte) (int, error) {
log.Error(string(b))
return len(b), nil
}
func HandlePrometheusScrape(w http.ResponseWriter, r *http.Request) {
reg := prom.NewRegistry()
err := reg.Register(promServer)
utils.LogError(err)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't register collector: %s\n", err)))
return
}
gatherers := prom.Gatherers{
// prom.DefaultGatherer,
reg,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorLog: glog.New(&errorLogWriter{}, "", 0),
ErrorHandling: promhttp.ContinueOnError,
})
h.ServeHTTP(w, r)
}
func PrometheusEntryPoint() {
server.RegisterRawHttpHandler(PROMETHEUS_METRIC_PATH, HandlePrometheusScrape)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zstackio/zstack-vyos.git
git@gitee.com:zstackio/zstack-vyos.git
zstackio
zstack-vyos
zstack-vyos
master

搜索帮助