-
Notifications
You must be signed in to change notification settings - Fork 18
/
couchbase_exporter.go
380 lines (357 loc) · 13.8 KB
/
couchbase_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2019 Adel Abdelhak.
// Use of this source code is governed by the Apache
// license that can be found in the LICENSE.txt file.
package main
import (
"flag"
"net/http"
"os"
"strconv"
"time"
"github.com/blakelead/couchbase_exporter/collector"
"github.com/prometheus/client_golang/prometheus/promhttp"
cl "github.com/blakelead/confloader"
log "github.com/sirupsen/logrus"
)
// Options struct regroups runtime parameters
type Options struct {
serverListenAddress string
serverMetricsPath string
serverTimeout time.Duration
dbUsername string
dbPassword string
dbURI string
dbTimeout time.Duration
tlsEnabled bool
tlsSkipInsecure bool
tlsCACert string
tlsClientCert string
tlsClientKey string
logLevel string
logFormat string
scrapeCluster bool
scrapeNode bool
scrapeBucket bool
scrapeXDCR bool
configFile string
}
var (
version = "0.9.0"
runtimeOptions = &Options{}
)
func main() {
// Initialize global variables, initialize logger and display user defined values.
initEnv()
initLogger()
displayInfo()
// Context encapsulates connection and scraping details for the exporters.
context := collector.Context{
URI: runtimeOptions.dbURI,
Username: runtimeOptions.dbUsername,
Password: runtimeOptions.dbPassword,
Timeout: runtimeOptions.dbTimeout,
TLSEnabled: runtimeOptions.tlsEnabled,
TLSSkipInsecure: runtimeOptions.tlsSkipInsecure,
TLSCACert: runtimeOptions.tlsCACert,
TLSClientCert: runtimeOptions.tlsClientCert,
TLSClientKey: runtimeOptions.tlsClientKey,
ScrapeCluster: runtimeOptions.scrapeCluster,
ScrapeNode: runtimeOptions.scrapeNode,
ScrapeBucket: runtimeOptions.scrapeBucket,
ScrapeXDCR: runtimeOptions.scrapeXDCR,
}
// Exporters are initialized, meaning that metrics files are loaded and
// Exporter objects are created and filled with metrics metadata.
collector.InitExporters(context)
// Handle metrics path with the prometheus handler.
http.Handle(runtimeOptions.serverMetricsPath, promhttp.Handler())
// Handle paths other than given metrics path.
if runtimeOptions.serverMetricsPath != "/" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<body><p>See <a href="` + runtimeOptions.serverMetricsPath + `">Metrics</a></p></body>`))
})
}
// Create the server with provided listen address and server timeout.
s := &http.Server{
Addr: runtimeOptions.serverListenAddress,
ReadTimeout: runtimeOptions.serverTimeout,
}
// Start the server.
log.Info("Started listening at ", runtimeOptions.serverListenAddress)
log.Fatal(s.ListenAndServe())
}
// FlagPresent returns true if the flag name passed as the
// function parameter is found in the command-line flags.
func FlagPresent(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func initEnv() {
// Default parameters.
runtimeOptions.serverListenAddress = "127.0.0.1:9191"
runtimeOptions.serverMetricsPath = "/metrics"
runtimeOptions.serverTimeout = 10 * time.Second
runtimeOptions.dbURI = "http://localhost:8091"
runtimeOptions.dbTimeout = 10 * time.Second
runtimeOptions.tlsEnabled = false
runtimeOptions.tlsSkipInsecure = false
runtimeOptions.tlsCACert = ""
runtimeOptions.tlsClientCert = ""
runtimeOptions.tlsClientKey = ""
runtimeOptions.logLevel = "info"
runtimeOptions.logFormat = "text"
runtimeOptions.scrapeCluster = true
runtimeOptions.scrapeNode = true
runtimeOptions.scrapeBucket = true
runtimeOptions.scrapeXDCR = true
runtimeOptions.configFile = ""
// Get command-line values.
var cmdlineOptions = &Options{}
flag.StringVar(&cmdlineOptions.configFile, "config.file", runtimeOptions.configFile, "Path to configuration file.")
flag.StringVar(&cmdlineOptions.serverListenAddress, "web.listen-address", runtimeOptions.serverListenAddress, "Address to listen on for HTTP requests.")
flag.StringVar(&cmdlineOptions.serverMetricsPath, "web.telemetry-path", runtimeOptions.serverMetricsPath, "Path under which to expose metrics.")
flag.DurationVar(&cmdlineOptions.serverTimeout, "web.timeout", runtimeOptions.serverTimeout, "Server read timeout in seconds.")
flag.StringVar(&cmdlineOptions.dbURI, "db.uri", runtimeOptions.dbURI, "Couchbase node URI with port.")
flag.DurationVar(&cmdlineOptions.dbTimeout, "db.timeout", runtimeOptions.dbTimeout, "Couchbase client timeout in seconds.")
flag.BoolVar(&cmdlineOptions.tlsEnabled, "tls.enabled", runtimeOptions.tlsEnabled, "If true, TLS is used when communicating with cluster.")
flag.BoolVar(&cmdlineOptions.tlsSkipInsecure, "tls.skip-insecure", runtimeOptions.tlsSkipInsecure, "If true, certificate won't be verified.")
flag.StringVar(&cmdlineOptions.tlsCACert, "tls.ca-cert", runtimeOptions.tlsCACert, "Root certificate of the cluster.")
flag.StringVar(&cmdlineOptions.tlsClientCert, "tls.client-cert", runtimeOptions.tlsClientCert, "Client certificate.")
flag.StringVar(&cmdlineOptions.tlsClientKey, "tls.client-key", runtimeOptions.tlsClientKey, "Client private key.")
flag.StringVar(&cmdlineOptions.logLevel, "log.level", runtimeOptions.logLevel, "Log level: info, debug, warn, error, fatal.")
flag.StringVar(&cmdlineOptions.logFormat, "log.format", runtimeOptions.logFormat, "Log format: text or json.")
flag.BoolVar(&cmdlineOptions.scrapeCluster, "scrape.cluster", runtimeOptions.scrapeCluster, "If false, cluster metrics won't be scraped.")
flag.BoolVar(&cmdlineOptions.scrapeNode, "scrape.node", runtimeOptions.scrapeNode, "If false, node metrics won't be scraped.")
flag.BoolVar(&cmdlineOptions.scrapeBucket, "scrape.bucket", runtimeOptions.scrapeBucket, "If false, bucket metrics won't be scraped.")
flag.BoolVar(&cmdlineOptions.scrapeXDCR, "scrape.xdcr", runtimeOptions.scrapeXDCR, "If false, XDCR metrics won't be scraped.")
flag.Parse()
configFileProvided := FlagPresent("config.file")
configLocations := [4]string{cmdlineOptions.configFile, "config.json", "config.yml", "config.yaml"}
for idx, configLocation := range configLocations {
config, err := cl.Load(configLocation)
if err != nil && configFileProvided && idx == 0 {
log.Fatalf("Could not use provided configuration file (%s). Error: %s", configLocation, err)
}
if err != nil {
continue
}
if config.GetString("web.listenAddress") != "" {
runtimeOptions.serverListenAddress = config.GetString("web.listenAddress")
}
if config.GetString("web.telemetryPath") != "" {
runtimeOptions.serverMetricsPath = config.GetString("web.telemetryPath")
}
if config.GetDuration("web.timeout") != 0*time.Second {
runtimeOptions.serverTimeout = config.GetDuration("web.timeout")
}
if config.GetString("db.user") != "" {
runtimeOptions.dbUsername = config.GetString("db.user")
}
if config.GetString("db.password") != "" {
runtimeOptions.dbPassword = config.GetString("db.password")
}
if config.GetString("db.uri") != "" {
runtimeOptions.dbURI = config.GetString("db.uri")
}
if config.GetDuration("db.timeout") != 0*time.Second {
runtimeOptions.dbTimeout = config.GetDuration("db.timeout")
}
if config.GetBool("tls.enabled") != runtimeOptions.tlsEnabled {
runtimeOptions.tlsEnabled = config.GetBool("tls.enabled")
}
if config.GetBool("tls.skip-insecure") != runtimeOptions.tlsSkipInsecure {
runtimeOptions.tlsSkipInsecure = config.GetBool("tls.skip-insecure")
}
if config.GetString("tls.ca-cert") != "" {
runtimeOptions.tlsCACert = config.GetString("tls.ca-cert")
}
if config.GetString("tls.client-cert") != "" {
runtimeOptions.tlsClientCert = config.GetString("tls.client-cert")
}
if config.GetString("tls.client-key") != "" {
runtimeOptions.tlsClientKey = config.GetString("tls.client-key")
}
if config.GetString("log.level") != "" {
runtimeOptions.logLevel = config.GetString("log.level")
}
if config.GetString("log.format") != "" {
runtimeOptions.logFormat = config.GetString("log.format")
}
if config.GetBool("scrape.cluster") != runtimeOptions.scrapeCluster {
runtimeOptions.scrapeCluster = config.GetBool("scrape.cluster")
}
if config.GetBool("scrape.node") != runtimeOptions.scrapeNode {
runtimeOptions.scrapeNode = config.GetBool("scrape.node")
}
if config.GetBool("scrape.bucket") != runtimeOptions.scrapeBucket {
runtimeOptions.scrapeBucket = config.GetBool("scrape.bucket")
}
if config.GetBool("scrape.xdcr") != runtimeOptions.scrapeXDCR {
runtimeOptions.scrapeXDCR = config.GetBool("scrape.xdcr")
}
// Stop on first encounter
runtimeOptions.configFile = configLocation
break
}
// Get environment variables values.
if val, ok := os.LookupEnv("CB_EXPORTER_LISTEN_ADDR"); ok {
runtimeOptions.serverListenAddress = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_TELEMETRY_PATH"); ok {
runtimeOptions.serverMetricsPath = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_SERVER_TIMEOUT"); ok {
runtimeOptions.serverTimeout, _ = time.ParseDuration(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_DB_USER"); ok {
runtimeOptions.dbUsername = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_DB_PASSWORD"); ok {
runtimeOptions.dbPassword = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_DB_URI"); ok {
runtimeOptions.dbURI = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_DB_TIMEOUT"); ok {
runtimeOptions.dbTimeout, _ = time.ParseDuration(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_TLS_ENABLED"); ok {
runtimeOptions.tlsEnabled, _ = strconv.ParseBool(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_TLS_SKIP_INSECURE"); ok {
runtimeOptions.tlsSkipInsecure, _ = strconv.ParseBool(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_TLS_CA_CERT"); ok {
runtimeOptions.tlsCACert = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_TLS_CLIENT_CERT"); ok {
runtimeOptions.tlsClientCert = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_TLS_CLIENT_KEY"); ok {
runtimeOptions.tlsClientKey = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_LOG_LEVEL"); ok {
runtimeOptions.logLevel = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_LOG_FORMAT"); ok {
runtimeOptions.logFormat = val
}
if val, ok := os.LookupEnv("CB_EXPORTER_SCRAPE_CLUSTER"); ok {
runtimeOptions.scrapeCluster, _ = strconv.ParseBool(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_SCRAPE_NODE"); ok {
runtimeOptions.scrapeNode, _ = strconv.ParseBool(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_SCRAPE_BUCKET"); ok {
runtimeOptions.scrapeBucket, _ = strconv.ParseBool(val)
}
if val, ok := os.LookupEnv("CB_EXPORTER_SCRAPE_XDCR"); ok {
runtimeOptions.scrapeXDCR, _ = strconv.ParseBool(val)
}
// Command-line values
if FlagPresent("web.listen-address") {
runtimeOptions.serverListenAddress = cmdlineOptions.serverListenAddress
}
if FlagPresent("web.telemetry-path") {
runtimeOptions.serverMetricsPath = cmdlineOptions.serverMetricsPath
}
if FlagPresent("web.timeout") {
runtimeOptions.serverTimeout = cmdlineOptions.serverTimeout
}
if FlagPresent("db.username") {
runtimeOptions.dbUsername = cmdlineOptions.dbUsername
}
if FlagPresent("db.password") {
runtimeOptions.dbPassword = cmdlineOptions.dbPassword
}
if FlagPresent("db.uri") {
runtimeOptions.dbURI = cmdlineOptions.dbURI
}
if FlagPresent("db.timeout") {
runtimeOptions.dbTimeout = cmdlineOptions.dbTimeout
}
if FlagPresent("tls.enabled") {
runtimeOptions.tlsEnabled = cmdlineOptions.tlsEnabled
}
if FlagPresent("tls.skip-insecure") {
runtimeOptions.tlsSkipInsecure = cmdlineOptions.tlsSkipInsecure
}
if FlagPresent("tls.ca-cert") {
runtimeOptions.tlsCACert = cmdlineOptions.tlsCACert
}
if FlagPresent("tls.client-cert") {
runtimeOptions.tlsClientCert = cmdlineOptions.tlsClientCert
}
if FlagPresent("tls.client-key") {
runtimeOptions.tlsClientKey = cmdlineOptions.tlsClientKey
}
if FlagPresent("log.level") {
runtimeOptions.logLevel = cmdlineOptions.logLevel
}
if FlagPresent("log.format") {
runtimeOptions.logFormat = cmdlineOptions.logFormat
}
if FlagPresent("scrape.cluster") {
runtimeOptions.scrapeCluster = cmdlineOptions.scrapeCluster
}
if FlagPresent("scrape.node") {
runtimeOptions.scrapeNode = cmdlineOptions.scrapeNode
}
if FlagPresent("scrape.bucket") {
runtimeOptions.scrapeBucket = cmdlineOptions.scrapeBucket
}
if FlagPresent("scrape.xdcr") {
runtimeOptions.scrapeXDCR = cmdlineOptions.scrapeXDCR
}
}
func initLogger() {
switch runtimeOptions.logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
default:
log.SetLevel(log.InfoLevel)
}
switch runtimeOptions.logFormat {
case "json":
log.SetFormatter(&log.JSONFormatter{
PrettyPrint: true,
})
default:
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}
log.SetOutput(os.Stdout)
}
func displayInfo() {
log.Info("Couchbase Exporter Version: ", version)
log.Info("config.file=", runtimeOptions.configFile)
log.Info("web.listen-address=", runtimeOptions.serverListenAddress)
log.Info("web.telemetry-path=", runtimeOptions.serverMetricsPath)
log.Info("web.timeout=", runtimeOptions.serverTimeout)
log.Info("db.uri=", runtimeOptions.dbURI)
log.Info("db.timeout=", runtimeOptions.dbTimeout)
log.Info("tls.skip-insecure=", runtimeOptions.tlsSkipInsecure)
log.Info("tls.ca-cert=", runtimeOptions.tlsCACert)
log.Info("tls.enabled=", runtimeOptions.tlsEnabled)
log.Info("tls.client-cert=", runtimeOptions.tlsClientCert)
log.Info("tls.client-key=", runtimeOptions.tlsClientKey)
log.Info("log.level=", runtimeOptions.logLevel)
log.Info("log.format=", runtimeOptions.logFormat)
log.Info("scrape.cluster=", runtimeOptions.scrapeCluster)
log.Info("scrape.node=", runtimeOptions.scrapeNode)
log.Info("scrape.bucket=", runtimeOptions.scrapeBucket)
log.Info("scrape.xdcr=", runtimeOptions.scrapeXDCR)
}