forked from aerospike/aerospike-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge_stats.go
95 lines (78 loc) · 2.66 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
86
87
88
89
90
91
92
93
94
95
package main
import (
"os"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
/**
* 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"`
// 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
}
// Initialize exporter configuration
func initGaugeStats(pGaugeStatsFile string, pGaugeStats *GaugeStats) {
log.Infof("Loading Gauge Stats file %s", pGaugeStatsFile)
// fmt.Println("Loading Gauge Stats file ", configFile)
blob, err := os.ReadFile(pGaugeStatsFile)
if err != nil {
log.Fatalln(err)
}
md, err := toml.Decode(string(blob), &pGaugeStats)
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
pGaugeStats.NamespaceStats = pGaugeStats.createMapFromArray(pGaugeStats.Namespace)
pGaugeStats.NodeStats = pGaugeStats.createMapFromArray(pGaugeStats.Node)
pGaugeStats.SindexStats = pGaugeStats.createMapFromArray(pGaugeStats.Sindex)
pGaugeStats.SetsStats = pGaugeStats.createMapFromArray(pGaugeStats.Sets)
pGaugeStats.XdrStats = pGaugeStats.createMapFromArray(pGaugeStats.Xdr)
// Nullify/empty the Arrays to avoid duplicate stats-copy
pGaugeStats.Namespace = nil
pGaugeStats.Node = nil
pGaugeStats.Sindex = nil
pGaugeStats.Sets = nil
pGaugeStats.Xdr = nil
log.Debugln("# of Gauge Keys defined at Gauge Stat level are: ", len(md.Keys()))
}
/**
* Check if given stat is a Gauge in a given context like Node, Namespace etc.,
*/
func (gm *GaugeStats) isGauge(pContextType ContextType, pStat string) bool {
switch pContextType {
case CTX_NAMESPACE:
return gm.NamespaceStats[pStat]
case CTX_NODE_STATS:
return gm.NodeStats[pStat]
case CTX_SETS:
return gm.SetsStats[pStat]
case CTX_SINDEX:
return gm.SindexStats[pStat]
case CTX_XDR:
return gm.XdrStats[pStat]
}
return false
}
/**
* 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
}