-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
71 lines (59 loc) · 1.81 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
package main
import (
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"sql_zh_exporter/config"
"sql_zh_exporter/util/log"
"net/http"
)
func init() {
prometheus.MustRegister(version.NewCollector("sql_exporter"))
}
func main() {
var (
configFile = flag.String("config.file", "./config.yml", "SQL Exporter config file")
jobConfigFile = flag.String("job.config.file", "./job_config.yml", "SQL Exporter job config file")
)
flag.Parse()
log.Init(*configFile)
log.Info().Fields(map[string]interface{}{
"message": "Starting sql_exporter",
"version_info": version.Info(),
"build_context": version.BuildContext(),
}).Send()
exporter, err := NewExporter(*jobConfigFile)
if err != nil {
log.Panic().Fields(map[string]interface{}{
"action": "starting exporter",
"error": err,
}).Send()
}
prometheus.MustRegister(exporter)
// setup and start webserver
metricPath := "/metrics"
http.Handle(metricPath, promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(fmt.Sprintf(`<html>
<head><title>SQL Exporter</title></head>
<body>
<h1>SQL Exporter</h1>
<p><a href=" %s ">Metrics</a></p>
</body>
</html>
`, metricPath)))
})
log.Info().Fields(map[string]interface{}{
"action": "start server",
"listenAddress": config.GetConfig(ServerPort, *configFile),
}).Send()
if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", config.GetConfig(ServerPort, *configFile)), nil); err != nil {
log.Error().Fields(map[string]interface{}{
"action": "starting HTTP server",
"error": err,
}).Send()
}
}