-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (165 loc) · 6.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/user"
"strings"
stdlog "log"
"github.com/go-kit/log"
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"cloud.google.com/go/compute/metadata"
"github.com/7onn/gcp-idleness-exporter/collector"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/tidwall/gjson"
)
var (
gcpProjectID = kingpin.Flag(
"project-id", "GCP Project ID to monitor. ($GCP_PROJECT_ID)",
).Envar("GCP_PROJECT_ID").String()
gcpRegions = kingpin.Flag(
"regions", "Comma-separated GCP regions to monitor. e.g: asia-east1,southamerica-east1,us-east1 ($GCP_REGIONS)",
).Envar("GCP_REGIONS").String()
monitoredRegions []string
gcpMaxRetries = kingpin.Flag(
"max-retries", "Max number of retries that should be attempted on 503 errors from gcp. ($GCP_EXPORTER_MAX_RETRIES)\n",
).Envar("GCP_EXPORTER_MAX_RETRIES").Default("0").Int()
gcpHttpTimeout = kingpin.Flag(
"http-timeout", "How long in seconds should gcp_exporter wait for a result from the Google API ($GCP_EXPORTER_HTTP_TIMEOUT)",
).Envar("GCP_EXPORTER_HTTP_TIMEOUT").Default("10s").Duration()
gcpMaxBackoffDuration = kingpin.Flag(
"max-backoff", "Max time in seconds between each request in an exp backoff scenario ($GCP_EXPORTER_MAX_BACKOFF_DURATION)",
).Envar("GCP_EXPORTER_MAX_BACKOFF_DURATION").Default("5s").Duration()
gcpBackoffJitterBase = kingpin.Flag(
"backoff-jitter", "The amount in seconds of jitter to introduce in a exp backoff scenario ($GCP_EXPORTER_BACKODFF_JITTER_BASE)",
).Envar("GCP_EXPORTER_BACKOFF_JITTER_BASE").Default("1s").Duration()
gcpRetryStatuses = kingpin.Flag(
"retry-statuses", "The HTTP statuses that should trigger a retry ($GCP_EXPORTER_RETRY_STATUSES)",
).Envar("GCP_EXPORTER_RETRY_STATUSES").Default("503").Ints()
disableDefaultCollectors = kingpin.Flag(
"collector.disable-defaults",
"Set all collectors to disabled by default.",
).Default("false").Bool()
)
type MetricsHandler struct {
exporterMetricsRegistry *prometheus.Registry
logger log.Logger
}
func newMetricsHandler(logger log.Logger) *MetricsHandler {
h := &MetricsHandler{
exporterMetricsRegistry: prometheus.NewRegistry(),
logger: logger,
}
h.exporterMetricsRegistry.MustRegister(
promcollectors.NewProcessCollector(promcollectors.ProcessCollectorOpts{}),
promcollectors.NewGoCollector(),
)
return h
}
// ServeHTTP implements http.Handler.
func (h *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
gcpCollector, err := collector.NewGCPCollector(ctx, h.logger, *gcpProjectID, monitoredRegions)
if err != nil {
level.Error(h.logger).Log("msg", "couldn't create collector", "err", err)
}
for n, c := range gcpCollector.Collectors {
level.Info(h.logger).Log("collector", n, "metrics", fmt.Sprintf("%+v", c.ListMetrics()))
}
pr := prometheus.NewRegistry()
pr.MustRegister(version.NewCollector("gcp_idleness_exporter"))
if err = pr.Register(gcpCollector); err != nil {
level.Error(h.logger).Log("msg", "couldn't register gcp_idleness_exporter collector", "err", err)
}
handler := promhttp.HandlerFor(
prometheus.Gatherers{h.exporterMetricsRegistry, pr},
promhttp.HandlerOpts{
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0),
ErrorHandling: promhttp.ContinueOnError,
Registry: h.exporterMetricsRegistry,
},
)
handler = promhttp.InstrumentMetricHandler(h.exporterMetricsRegistry, handler)
handler.ServeHTTP(w, r)
}
func main() {
var (
listenAddress = kingpin.Flag("listen-address", "Address to listen on for web interface and telemetry.").Envar("LISTEN_ADDRESS").Default(":5000").String()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("gcp-idleness-exporter"))
kingpin.CommandLine.UsageWriter(os.Stdout)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
collector.GCPHttpTimeout = *gcpHttpTimeout
collector.GCPMaxRetries = *gcpMaxRetries
collector.GCPRetryStatuses = *gcpRetryStatuses
collector.GCPBackoffJitterBase = *gcpBackoffJitterBase
collector.GCPMaxBackoffDuration = *gcpMaxBackoffDuration
logger := promlog.New(promlogConfig)
if *disableDefaultCollectors {
collector.DisableDefaultCollectors()
}
level.Info(logger).Log("msg", "Starting gcp-idleness-exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
if user, err := user.Current(); err == nil && user.Uid == "0" {
level.Warn(logger).Log("msg", "gcp-idleness-exporter is running as root user. This exporter is designed to run as unpriviledged user, root is not required.")
}
// Detect Project ID
if *gcpProjectID == "" {
credentialsFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credentialsFile != "" {
c, err := ioutil.ReadFile(credentialsFile)
if err != nil {
level.Error(logger).Log("msg", fmt.Sprintf("Unable to read %s", credentialsFile), "err", err)
}
projectId := gjson.GetBytes(c, "project_id")
if projectId.String() == "" {
level.Error(logger).Log("msg", fmt.Sprintf("Could not retrieve Project ID from %s", credentialsFile))
}
*gcpProjectID = projectId.String()
} else {
// Get project id from metadata
client := metadata.NewClient(&http.Client{})
project_id, err := client.ProjectID()
if err != nil {
level.Error(logger).Log("msg", fmt.Sprintf("error getting GCP project ID from metadata: %+v", err))
}
*gcpProjectID = project_id
}
}
if *gcpProjectID == "" {
level.Error(logger).Log("msg", "GCP Project ID cannot be empty")
}
monitoredRegions = strings.Split(*gcpRegions, ",")
level.Info(logger).Log("msg", fmt.Sprintf("Starting exporter for project %s at %v", *gcpProjectID, monitoredRegions))
level.Info(logger).Log("msg", fmt.Sprintf("Listening on %s", *listenAddress))
http.Handle("/metrics", newMetricsHandler(logger))
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("aaa aaa aaa aaa staying alive staying alive"))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head>
<title>gcp-idleness-exporter</title>
</head>
<body>
<h1>GCP idleness exporter</h1>
<p>
<a href='/metrics'>Metrics</a>
</p>
</body>
</html>`))
})
level.Error(logger).Log("err", http.ListenAndServe(*listenAddress, nil))
}