-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
185 lines (151 loc) · 5.57 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
package main
import (
"net/http"
"os"
"runtime"
"time"
arg "github.com/alexflint/go-arg"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/yoannma/scaleway_exporter/collector"
)
var (
// Version of this binary.
Version string //nolint:gochecknoglobals // LDFlags
// Revision or Commit this binary was built from.
Revision string //nolint: gochecknoglobals // LDFlags
// BuildDate this binary was built.
BuildDate string //nolint:gochecknoglobals // LDFlags
// GoVersion running this binary.
GoVersion = runtime.Version() //nolint:gochecknoglobals // LDFlags
// StartTime has the time this was started.
StartTime = time.Now() //nolint:gochecknoglobals // LDFlags
)
// Config gets its content from env and passes it on to different packages.
type Config struct {
Debug bool `arg:"env:DEBUG"`
ScalewayAccessKey string `arg:"env:SCALEWAY_ACCESS_KEY"`
ScalewaySecretKey string `arg:"env:SCALEWAY_SECRET_KEY"`
ScalewayRegion scw.Region `arg:"env:SCALEWAY_REGION"`
ScalewayZone scw.Zone `arg:"env:SCALEWAY_ZONE"`
ScalewayOrganizationID string `arg:"env:SCALEWAY_ORGANIZATION_ID"`
HTTPTimeout int `arg:"env:HTTP_TIMEOUT"`
WebAddr string `arg:"env:WEB_ADDR"`
WebPath string `arg:"env:WEB_PATH"`
DisableBillingCollector bool `arg:"--disable-billing-collector"`
DisableBucketCollector bool `arg:"--disable-bucket-collector"`
DisableDatabaseCollector bool `arg:"--disable-database-collector"`
DisableLoadBalancerCollector bool `arg:"--disable-loadbalancer-collector"`
DisableRedisCollector bool `arg:"--disable-redis-collector"`
}
func main() {
_ = godotenv.Load()
c := Config{
HTTPTimeout: 5000,
WebPath: "/metrics",
WebAddr: ":9503",
DisableBillingCollector: false,
DisableBucketCollector: false,
DisableDatabaseCollector: false,
DisableLoadBalancerCollector: false,
}
arg.MustParse(&c)
filterOption := level.AllowInfo()
if c.Debug {
filterOption = level.AllowDebug()
}
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, filterOption)
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
if c.ScalewayAccessKey == "" {
_ = level.Error(logger).Log("msg", "Scaleway Access Key is required")
os.Exit(1)
}
if c.ScalewaySecretKey == "" {
_ = level.Error(logger).Log("msg", "Scaleway Secret Key is required")
os.Exit(1)
}
var regions []scw.Region
if c.ScalewayRegion == "" {
_ = level.Info(logger).Log("msg", "Scaleway Region is set to ALL")
regions = scw.AllRegions
} else {
regions = []scw.Region{c.ScalewayRegion}
}
var zones []scw.Zone
if c.ScalewayZone == "" {
_ = level.Info(logger).Log("msg", "Scaleway Zone is set to ALL")
zones = scw.AllZones
} else {
zones = []scw.Zone{c.ScalewayZone}
}
_ = level.Info(logger).Log(
"msg", "starting scaleway_exporter",
"version", Version,
"revision", Revision,
"buildDate", BuildDate,
"goVersion", GoVersion,
)
client, err := scw.NewClient(
// Get your credentials at https://console.scaleway.com/account/credentials
scw.WithDefaultRegion(regions[0]),
scw.WithAuth(c.ScalewayAccessKey, c.ScalewaySecretKey),
)
if err != nil {
_ = level.Error(logger).Log("msg", "Scaleway client initialization error", "err", err)
os.Exit(1)
}
timeout := time.Duration(c.HTTPTimeout) * time.Millisecond
errors := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "scaleway_errors_total",
Help: "The total number of errors per collector",
}, []string{"collector"})
r := prometheus.NewRegistry()
r.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
r.MustRegister(collectors.NewGoCollector())
r.MustRegister(errors)
r.MustRegister(collector.NewExporterCollector(logger, Version, Revision, BuildDate, GoVersion, StartTime))
if !c.DisableBillingCollector && c.ScalewayOrganizationID != "" {
r.MustRegister(collector.NewBillingCollector(logger, errors, client, timeout, c.ScalewayOrganizationID))
}
if !c.DisableBucketCollector {
r.MustRegister(collector.NewBucketCollector(logger, errors, client, timeout, regions))
}
if !c.DisableDatabaseCollector {
r.MustRegister(collector.NewDatabaseCollector(logger, errors, client, timeout, regions))
}
if !c.DisableLoadBalancerCollector {
r.MustRegister(collector.NewLoadBalancerCollector(logger, errors, client, timeout, zones))
}
if !c.DisableRedisCollector {
r.MustRegister(collector.NewRedisCollector(logger, errors, client, timeout, zones))
}
http.Handle(c.WebPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Scaleway Exporter</title></head>
<body>
<h1>Scaleway Exporter</h1>
<p><a href="` + c.WebPath + `">Metrics</a></p>
</body>
</html>`))
})
_ = level.Info(logger).Log("msg", "listening", "addr", c.WebAddr)
server := &http.Server{
Addr: c.WebAddr,
ReadHeaderTimeout: 5 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
_ = level.Error(logger).Log("msg", "http ListenAndServe error", "err", err)
os.Exit(1)
}
}