1 Star 2 Fork 0

dreammo/prom-elastic-alert

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
dreammo 提交于 2022-12-06 18:15 . help flag
package main
import (
"fmt"
"github.com/dream-mo/prom-elastic-alert/boot"
"github.com/dream-mo/prom-elastic-alert/conf"
"github.com/dream-mo/prom-elastic-alert/utils/logger"
"github.com/dream-mo/prom-elastic-alert/utils/redis"
"github.com/dream-mo/prom-elastic-alert/utils/xtime"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"
)
func main() {
var opts conf.FlagOption
defer func() {
if e := recover(); e != nil {
fmt.Printf("%s\n", e)
if opts.Debug {
debug.PrintStack()
}
}
}()
p := flags.NewParser(&opts, flags.HelpFlag)
_, err := p.ParseArgs(os.Args)
if err != nil {
panic(err)
}
logger.SetLogLevel(opts.GetLogLevel())
xtime.FixedZone(opts.Zone)
c := conf.GetAppConfig(opts.ConfigPath)
redis.Setup()
ea := boot.NewElasticAlert(c, &opts)
ea.Start()
if c.Exporter.Enabled {
metrics := boot.NewRuleStatusCollector(ea)
reg := prometheus.NewPedanticRegistry()
err := reg.Register(metrics)
if err != nil {
t := fmt.Sprintf("Register prometheus collector error: %s", err.Error())
panic(t)
}
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
reg,
}
h := promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle("/metrics", h)
http.HandleFunc("/alert/message", boot.RenderAlertMessage)
e := http.ListenAndServe(c.Exporter.ListenAddr, nil)
if e != nil {
t := fmt.Sprintf("Prometheus exporter start error: %s", e.Error())
panic(t)
}
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
for {
s := <-quit
switch s {
case syscall.SIGHUP:
c := conf.GetAppConfig(opts.ConfigPath)
ea.SetAppConf(c)
logger.Logger.Infoln("Reload application config success!")
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
ea.Stop()
logger.Logger.Infoln("exiting...")
return
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dreammo/prom-elastic-alert.git
git@gitee.com:dreammo/prom-elastic-alert.git
dreammo
prom-elastic-alert
prom-elastic-alert
main

搜索帮助