-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
90 lines (80 loc) · 2.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
}
}
}