-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
936 lines (832 loc) · 29.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
package main
import (
"encoding/json"
"fmt"
"log/slog"
"reflect"
"regexp"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v3"
)
// Load attempts to parse the given config file and return a Config object.
func LoadConfig(configFile string, logger *slog.Logger, collectorName string) (*Config, error) {
logger.Info(fmt.Sprintf("Loading configuration from %s", configFile))
buf, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
c := Config{
configFile: configFile,
logger: logger,
collectorName: collectorName,
}
err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, err
}
return &c, nil
}
//
// Top-level config
//
// Config is a collection of targets and collectors.
type Config struct {
Globals *GlobalConfig `yaml:"global"`
CollectorFiles []string `yaml:"collector_files,omitempty"`
Targets []*TargetConfig `yaml:"targets,omitempty"`
Collectors []*CollectorConfig `yaml:"collectors,omitempty"`
HttpAPIConfig map[string]*YAMLScript `yaml:"httpapi_config"`
AuthConfigs map[string]*AuthConfig `yaml:"auth_configs,omitempty"`
configFile string
logger *slog.Logger
// collectorName is a restriction: collectors set for a target are replaced by this only one.
collectorName string
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for Config.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Config
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if len(c.Targets) == 0 {
return fmt.Errorf("at least one target in `targets` must be defined")
}
// Load any externally defined collectors.
if err := c.loadCollectorFiles(); err != nil {
return err
}
if len(c.Collectors) == 0 {
return fmt.Errorf("at least one collector in `collectors` must be defined")
}
// Populate collector references for the target/jobs.
colls := make(map[string]*CollectorConfig)
for id, coll := range c.Collectors {
// Set the min interval to the global default if not explicitly set.
if coll.MinInterval < 0 {
coll.MinInterval = c.Globals.MinInterval
}
if _, found := colls[coll.Name]; found {
return fmt.Errorf("duplicate collector name: %s", coll.Name)
}
colls[coll.Name] = coll
// set metric prefix
var prefix string
for _, cs := range coll.CollectScripts {
for _, a := range cs.Actions {
reslist := a.GetMetrics()
for _, res := range reslist {
for _, metric := range res.mc {
// add metric prefix to all metrics name
if res.maprefix != "" {
prefix = res.maprefix
} else if coll.MetricPrefix != "" {
prefix = coll.MetricPrefix
} else if c.Globals.MetricPrefix != "" {
prefix = c.Globals.MetricPrefix
}
if !strings.HasPrefix(metric.Name, prefix) {
metric.Name = fmt.Sprintf("%s_%s", prefix, metric.Name)
}
}
}
}
}
coll.id = fmt.Sprintf("%02d", id)
}
// read the target config with a TargetsFiles specfied
for _, t := range c.Targets {
if len(t.TargetsFiles) > 0 {
err := c.loadTargetsFiles(t.TargetsFiles)
if err != nil {
return err
}
} else {
c.logger.Info(fmt.Sprintf("static target '%s' found", t.Name))
}
}
targets := c.Targets
c.Targets = nil
// remove pseudo targets with a TargetsFiles
for _, t := range targets {
if len(t.TargetsFiles) == 0 {
c.Targets = append(c.Targets, t)
}
}
// check if a target nammed "default" exists
// if not create one with default parameters from TargetConfig
found := false
for _, t := range c.Targets {
if strings.ToLower(t.Name) == "default" {
t.Name = "default"
found = true
break
}
}
if !found {
default_target := `
name: default
host: set_later
verifySSL: true
collectors:
- ~.*_metrics
`
t := &TargetConfig{}
if err := yaml.Unmarshal([]byte(default_target), t); err != nil {
return err
}
c.Targets = append(c.Targets, t)
c.logger.Info(fmt.Sprintf("target '%s' added", t.Name))
}
for _, t := range c.Targets {
// substitute the collector names list set in config by the value forced in command line argument
if c.collectorName != "" {
t.CollectorRefs = nil
t.CollectorRefs = append(t.CollectorRefs, c.collectorName)
}
cs, err := resolveCollectorRefs(t.CollectorRefs, colls, fmt.Sprintf("target %q", t.Name))
if err != nil {
return err
}
t.collectors = cs
// substitute AuthConfig name with auth config parameters
if t.AuthName != "" {
auth := c.FindAuthConfig(t.AuthName)
if auth != nil {
t.AuthConfig = *auth
} else {
return fmt.Errorf("auth_name '%s' not found for target '%s", t.AuthName, t.Name)
}
}
}
// Check for empty/duplicate target names
tnames := make(map[string]interface{})
for _, t := range c.Targets {
if len(t.TargetsFiles) > 0 {
continue
}
if t.Name == "" {
return fmt.Errorf("empty target name in static config %+v", t)
}
if _, ok := tnames[t.Name]; ok {
return fmt.Errorf("duplicate target name %q in target %+v", t.Name, t)
}
tnames[t.Name] = nil
if t.ScrapeTimeout == 0 {
t.ScrapeTimeout = c.Globals.ScrapeTimeout
}
if t.QueryRetry == -1 {
t.QueryRetry = c.Globals.QueryRetry
}
}
// check HttpAPIConfig script:
for name, sc := range c.HttpAPIConfig {
if sc != nil {
sc.name = name
// have to set the action to play for play_script_action
for _, a := range sc.Actions {
if a.Type() == play_script_action || a.Type() == actions_action {
if err := a.SetPlayAction(c.HttpAPIConfig); err != nil {
return err
}
}
}
}
}
return checkOverflow(c.XXX, "config")
}
func (c *Config) FindAuthConfig(auth_name string) *AuthConfig {
var auth *AuthConfig
auth, found := c.AuthConfigs[auth_name]
if !found {
return nil
}
return auth
}
func GetScriptsDef(map_src map[string]*YAMLScript) map[string]ActionsList {
var val ActionsList
scdef := make(map[string]ActionsList, len(map_src)+1)
for name, scr := range map_src {
val = (ActionsList)(nil)
if scr != nil {
val = (ActionsList)(scr.Actions)
}
scdef[name] = val
}
return scdef
}
type dumpConfig struct {
Globals *GlobalConfig `yaml:"global" json:"global"`
CollectorFiles []string `yaml:"collector_files,omitempty" json:"collector_files,omitempty"`
Collectors []*dumpCollectorConfig `yaml:"collectors,omitempty" json:"collectors,omitempty"`
AuthConfigs map[string]*AuthConfig `yaml:"auth_configs,omitempty" json:"auth_configs,omitempty"`
HttpAPIConfig map[string]ActionsList `yaml:"httpapi_config" json:"httpapi_config"`
}
// YAML marshals the config into YAML format.
func (c *Config) YAML() ([]byte, error) {
dc := &dumpConfig{
Globals: c.Globals,
AuthConfigs: c.AuthConfigs,
CollectorFiles: c.CollectorFiles,
Collectors: GetCollectorsDef(c.Collectors),
HttpAPIConfig: GetScriptsDef(c.HttpAPIConfig),
}
return yaml.Marshal(dc)
}
// JSON marshals the config into JSON format.
func (c *Config) JSON() ([]byte, error) {
type fullConf struct {
Config *dumpConfig `json:"config"`
}
fc := &fullConf{
Config: &dumpConfig{
Globals: c.Globals,
AuthConfigs: c.AuthConfigs,
CollectorFiles: c.CollectorFiles,
Collectors: GetCollectorsDef(c.Collectors),
HttpAPIConfig: GetScriptsDef(c.HttpAPIConfig),
},
}
return json.Marshal(fc)
}
// loadCollectorFiles resolves all collector file globs to files and loads the collectors they define.
func (c *Config) loadCollectorFiles() error {
baseDir := filepath.Dir(c.configFile)
for _, cfglob := range c.CollectorFiles {
// Resolve relative paths by joining them to the configuration file's directory.
if len(cfglob) > 0 && !filepath.IsAbs(cfglob) {
cfglob = filepath.Join(baseDir, cfglob)
}
// Resolve the glob to actual filenames.
cfs, err := filepath.Glob(cfglob)
c.logger.Debug(fmt.Sprintf("Checking collectors from %s", cfglob))
if err != nil {
// The only error can be a bad pattern.
return fmt.Errorf("error parsing collector files for %s: %s", cfglob, err)
}
// And load the CollectorConfig defined in each file.
for _, cf := range cfs {
c.logger.Debug(fmt.Sprintf("Loading collectors from %s", cf))
buf, err := os.ReadFile(cf)
if err != nil {
return fmt.Errorf("reading collectors file %s: %s", cf, err)
}
cc := CollectorConfig{
symtab: map[string]any{},
}
err = yaml.Unmarshal(buf, &cc)
if err != nil {
return fmt.Errorf("reading %s: %s", cf, err)
}
c.Collectors = append(c.Collectors, &cc)
c.logger.Info(fmt.Sprintf("Loaded collector %s from %s", cc.Name, cf))
}
}
return nil
}
// loadTargetsFiles resolves all targets file globs to files and loads the targets they define.
func (c *Config) loadTargetsFiles(targetFilepath []string) error {
baseDir := filepath.Dir(c.configFile)
for _, tfglob := range targetFilepath {
// Resolve relative paths by joining them to the configuration file's directory.
if len(tfglob) > 0 && !filepath.IsAbs(tfglob) {
tfglob = filepath.Join(baseDir, tfglob)
}
// Resolve the glob to actual filenames.
tfs, err := filepath.Glob(tfglob)
c.logger.Debug(fmt.Sprintf("Checking targets from %s", tfglob))
if err != nil {
// The only error can be a bad pattern.
return fmt.Errorf("error resolving targets_files files for %s: %s", tfglob, err)
}
// And load the CollectorConfig defined in each file.
for _, tf := range tfs {
c.logger.Debug(fmt.Sprintf("Loading targets from %s", tf))
buf, err := os.ReadFile(tf)
if err != nil {
return fmt.Errorf("reading targets_files for %s: %s", tf, err)
}
target := TargetConfig{}
err = yaml.Unmarshal(buf, &target)
if err != nil {
return fmt.Errorf("parsing targets_files for %s: %s", tf, err)
}
target.setFromFile(tf)
c.Targets = append(c.Targets, &target)
c.logger.Info(fmt.Sprintf("Loaded target '%q' from %s", target.Name, tf))
}
}
return nil
}
// GlobalConfig contains globally applicable defaults.
type GlobalConfig struct {
MinInterval model.Duration `yaml:"min_interval" json:"min_interval"` // minimum interval between query executions, default is 0
ScrapeTimeout model.Duration `yaml:"scrape_timeout" json:"scrape_timeout"` // per-scrape timeout, global
TimeoutOffset model.Duration `yaml:"scrape_timeout_offset" json:"scrape_timeout_offset"` // offset to subtract from timeout in seconds
MetricPrefix string `yaml:"metric_prefix" json:"metric_prefix"` // a prefix to ad dto all metric name; may be redefined in collector files
QueryRetry int `yaml:"query_retry,omitempty" json:"query_retry,omitempty"` // target specific number of times to retry a query
InvalidHttpCode any `yaml:"invalid_auth_code,omitempty" json:"invalid_auth_code,omitempty"`
ExporterName string `yaml:"exporter_name,omitempty" json:"exporter_name,omitempty"`
invalid_auth_code []int
// query_retry int
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for GlobalConfig.
func (g *GlobalConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Default to running the queries on every scrape.
g.MinInterval = model.Duration(0)
// Default to 10 seconds, since Prometheus has a 10 second scrape timeout default.
g.ScrapeTimeout = model.Duration(10 * time.Second)
// Default to .5 seconds.
g.TimeoutOffset = model.Duration(500 * time.Millisecond)
g.ExporterName = exporter_name
// Default tp 3
g.QueryRetry = 3
// Default to httpapi
g.MetricPrefix = "httpapi"
type plain GlobalConfig
if err := unmarshal((*plain)(g)); err != nil {
return err
}
if g.TimeoutOffset <= 0 {
return fmt.Errorf("global.scrape_timeout_offset must be strictly positive, have %s", g.TimeoutOffset)
}
if g.ScrapeTimeout <= 0 {
return fmt.Errorf("global.connection_timeout must be strictly positive, have %s", g.ScrapeTimeout)
}
if g.InvalidHttpCode == nil {
g.invalid_auth_code = []int{401, 403}
} else {
g.invalid_auth_code = buildStatus(g.InvalidHttpCode)
}
return checkOverflow(g.XXX, "global")
}
//
// Targets
//
// TargetConfig defines a url and a set of collectors to be executed on it.
type TargetConfig struct {
Name string `yaml:"name" json:"name"` // target name to connect to from prometheus
Scheme string `yaml:"scheme" json:"scheme"`
Host string `yaml:"host" json:"host"`
Port string `yaml:"port,omitempty" json:"port,omitempty"`
BaseUrl string `yaml:"baseUrl,omitempty" json:"baseUrl,omitempty"`
AuthName string `yaml:"auth_name,omitempty" json:"auth_name,omitempty"`
AuthConfig AuthConfig `yaml:"auth_config,omitempty" json:"auth_config,omitempty"`
ProxyUrl string `yaml:"proxy,omitempty" json:"proxy,omitempty"`
VerifySSLString string `yaml:"verifySSL,omitempty" json:"verifySSL,omitempty"`
ScrapeTimeout model.Duration `yaml:"scrape_timeout" json:"scrape_timeout"` // per-scrape timeout, global
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` // labels to apply to all metrics collected from the targets
CollectorRefs []string `yaml:"collectors" json:"collectors"` // names of collectors to execute on the target
TargetsFiles []string `yaml:"targets_files,omitempty" json:"targets_files,omitempty"` // slice of path and pattern for files that contains targets
QueryRetry int `yaml:"query_retry,omitempty" json:"query_retry,omitempty"` // target specific number of times to retry a query
collectors []*CollectorConfig // resolved collector references
fromFile string // filepath if loaded from targets_files pattern
verifySSLUserSet bool
verifySSL ConvertibleBoolean
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// Collectors returns the collectors referenced by the target, resolved.
func (t *TargetConfig) Collectors() []*CollectorConfig {
return t.collectors
}
// set fromFile for target when read from targets_files directive
func (t *TargetConfig) setFromFile(file_path string) {
t.fromFile = file_path
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for TargetConfig.
func (t *TargetConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain TargetConfig
// set default value for target
t.QueryRetry = -1
// set default value for VerifySSL
t.verifySSL = true
// by default value is not set by user; will be overwritten if user set a value
t.verifySSLUserSet = false
if err := unmarshal((*plain)(t)); err != nil {
return err
}
// Check required fields
if len(t.TargetsFiles) == 0 {
if t.Name == "" {
return fmt.Errorf("empty target name in target %+v", t)
}
if t.Scheme == "" {
t.Scheme = "https"
}
if t.Port == "" {
t.Port = "443"
}
if t.BaseUrl != "" {
t.BaseUrl = strings.Trim(t.BaseUrl, "/")
}
if t.Host == "" {
return fmt.Errorf("missing data_source_name for target %+v", t)
}
if t.VerifySSLString != "" {
if err := t.verifySSL.UnmarshalJSON([]byte(t.VerifySSLString)); err != nil {
return err
}
t.verifySSLUserSet = true
}
checkCollectorRefs(t.CollectorRefs, t.Name)
if len(t.Labels) > 0 {
err := t.checkLabelCollisions()
if err != nil {
return err
}
}
} else {
for _, file := range t.TargetsFiles {
if file == "" {
return fmt.Errorf("missing targets_files pattern")
}
}
}
if t.AuthConfig.Mode == "" {
t.AuthConfig.Mode = "basic"
}
return checkOverflow(t.XXX, "target")
}
// checkLabelCollisions checks for label collisions between StaticConfig labels and Metric labels.
func (t *TargetConfig) checkLabelCollisions() error {
sclabels := make(map[string]interface{})
for _, l := range t.Labels {
sclabels[l] = nil
}
for _, c := range t.collectors {
// for _, m := range c.Metrics {
for _, cs := range c.CollectScripts {
for _, a := range cs.Actions {
fmt.Printf("action type: %s", reflect.TypeOf(a))
reslist := a.GetMetrics()
for _, res := range reslist {
for _, m := range res.mc {
if keymap, ok := m.KeyLabels.(map[string]string); ok {
for _, l := range keymap {
if _, ok := sclabels[l]; ok {
return fmt.Errorf(
"label collision in target %q: label %q is defined both by a static_config and by metric %q of collector %q",
t.Name, l, m.Name, c.Name)
}
}
}
}
}
}
}
}
return nil
}
// method to build a temporary TargetConfig from "default" with host_name & and auth_name
func (t *TargetConfig) Clone(host_path string, auth_name string) (*TargetConfig, error) {
new := &TargetConfig{
Name: host_path,
Scheme: t.Scheme,
Host: t.Host,
Port: t.Port,
BaseUrl: t.BaseUrl,
AuthConfig: t.AuthConfig,
ProxyUrl: t.ProxyUrl,
ScrapeTimeout: t.ScrapeTimeout,
Labels: t.Labels,
QueryRetry: t.QueryRetry,
CollectorRefs: t.CollectorRefs,
collectors: t.collectors,
verifySSLUserSet: t.verifySSLUserSet,
verifySSL: t.verifySSL,
}
url_elmt, err := url.Parse(host_path)
if err != nil {
return nil, err
}
if url_elmt.Scheme != "" && new.Scheme != url_elmt.Scheme {
if url_elmt.Scheme == "https" || url_elmt.Scheme == "http" {
new.Scheme = url_elmt.Scheme
} else if url_elmt.Host == "" {
// url.Parse for input "host.domain:port" builds .Scheme = "host.domain" .Opaque = "port"
new.Host = url_elmt.Scheme
if url_elmt.Opaque != "" {
new.Port = url_elmt.Opaque
}
}
}
if url_elmt.Host == "" && url_elmt.Path != "" {
new.Host = url_elmt.Path
} else {
if url_elmt.Host != "" {
elmts := strings.Split(url_elmt.Host, ":")
if new.Host != elmts[0] {
new.Host = elmts[0]
}
if len(elmts) > 1 {
new.Port = elmts[1]
}
}
}
if url_elmt.User.Username() != "" {
new.AuthConfig.Username = url_elmt.User.Username()
if tmp, set := url_elmt.User.Password(); set {
new.AuthConfig.Password = Secret(tmp)
}
new.AuthConfig.Mode = "basic"
}
return new, nil
}
//
// Collectors
//
// CollectorConfig defines a set of metrics and how they are collected.
type CollectorConfig struct {
Name string `yaml:"collector_name" json:"collector_name"` // name of this collector
MetricPrefix string `yaml:"metric_prefix,omitempty" json:"metric_prefix,omitempty"` // a prefix to ad dto all metric name; may be redefined in collector files
MinInterval model.Duration `yaml:"min_interval,omitempty" json:"min_interval,omitempty"` // minimum interval between query executions
Templates map[string]string `yaml:"templates,omitempty" json:"templates,omitempty"` // share custom templates/funcs for results templating
CollectScripts map[string]*YAMLScript `yaml:"scripts,omitempty" json:"scripts,omitempty"` // map of all independent scripts to collect metrics - each script can run in parallem
symtab map[string]any
customTemplate *exporterTemplate // to store the custom Templates used by this collector
// id to print in log and to follow request action
id string
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for CollectorConfig.
func (c *CollectorConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Default to undefined (a negative value) so it can be overridden by the global default when not explicitly set.
c.MinInterval = -1
c.MetricPrefix = ""
type plain CollectorConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
// build the default templates/funcs that my be used by all templates
if len(c.Templates) > 0 {
// c.customTemplate = template.New("default").Funcs(sprig.FuncMap())
c.customTemplate = (*exporterTemplate)(template.New("default").Funcs(mymap()))
if c.customTemplate == nil {
return fmt.Errorf("for collector %s template is invalid", c.Name)
}
for name, tmpl := range c.Templates {
def := "{{- define \"" + name + "\" }}" + strings.ReplaceAll(tmpl, "\n", "") + "{{ end -}}"
ptr := (*template.Template)(c.customTemplate)
if tmp_tpl, err := ptr.Parse(def); err != nil {
return fmt.Errorf("for collector %s template %s is invalid: %s", c.Name, def, err)
} else {
c.customTemplate = (*exporterTemplate)(tmp_tpl)
}
}
}
if c.CollectScripts != nil {
for collect_script_name, c_script := range c.CollectScripts {
if c_script.name == "" {
c_script.name = collect_script_name
}
if err := c_script.AddCustomTemplate(c.customTemplate); err != nil {
err = fmt.Errorf("script %s: error with custom template: %s", c_script.name, err)
return err
}
}
}
return checkOverflow(c.XXX, "collector")
}
type dumpCollectorConfig struct {
Name string `yaml:"collector_name" json:"collector_name"` // name of this collector
MetricPrefix string `yaml:"metric_prefix,omitempty" json:"metric_prefix,omitempty"` // a prefix to ad dto all metric name; may be redefined in collector files
MinInterval model.Duration `yaml:"min_interval,omitempty" json:"min_interval,omitempty"` // minimum interval between query executions
Templates map[string]string `yaml:"templates,omitempty" json:"templates,omitempty"` // share custom templates/funcs for results templating
CollectScripts map[string]ActionsList `yaml:"scripts,omitempty" json:"scripts,omitempty"` // map of all independent scripts to collect metrics - each script can run in parallem
}
func GetCollectorsDef(src_colls []*CollectorConfig) []*dumpCollectorConfig {
colls := make([]*dumpCollectorConfig, len(src_colls))
for idx, coll := range src_colls {
colls[idx] = &dumpCollectorConfig{
Name: coll.Name,
MetricPrefix: coll.MetricPrefix,
MinInterval: coll.MinInterval,
Templates: coll.Templates,
CollectScripts: GetScriptsDef(coll.CollectScripts),
}
}
return colls
}
// MetricConfig defines a Prometheus metric, the SQL query to populate it and the mapping of columns to metric
// keys/values.
type MetricConfig struct {
Name string `yaml:"metric_name" json:"metric_name"` // the Prometheus metric name
TypeString string `yaml:"type" json:"type"` // the Prometheus metric type
Help string `yaml:"help" json:"help"` // the Prometheus metric help text
KeyLabels any `yaml:"key_labels,omitempty" json:"key_labels,omitempty"` // expose these atributes as labels from JSON object: format name: value with name and value that should be template
StaticLabels map[string]string `yaml:"static_labels,omitempty" json:"static_labels,omitempty"` // fixed key/value pairs as static labels
ValueLabel string `yaml:"value_label,omitempty" json:"value_label,omitempty"` // with multiple value columns, map their names under this label
Values map[string]string `yaml:"values" json:"values"` // expose each of these columns as a value, keyed by column name
Scope string `yaml:"scope,omitempty" json:"scope,omitempty"` // var path where to collect data: shortcut for {{ .scope.path.var }}
valueType prometheus.ValueType // TypeString converted to prometheus.ValueType
key_labels_map map[string]string
key_labels *Field
}
// ValueType returns the metric type, converted to a prometheus.ValueType.
func (m *MetricConfig) ValueType() prometheus.ValueType {
return m.valueType
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for MetricConfig.
func (m *MetricConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain MetricConfig
if err := unmarshal((*plain)(m)); err != nil {
return err
}
// Check required fields
if m.Name == "" {
return fmt.Errorf("missing name for metric %+v", m)
}
if m.TypeString == "" {
return fmt.Errorf("missing type for metric %q", m.Name)
}
switch strings.ToLower(m.TypeString) {
case "counter":
m.valueType = prometheus.CounterValue
case "gauge":
m.valueType = prometheus.GaugeValue
default:
return fmt.Errorf("unsupported metric type: %s", m.TypeString)
}
// Check for duplicate key labels
if m.KeyLabels != nil {
switch ktype := m.KeyLabels.(type) {
case map[string]string:
for key := range ktype {
checkLabel(key, "metric", m.Name)
}
m.key_labels_map = ktype
case map[string]any:
m.key_labels_map = make(map[string]string, len(ktype))
for key, val_raw := range ktype {
checkLabel(key, "metric", m.Name)
if val, ok := val_raw.(string); ok {
m.key_labels_map[key] = val
}
}
case string:
if ktype != "" {
if val, err := NewField(ktype, nil); err == nil {
m.key_labels = val
} else {
return err
}
}
default:
return fmt.Errorf("key_labels should be a map[string][string] or Template(string) that will contain a map[string][string] for metric %q", m.Name)
}
}
if len(m.Values) == 0 {
return fmt.Errorf("no values defined for metric %q", m.Name)
}
if len(m.Values) > 1 {
// Multiple value columns but no value label to identify them
if m.ValueLabel == "" {
return fmt.Errorf("value_label must be defined for metric with multiple values %q", m.Name)
}
checkLabel(m.ValueLabel, "value_label for metric", m.Name)
}
return nil
}
// Secret special type for storing secrets.
type Secret string
// UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets.
func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Secret
return unmarshal((*plain)(s))
}
// MarshalYAML implements the yaml.Marshaler interface for Secrets.
func (s Secret) MarshalYAML() (interface{}, error) {
if s != "" {
return "<secret>", nil
}
return nil, nil
}
// ConvertibleBoolean special type to retrive 1 yes true to boolean true
type ConvertibleBoolean bool
func (bit *ConvertibleBoolean) UnmarshalJSON(data []byte) error {
asString := strings.ToLower(string(data))
if asString == "1" || asString == "true" || asString == "yes" || asString == "on" {
*bit = true
} else if asString == "0" || asString == "false" || asString == "no" || asString == "off" {
*bit = false
} else {
return fmt.Errorf("boolean unmarshal error: invalid input %s", asString)
}
return nil
}
type AuthConfig struct {
Mode string `yaml:"mode,omitempty" json:"mode,omitempty"` // basic, encrypted, bearer
Username string `yaml:"user,omitempty" json:"user,omitempty"`
Password Secret `yaml:"password,omitempty" json:"password,omitempty"`
Token Secret `yaml:"token,omitempty" json:"token,omitempty"`
authKey string
}
func check_env_var(value string) string {
if value != "" && strings.HasPrefix(value, "$env:") {
value = os.Getenv(value[5:])
}
return value
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for authConfig
func (auth *AuthConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain AuthConfig
if err := unmarshal((*plain)(auth)); err != nil {
return err
}
// Check required fields
if auth.Mode == "" {
auth.Mode = "basic"
} else {
auth.Mode = strings.ToLower(auth.Mode)
mode := make(map[string]int)
for _, val := range []string{"basic", "token", "script"} {
mode[val] = 1
}
if _, err := mode[auth.Mode]; !err {
return fmt.Errorf("invalid mode auth %s", auth.Mode)
}
}
if auth.Mode == "token" && auth.Token == "" {
return fmt.Errorf("token not set with auth mode 'token'")
}
// auth.Username == $env:VAR_NAME
auth.Username = check_env_var(auth.Username)
auth.Password = Secret(check_env_var(string(auth.Password)))
auth.Token = Secret(check_env_var(string(auth.Token)))
return nil
}
// *************************************************************************************************
func checkCollectorRefs(collectorRefs []string, ctx string) error {
// At least one collector, no duplicates
if len(collectorRefs) == 0 {
return fmt.Errorf("no collectors defined for %s", ctx)
}
for i, ci := range collectorRefs {
for _, cj := range collectorRefs[i+1:] {
if ci == cj {
return fmt.Errorf("duplicate collector reference %q in %s", ci, ctx)
}
}
}
return nil
}
func resolveCollectorRefs(
collectorRefs []string, collectors map[string]*CollectorConfig, ctx string) ([]*CollectorConfig, error) {
resolved := make([]*CollectorConfig, 0, len(collectorRefs))
for _, cref := range collectorRefs {
// check if cref(a collector name) is a pattern or not
if strings.HasPrefix(cref, "~") {
pat := regexp.MustCompile(cref[1:])
for c_name, c := range collectors {
if pat.MatchString(c_name) {
resolved = append(resolved, c)
}
}
} else if strings.HasPrefix(cref, "!~") {
pat := regexp.MustCompile(cref[2:])
for c_name, c := range collectors {
if !pat.MatchString(c_name) {
resolved = append(resolved, c)
}
}
} else {
c, found := collectors[cref]
if !found {
return nil, fmt.Errorf("unknown collector %q referenced in %s", cref, ctx)
}
resolved = append(resolved, c)
}
}
return resolved, nil
}
func checkLabel(label string, ctx ...string) error {
if label == "" {
return fmt.Errorf("empty label defined in %s", strings.Join(ctx, " "))
}
if label == "job" || label == "instance" {
return fmt.Errorf("reserved label %q redefined in %s", label, strings.Join(ctx, " "))
}
return nil
}
func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("unknown fields '%s' in '%s'", strings.Join(keys, ", "), ctx)
}
return nil
}