forked from justwatchcom/sql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (73 loc) · 2.41 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
)
func init() {
prometheus.MustRegister(version.NewCollector("sql_exporter"))
}
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9237", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("config.file", os.Getenv("CONFIG"), "SQL Exporter configuration file name.")
)
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("sql_exporter"))
os.Exit(0)
}
// init logger
logger := log.NewJSONLogger(os.Stdout)
// set the allowed log level filter
switch strings.ToLower(os.Getenv("LOGLEVEL")) {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
case "info":
logger = level.NewFilter(logger, level.AllowInfo())
case "warn":
logger = level.NewFilter(logger, level.AllowWarn())
case "error":
logger = level.NewFilter(logger, level.AllowError())
default:
logger = level.NewFilter(logger, level.AllowAll())
}
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
logger.Log("msg", "Starting sql_exporter", "version_info", version.Info(), "build_context", version.BuildContext())
exporter, err := NewExporter(logger, *configFile)
if err != nil {
level.Error(logger).Log("msg", "Error starting exporter", "err", err)
os.Exit(1)
}
prometheus.MustRegister(exporter)
// setup and start webserver
http.Handle(*metricsPath, 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(`<html>
<head><title>SQL Exporter</title></head>
<body>
<h1>SQL Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>
`))
})
level.Info(logger).Log("msg", "Listening", "listenAddress", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server:", "err", err)
os.Exit(1)
}
}