-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 2.09 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
package main
import (
"hadoop_jmx_exporter/collector"
"net/http"
"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
_ "github.com/sijms/go-ora/v2"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
// Required for debugging
// _ "net/http/pprof"
)
var (
// Version will be set at build time.
Version = "0.0.0.dev"
scrapePath = kingpin.Flag("web.scrape-path", "Path under which to expose metrics. (env: TELEMETRY_PATH)").Default(getEnv("TELEMETRY_PATH", "/scrape")).String()
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9070")
)
func main() {
promLogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promLogConfig)
kingpin.HelpFlag.Short('\n')
kingpin.Version(version.Print("hadoop_jmx_exporter"))
kingpin.Parse()
logger := promlog.New(promLogConfig)
version.Version = Version
level.Info(logger).Log("msg", "Starting hadoop_jmx_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build", version.BuildContext())
http.HandleFunc("/scrape", scrapeHandle(logger))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<html><head><title>Hadoop Jmx Exporter " + Version + "</title></head><body><h1>Hadoop Jmx Exporter " + Version + "</h1><p><a href='" + *scrapePath + "'>Scrape</a></p></body></html>"))
})
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, logger); err != nil {
level.Error(logger).Log("msg", "Listening error", "reason", err)
os.Exit(1)
}
}
func scrapeHandle(logger log.Logger) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
collector.Handler(w, r, logger)
}
}
// getEnv returns the value of an environment variable, or returns the provided fallback value
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}