-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (83 loc) · 2.27 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
91
92
93
94
95
96
97
98
package main
import (
"flag"
stdlog "log"
"net/http"
"os"
"runtime"
"time"
"github.com/DazWilkin/fly-exporter/collector"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace string = "fly"
subsystem string = "exporter"
version string = "v0.0.1"
)
var (
// GitCommit is the git commit value and is expected to be set during build
GitCommit string
// GoVersion is the Golang runtime version
GoVersion = runtime.Version()
// OSVersion is the OS version (uname --kernel-release) and is expected to be set during build
OSVersion string
// StartTime is the start time of the exporter represented as a UNIX epoch
StartTime = time.Now().Unix()
)
var (
endpoint = flag.String("endpoint", "0.0.0.0:8080", "The endpoint of the HTTP server")
)
var (
log logr.Logger
)
var (
token = os.Getenv("TOKEN")
)
func handleHealthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
func handleRoot(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("<a href=/metrics>metrics</a>"))
}
func main() {
log = stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
log = log.WithName("main")
if token == "" {
msg := "environment variable `TOKEN` is required (use `flyctl auth token`)"
log.Info(msg)
panic(msg)
}
flag.Parse()
if *endpoint == "" {
msg := "expected flag `--endpoint"
log.Info(msg)
panic(msg)
}
registry := prometheus.NewRegistry()
s := collector.System{
Namespace: namespace,
Subsystem: subsystem,
Version: version,
}
b := collector.Build{
OsVersion: OSVersion,
GoVersion: GoVersion,
GitCommit: GitCommit,
StartTime: StartTime,
}
registry.MustRegister(collector.NewExporterCollector(s, b, log))
registry.MustRegister(collector.NewFlyCollector(s, token, log))
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(handleRoot))
mux.Handle("/healthz", http.HandlerFunc(handleHealthz))
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
log.Info("Server starting",
"endpoint", *endpoint,
)
log.Error(http.ListenAndServe(*endpoint, mux), "unable to start server")
}