-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_billing_exporter.go
77 lines (67 loc) · 2.27 KB
/
github_billing_exporter.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
package main
import (
"fmt"
"github.com/borisputerka/github_billing_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address on which to expose metrics.",
).Envar("LISTEN_ADDRESS").Default(":9776").String()
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Envar("METRICS_PATH").Default("/metrics").String()
gracefulStop = make(chan os.Signal)
)
func main() {
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
signal.Notify(gracefulStop, syscall.SIGHUP)
signal.Notify(gracefulStop, syscall.SIGQUIT)
exporter, err := collector.NewBillingCollector(logger)
if err != nil {
fmt.Errorf("couldn't create collector: %s", err)
}
prometheus.MustRegister(exporter)
prometheus.MustRegister(version.NewCollector("github_billing_exporter"))
level.Info(logger).Log("msg", "Starting exporter", "version", version.Info())
level.Info(logger).Log("Build context", version.BuildContext())
level.Info(logger).Log("msg", "Starting Server", "listening address", *listenAddress)
// listener for the termination signals from the OS
go func() {
level.Info(logger).Log("msg", "listening and wait for graceful stop")
sig := <-gracefulStop
level.Info(logger).Log("msg", "Shutting exporter", "caught sig", sig)
time.Sleep(2 * time.Second)
os.Exit(0)
}()
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>GitHub Billing Exporter</title></head>
<body>
<h1>GitHub Billing Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
level.Error(logger).Log("msg", http.ListenAndServe(*listenAddress, nil))
}