-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
378 lines (291 loc) · 11.9 KB
/
config.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
package config
import (
"os"
"strings"
"github.com/BurntSushi/toml"
aslog "github.com/aerospike/aerospike-client-go/v6/logger"
log "github.com/sirupsen/logrus"
)
var Cfg Config
// Config represents the aerospike-prometheus-exporter configuration
type Config struct {
Agent struct {
OtelEnabled bool `toml:"enable_open_telemetry"`
PrometheusEnabled bool `toml:"enable_prometheus"`
MetricLabels map[string]string `toml:"labels"`
Timeout uint8 `toml:"timeout"`
RefreshSystemStats bool `toml:"refresh_system_stats"`
CloudProvider string `toml:"cloud_provider"`
LogFile string `toml:"log_file"`
LogLevel string `toml:"log_level"`
UseMockDatasource bool `toml:"use_mock_datasource"`
Bind string `toml:"bind"`
CertFile string `toml:"cert_file"`
KeyFile string `toml:"key_file"`
RootCA string `toml:"root_ca"`
KeyFilePassphrase string `toml:"key_file_passphrase"`
BasicAuthUsername string `toml:"basic_auth_username"`
BasicAuthPassword string `toml:"basic_auth_password"`
Otel struct {
OtelServiceName string `toml:"service_name"`
OtelEndpoint string `toml:"endpoint"`
OtelTlsEnabled bool `toml:"endpoint_tls_enabled"`
OtelHeaders map[string]string `toml:"headers"`
OtelPushInterval uint8 `toml:"push_interval"`
OtelServerStatFetchInterval uint8 `toml:"server_stat_fetch_interval"`
} `toml:"OpenTelemetry"`
IsKubernetes bool
KubernetesPodName string
} `toml:"Agent"`
Aerospike struct {
Host string `toml:"db_host"`
Port uint16 `toml:"db_port"`
CertFile string `toml:"cert_file"`
KeyFile string `toml:"key_file"`
KeyFilePassphrase string `toml:"key_file_passphrase"`
NodeTLSName string `toml:"node_tls_name"`
RootCA string `toml:"root_ca"`
AuthMode string `toml:"auth_mode"`
User string `toml:"user"`
Password string `toml:"password"`
Timeout uint8 `toml:"timeout"`
LatencyBucketsCount uint8 `toml:"latency_buckets_count"`
// Order of context ( from observer.go) - namespace, set, latencies, node-stats, xdr, user, jobs, sindex
// Namespace metrics allow/block
NamespaceMetricsAllowlist []string `toml:"namespace_metrics_allowlist"`
NamespaceMetricsBlocklist []string `toml:"namespace_metrics_blocklist"`
NamespaceMetricsAllowlistEnabled bool
// Set metrics allow/block
SetMetricsAllowlist []string `toml:"set_metrics_allowlist"`
SetMetricsBlocklist []string `toml:"set_metrics_blocklist"`
SetMetricsAllowlistEnabled bool
// Latencies metrics allow/block
LatenciesMetricsAllowlist []string `toml:"latencies_metrics_allowlist"`
LatenciesMetricsBlocklist []string `toml:"latencies_metrics_blocklist"`
LatenciesMetricsAllowlistEnabled bool
// knob to disable latencies metrics collection (for internal use only, will be deprecated)
DisableLatenciesMetrics bool `toml:"disable_latencies_metrics"`
// Node metrics allow/block
NodeMetricsAllowlist []string `toml:"node_metrics_allowlist"`
NodeMetricsBlocklist []string `toml:"node_metrics_blocklist"`
NodeMetricsAllowlistEnabled bool
// Xdr metrics allow/block
XdrMetricsAllowlist []string `toml:"xdr_metrics_allowlist"`
XdrMetricsBlocklist []string `toml:"xdr_metrics_blocklist"`
XdrMetricsAllowlistEnabled bool
// User metrics allow/block
UserMetricsUsersAllowlist []string `toml:"user_metrics_users_allowlist"`
UserMetricsUsersBlocklist []string `toml:"user_metrics_users_blocklist"`
UserMetricsUsersAllowlistEnabled bool
// Job metrics allow/block
JobMetricsAllowlist []string `toml:"job_metrics_allowlist"`
JobMetricsBlocklist []string `toml:"job_metrics_blocklist"`
JobMetricsAllowlistEnabled bool
// knob to disable job metrics collection (for internal use only, will be deprecated)
DisableJobMetrics bool `toml:"disable_job_metrics"`
// Sindex metrics allow/block
SindexMetricsAllowlist []string `toml:"sindex_metrics_allowlist"`
SindexMetricsBlocklist []string `toml:"sindex_metrics_blocklist"`
SindexMetricsAllowlistEnabled bool
// knob to disable sindex metrics collection (for internal use only, will be deprecated)
DisableSindexMetrics bool `toml:"disable_sindex_metrics"`
// Tolerate older whitelist and blacklist configurations for a while
NamespaceMetricsWhitelist []string `toml:"namespace_metrics_whitelist"`
SetMetricsWhitelist []string `toml:"set_metrics_whitelist"`
NodeMetricsWhitelist []string `toml:"node_metrics_whitelist"`
XdrMetricsWhitelist []string `toml:"xdr_metrics_whitelist"`
NamespaceMetricsBlacklist []string `toml:"namespace_metrics_blacklist"`
SetMetricsBlacklist []string `toml:"set_metrics_blacklist"`
NodeMetricsBlacklist []string `toml:"node_metrics_blacklist"`
XdrMetricsBlacklist []string `toml:"xdr_metrics_blacklist"`
} `toml:"Aerospike"`
LogFile *os.File
}
// Validate and update exporter configuration
func (c *Config) ValidateAndUpdate(md toml.MetaData) {
if c.Agent.Bind == "" {
c.Agent.Bind = ":9145"
}
if c.Agent.Timeout == 0 {
c.Agent.Timeout = 5
}
if c.Aerospike.AuthMode == "" {
c.Aerospike.AuthMode = "internal"
}
if c.Aerospike.Timeout == 0 {
c.Aerospike.Timeout = 5
}
if md.IsDefined("Agent", "use_mock_datasource") && c.Agent.UseMockDatasource {
c.Agent.UseMockDatasource = true
} else {
c.Agent.UseMockDatasource = false
}
if len(c.Agent.Otel.OtelServiceName) == 0 {
c.Agent.Otel.OtelServiceName = "aerospike-server-metrics-service"
}
if c.Agent.Otel.OtelPushInterval == 0 {
c.Agent.Otel.OtelPushInterval = 60
}
if c.Agent.Otel.OtelServerStatFetchInterval == 0 {
c.Agent.Otel.OtelPushInterval = 15
}
if !md.IsDefined("Agent", "enable_prometheus") {
log.Infof("Defaulting to Prometheus Exporting mode")
c.Agent.PrometheusEnabled = true
}
}
func (c *Config) FetchCloudInfo(md toml.MetaData) {
if !md.IsDefined("Agent", "cloud_provider") {
return
}
if Cfg.Agent.CloudProvider != "" && len(strings.Trim(Cfg.Agent.CloudProvider, " ")) > 0 {
cloudLabels := CollectCloudDetails()
log.Debug("Adding Cloud Info to Metric Labels ", cloudLabels)
for k, v := range cloudLabels {
if v == "" || len(v) == 0 {
v = "null"
}
Cfg.Agent.MetricLabels[k] = v
}
}
}
func (c *Config) FetchKubernetesInfo(md toml.MetaData) {
// use kubectl to fetch required Kubernetes context and find the required Kubenetes environment variables
envKubeServiceHost := os.Getenv("KUBERNETES_SERVICE_HOST")
Cfg.Agent.IsKubernetes = false
if envKubeServiceHost != "" && len(strings.TrimSpace(envKubeServiceHost)) > 0 {
Cfg.Agent.IsKubernetes = true
log.Info("Exporter is running in Kubernetes")
// get host-name
var err error
Cfg.Agent.KubernetesPodName, err = os.Hostname()
if err != nil {
log.Errorln(err)
return
}
}
}
// Initialize exporter configuration
func InitConfig(configFile string) {
// to print everything out regarding reading the config in app init
log.SetLevel(log.DebugLevel)
log.Infof("Loading configuration file %s", configFile)
blob, err := os.ReadFile(configFile)
if err != nil {
log.Fatalln(err)
}
md, err := toml.Decode(string(blob), &Cfg)
if err != nil {
log.Fatalln(err)
}
initAllowlistAndBlocklistConfigs(md)
Cfg.LogFile = setLogFile(Cfg.Agent.LogFile)
aslog.Logger.SetLogger(log.StandardLogger())
setLogLevel(Cfg.Agent.LogLevel)
Cfg.ValidateAndUpdate(md)
Cfg.FetchCloudInfo(md)
Cfg.FetchKubernetesInfo(md)
}
// Set log file path
func setLogFile(filepath string) *os.File {
if len(strings.TrimSpace(filepath)) > 0 {
out, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
log.SetOutput(out)
return out
}
return nil
}
// Set logging level
func setLogLevel(level string) {
level = strings.ToLower(level)
switch level {
case "info":
log.SetLevel(log.InfoLevel)
aslog.Logger.SetLevel(aslog.INFO)
case "warning", "warn":
log.SetLevel(log.WarnLevel)
aslog.Logger.SetLevel(aslog.WARNING)
case "error", "err":
log.SetLevel(log.ErrorLevel)
aslog.Logger.SetLevel(aslog.ERR)
case "debug":
log.SetLevel(log.DebugLevel)
aslog.Logger.SetLevel(aslog.DEBUG)
case "trace":
log.SetLevel(log.TraceLevel)
aslog.Logger.SetLevel(aslog.DEBUG)
default:
log.SetLevel(log.InfoLevel)
aslog.Logger.SetLevel(aslog.INFO)
}
}
// Initialize Allowlist and Blocklist configurations
func initAllowlistAndBlocklistConfigs(md toml.MetaData) {
// Initialize AllowlistEnabled config
Cfg.Aerospike.NamespaceMetricsAllowlistEnabled = md.IsDefined("Aerospike", "namespace_metrics_allowlist")
Cfg.Aerospike.SetMetricsAllowlistEnabled = md.IsDefined("Aerospike", "set_metrics_allowlist")
Cfg.Aerospike.NodeMetricsAllowlistEnabled = md.IsDefined("Aerospike", "node_metrics_allowlist")
Cfg.Aerospike.XdrMetricsAllowlistEnabled = md.IsDefined("Aerospike", "xdr_metrics_allowlist")
Cfg.Aerospike.UserMetricsUsersAllowlistEnabled = md.IsDefined("Aerospike", "user_metrics_users_allowlist")
Cfg.Aerospike.JobMetricsAllowlistEnabled = md.IsDefined("Aerospike", "job_metrics_allowlist")
Cfg.Aerospike.SindexMetricsAllowlistEnabled = md.IsDefined("Aerospike", "sindex_metrics_allowlist")
Cfg.Aerospike.LatenciesMetricsAllowlistEnabled = md.IsDefined("Aerospike", "latencies_metrics_allowlist")
// Tolerate older whitelist and blacklist configurations for a while.
// If whitelist and blacklist configs are defined copy them into allowlist and blocklist.
// Error out if both configurations are used at the same time.
if md.IsDefined("Aerospike", "namespace_metrics_whitelist") {
if Cfg.Aerospike.NamespaceMetricsAllowlistEnabled {
log.Fatalf("namespace_metrics_whitelist and namespace_metrics_allowlist are mutually exclusive!")
}
Cfg.Aerospike.NamespaceMetricsAllowlistEnabled = true
Cfg.Aerospike.NamespaceMetricsAllowlist = Cfg.Aerospike.NamespaceMetricsWhitelist
}
if md.IsDefined("Aerospike", "set_metrics_whitelist") {
if Cfg.Aerospike.SetMetricsAllowlistEnabled {
log.Fatalf("set_metrics_whitelist and set_metrics_allowlist are mutually exclusive!")
}
Cfg.Aerospike.SetMetricsAllowlistEnabled = true
Cfg.Aerospike.SetMetricsAllowlist = Cfg.Aerospike.SetMetricsWhitelist
}
if md.IsDefined("Aerospike", "node_metrics_whitelist") {
if Cfg.Aerospike.NodeMetricsAllowlistEnabled {
log.Fatalf("node_metrics_whitelist and node_metrics_allowlist are mutually exclusive!")
}
Cfg.Aerospike.NodeMetricsAllowlistEnabled = true
Cfg.Aerospike.NodeMetricsAllowlist = Cfg.Aerospike.NodeMetricsWhitelist
}
if md.IsDefined("Aerospike", "xdr_metrics_whitelist") {
if Cfg.Aerospike.XdrMetricsAllowlistEnabled {
log.Fatalf("xdr_metrics_whitelist and xdr_metrics_allowlist are mutually exclusive!")
}
Cfg.Aerospike.XdrMetricsAllowlistEnabled = true
Cfg.Aerospike.XdrMetricsAllowlist = Cfg.Aerospike.XdrMetricsWhitelist
}
if md.IsDefined("Aerospike", "namespace_metrics_blacklist") {
if len(Cfg.Aerospike.NamespaceMetricsBlocklist) > 0 {
log.Fatalf("namespace_metrics_blacklist and namespace_metrics_blocklist are mutually exclusive!")
}
Cfg.Aerospike.NamespaceMetricsBlocklist = Cfg.Aerospike.NamespaceMetricsBlacklist
}
if md.IsDefined("Aerospike", "set_metrics_blacklist") {
if len(Cfg.Aerospike.SetMetricsBlocklist) > 0 {
log.Fatalf("set_metrics_blacklist and set_metrics_blocklist are mutually exclusive!")
}
Cfg.Aerospike.SetMetricsBlocklist = Cfg.Aerospike.SetMetricsBlacklist
}
if md.IsDefined("Aerospike", "node_metrics_blacklist") {
if len(Cfg.Aerospike.NodeMetricsBlocklist) > 0 {
log.Fatalf("node_metrics_blacklist and node_metrics_blocklist are mutually exclusive!")
}
Cfg.Aerospike.NodeMetricsBlocklist = Cfg.Aerospike.NodeMetricsBlacklist
}
if md.IsDefined("Aerospike", "xdr_metrics_blacklist") {
if len(Cfg.Aerospike.XdrMetricsBlocklist) > 0 {
log.Fatalf("xdr_metrics_blacklist and xdr_metrics_blocklist are mutually exclusive!")
}
Cfg.Aerospike.XdrMetricsBlocklist = Cfg.Aerospike.XdrMetricsBlacklist
}
}