-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.go
82 lines (69 loc) · 2.11 KB
/
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
package main
import (
"log"
"reflect"
"sync"
"time"
"github.com/blockassets/bwpool_exporter/bwpool"
"github.com/blockassets/prometheus_helper"
"github.com/prometheus/client_golang/prometheus"
)
//
var (
namespace = "bwpool"
idLabelNames = []string{"id"}
)
// Collector interface
type Exporter struct {
client *bwpool.BWClient
ConstLabels prometheus.Labels
Gauges prometheus_helper.GaugeMapMap
GaugeVecs prometheus_helper.GaugeVecMapMap
sync.Mutex
}
//
func NewExporter(poolConfig *bwpool.PoolConfig, timeout time.Duration) *Exporter {
constLabels := prometheus.Labels{"key": poolConfig.PublicKey[:8]}
structFieldMap := prometheus_helper.NewStructFieldMap(bwpool.PoolData{})
exporter := &Exporter{
client: bwpool.NewClient(poolConfig, timeout),
ConstLabels: constLabels,
Gauges: prometheus_helper.NewGaugeMapMap(structFieldMap, namespace, constLabels),
GaugeVecs: make(prometheus_helper.GaugeVecMapMap),
}
return exporter
}
//
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
prometheus_helper.DescribeGaugeMapMap(e.Gauges, ch)
prometheus_helper.DescribeGaugeVecMapMap(e.GaugeVecs, ch)
}
//
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// Prevents multiple concurrent calls
e.Lock()
defer e.Unlock()
poolData, err := e.client.FetchWorkers()
if err != nil {
log.Println(err)
return
}
poolDataMap := prometheus_helper.NewStructFieldMap(*poolData)
for key, value := range poolDataMap {
val := reflect.ValueOf(value)
// 'Workers' is a special case as a GaugeVec
if val.Kind() == reflect.Map {
for _, key := range val.MapKeys() {
name := key.Interface().(string)
worker := val.MapIndex(key).Interface()
prometheus_helper.CollectGaugeVecs(name, worker, e.GaugeVecs, namespace, e.ConstLabels, idLabelNames, prometheus.Labels{idLabelNames[0]: name})
}
} else {
meta := prometheus_helper.StructMeta{}
prometheus_helper.MakeStructMeta(value, &meta)
prometheus_helper.SetValuesOnGauges(meta, namespace, e.Gauges[key])
}
}
prometheus_helper.CollectGaugeMapMap(e.Gauges, ch)
prometheus_helper.CollectGaugeVecMapMap(e.GaugeVecs, ch)
}