forked from DazWilkin/gcp-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (76 loc) · 3.26 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"runtime"
"time"
"github.com/DazWilkin/gcp-exporter/collector"
"github.com/DazWilkin/gcp-exporter/gcp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
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 (
filter = flag.String("filter", "", "Filter the results of the request")
pagesize = flag.Int64("max_projects", 10, "Maximum number of projects to include")
endpoint = flag.String("endpoint", ":9402", "The endpoint of the HTTP server")
metricsPath = flag.String("path", "/metrics", "The path on which Prometheus metrics will be served")
)
func handleHealthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
func handleRoot(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
fmt.Fprint(w, "<h2>Google Cloud Platform Resources Exporter</h2>")
fmt.Fprint(w, "<ul>")
fmt.Fprintf(w, "<li><a href=\"%s\">metrics</a></li>", *metricsPath)
fmt.Fprintf(w, "<li><a href=\"/healthz\">healthz</a></li>")
fmt.Fprint(w, "</ul>")
}
func main() {
flag.Parse()
if GitCommit == "" {
log.Println("[main] GitCommit value unchanged: expected to be set during build")
}
if OSVersion == "" {
log.Println("[main] OSVersion value unchanged: expected to be set during build")
}
// Objects that holds GCP-specific resources (e.g. projects)
account := gcp.NewAccount()
registry := prometheus.NewRegistry()
registry.MustRegister(collector.NewExporterCollector(OSVersion, GoVersion, GitCommit, StartTime))
// ProjectCollector is a special case
// When it runs it replaces the Exporter's list of GCP projects
// The other collectors are dependent on this list of projects
registry.MustRegister(collector.NewProjectsCollector(account, *filter, *pagesize))
registry.MustRegister(collector.NewArtifactRegistryCollector(account))
registry.MustRegister(collector.NewCloudRunCollector(account))
registry.MustRegister(collector.NewComputeCollector(account))
registry.MustRegister(collector.NewEndpointsCollector(account))
registry.MustRegister(collector.NewEventarcCollector(account))
registry.MustRegister(collector.NewFunctionsCollector(account))
registry.MustRegister(collector.NewKubernetesCollector(account))
registry.MustRegister(collector.NewLoggingCollector(account))
registry.MustRegister(collector.NewMonitoringCollector(account))
registry.MustRegister(collector.NewSchedulerCollector(account))
registry.MustRegister(collector.NewStorageCollector(account))
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(handleRoot))
mux.Handle("/healthz", http.HandlerFunc(handleHealthz))
mux.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
log.Printf("[main] Server starting (%s)", *endpoint)
log.Printf("[main] metrics served on: %s", *metricsPath)
log.Fatal(http.ListenAndServe(*endpoint, mux))
}