forked from corundex/database_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 2.5 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"runtime"
_ "net/http/pprof"
"github.com/Corundex/database_exporter/exporter"
log "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
)
func init() {
prometheus.MustRegister(version.NewCollector("database_exporter"))
}
func main() {
if os.Getenv("DEBUG") != "" {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
}
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9285", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("config.file", "database_exporter.yml", "Database Exporter configuration file name.")
)
// Override --alsologtostderr default value.
if alsoLogToStderr := flag.Lookup("alsologtostderr"); alsoLogToStderr != nil {
alsoLogToStderr.DefValue = "true"
alsoLogToStderr.Value.Set("true")
}
// Override the config.file default with the CONFIG environment variable, if set. If the flag is explicitly set, it
// will end up overriding either.
if envConfigFile := os.Getenv("CONFIG"); envConfigFile != "" {
*configFile = envConfigFile
}
flag.Parse()
if *showVersion {
fmt.Println(version.Print("database_exporter"))
os.Exit(0)
}
log.Infof("Starting Database Exporter %s %s", version.Info(), version.BuildContext())
exporter, err := exporter.NewExporter(*configFile)
if err != nil {
log.Fatalf("Error creating exporter: %s", err)
}
// Setup and start webserver.
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
http.HandleFunc("/", HomeHandlerFunc(*metricsPath))
http.HandleFunc("/config", ConfigHandlerFunc(*metricsPath, exporter))
http.Handle(*metricsPath, ExporterHandlerFor(exporter))
// Expose exporter metrics separately, for debugging purposes.
http.Handle("/database_exporter_metrics", promhttp.Handler())
log.Infof("Listening on %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
// LogFunc is an adapter to allow the use of any function as a promhttp.Logger. If f is a function, LogFunc(f) is a
// promhttp.Logger that calls f.
type LogFunc func(args ...interface{})
// Println implements promhttp.Logger.
func (log LogFunc) Println(args ...interface{}) {
log(args)
}