/
gauge_stats.go
85 lines (67 loc) · 2.54 KB
/
gauge_stats.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
package config
import (
"os"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
var GaugeStatHandler GaugeStats
/**
* Defines the structure which holds various Gauge stats for each contexts from a toml file
*/
type GaugeStats struct {
// used to read from toml config file, these array will be emptied once read and stats are mapped to a file
Namespace []string `toml:"namespace_gauge_stats"`
Node []string `toml:"node_gauge_stats"`
Sets []string `toml:"sets_gauge_stats"`
Sindex []string `toml:"sindex_gauge_stats"`
Xdr []string `toml:"xdr_gauge_stats"`
// System Info Gauges
SysInfo []string `toml:"system_info_gauge_stats"`
// why below maps?
// all gauge stats are added and mapped as true after reading from toml file
NamespaceStats map[string]bool
NodeStats map[string]bool
SetsStats map[string]bool
SindexStats map[string]bool
XdrStats map[string]bool
// System Info Gauges
SysInfoStats map[string]bool
}
// Initialize exporter configuration
func InitGaugeStats(pGaugeStatsFile string) {
log.Infof("Loading Gauge Stats file %s", pGaugeStatsFile)
blob, err := os.ReadFile(pGaugeStatsFile)
if err != nil {
log.Fatalln(err)
}
md, err := toml.Decode(string(blob), &GaugeStatHandler)
if err != nil {
log.Fatalln(err)
}
// create maps from read stats, this is done as we check each stat if it is a gauge or not
GaugeStatHandler.NamespaceStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.Namespace)
GaugeStatHandler.NodeStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.Node)
GaugeStatHandler.SindexStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.Sindex)
GaugeStatHandler.SetsStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.Sets)
GaugeStatHandler.XdrStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.Xdr)
// Nullify/empty the Arrays to avoid duplicate stats-copy
GaugeStatHandler.Namespace = nil
GaugeStatHandler.Node = nil
GaugeStatHandler.Sindex = nil
GaugeStatHandler.Sets = nil
GaugeStatHandler.Xdr = nil
// System Info
GaugeStatHandler.SysInfoStats = GaugeStatHandler.createMapFromArray(GaugeStatHandler.SysInfo)
GaugeStatHandler.SysInfo = nil
log.Debugln("# of Gauge Keys defined at Gauge Stat level are: ", len(md.Keys()))
}
/**
* Utility, common logic used to loop through contextual-array like Nodes, Sets etc.,
*/
func (gm *GaugeStats) createMapFromArray(pArrStats []string) map[string]bool {
statsMap := make(map[string]bool)
for _, stat := range pArrStats {
statsMap[stat] = true
}
return statsMap
}