-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
81 lines (70 loc) · 2.62 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
package main
import (
"context"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/kaizendorks/terraform-cloud-exporter/internal/collector"
"github.com/kaizendorks/terraform-cloud-exporter/internal/setup"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Build information. Populated at build-time via ldflags.
var (
Version string
Commit string
GoVersion = runtime.Version()
BuildDate string
)
func newHandler(metrics collector.Metrics, config setup.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Use request context for cancellation when connection gets closed.
ctx := r.Context()
// If a timeout is configured via the Prometheus header, add it to the context.
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
timeoutSeconds, err := strconv.ParseFloat(v, 64)
if err != nil {
level.Error(config.Logger).Log("msg", "Failed to parse timeout from Prometheus header", "err", err)
} else {
// Create new timeout context with request context as parent.
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
// Overwrite request with timeout context.
r = r.WithContext(ctx)
}
}
registry := prometheus.NewRegistry()
registry.MustRegister(collector.New(ctx, config, metrics))
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
func main() {
config := setup.NewConfig()
level.Info(config.Logger).Log("msg", "Starting tf_exporter", "version", Version, "revision", Commit)
level.Debug(config.Logger).Log("msg", "Build Context", "go", GoVersion, "date", BuildDate)
handlerFunc := newHandler(collector.NewMetrics(), config)
http.Handle("/metrics", promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Terraform Cloud/Enterprise Exporter</title></head>
<body>
<h1>Terraform Cloud/Enterprise Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
level.Info(config.Logger).Log("msg", "Listening on address", "address", config.ListenAddress)
if err := http.ListenAndServe(config.ListenAddress, nil); err != nil {
level.Error(config.Logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}