@@ -3,27 +3,42 @@ package metrics
3
3
import (
4
4
log "github.com/Sirupsen/logrus"
5
5
"github.com/qa-dev/jsonwire-grid/pool"
6
+ "github.com/qa-dev/jsonwire-grid/pool/capabilities"
6
7
"gopkg.in/alexcesaro/statsd.v2"
7
8
"time"
8
9
)
9
10
10
11
// Sender - metrics sender.
11
12
type Sender struct {
12
- statd * statsd.Client
13
- pool * pool.Pool
14
- duration time.Duration
13
+ statd * statsd.Client
14
+ pool * pool.Pool
15
+ duration time.Duration
16
+ selectorList []CapabilitiesSelector
17
+ capsComparator capabilities.ComparatorInterface
18
+ }
19
+
20
+ type CapabilitiesSelector struct {
21
+ Tag string `json:"tag"`
22
+ Capabilities capabilities.Capabilities `json:"capabilities"`
15
23
}
16
24
17
25
// NewSender - constructor of sender.
18
- func NewSender (statd * statsd.Client , pool * pool.Pool , duration time.Duration ) * Sender {
19
- return & Sender {statd , pool , duration }
26
+ func NewSender (
27
+ statd * statsd.Client ,
28
+ pool * pool.Pool ,
29
+ duration time.Duration ,
30
+ capsMetricList []CapabilitiesSelector ,
31
+ capsComparator capabilities.ComparatorInterface ,
32
+ ) * Sender {
33
+ return & Sender {statd , pool , duration , capsMetricList , capsComparator }
20
34
}
21
35
22
36
// NewSender - sends metrics of nodes availability.
23
37
func (s * Sender ) SendAll () {
24
38
for {
25
39
s .countAvailableNodes ()
26
40
s .countTotalNodes ()
41
+ s .countByCapabilities ()
27
42
time .Sleep (s .duration )
28
43
}
29
44
}
@@ -46,3 +61,41 @@ func (s *Sender) countAvailableNodes() {
46
61
}
47
62
s .statd .Gauge ("node.available" , count )
48
63
}
64
+
65
+ func (s * Sender ) countByCapabilities () {
66
+ nodeList , err := s .pool .GetAll ()
67
+ if err != nil {
68
+ log .Error ("Can't get all nodes: " , err .Error ())
69
+ return
70
+ }
71
+
72
+ for _ , node := range nodeList {
73
+ for _ , availableCaps := range node .CapabilitiesList {
74
+ s .capsComparator .Register (availableCaps )
75
+ }
76
+ }
77
+
78
+ for _ , requiredCaps := range s .selectorList {
79
+ availableCount := 0
80
+ reservedCount := 0
81
+ busyCount := 0
82
+ for _ , node := range nodeList {
83
+ for _ , availableCaps := range node .CapabilitiesList {
84
+ if s .capsComparator .Compare (requiredCaps .Capabilities , availableCaps ) {
85
+ switch node .Status {
86
+ case pool .NodeStatusAvailable :
87
+ availableCount ++
88
+ case pool .NodeStatusReserved :
89
+ reservedCount ++
90
+ case pool .NodeStatusBusy :
91
+ busyCount ++
92
+ }
93
+ break
94
+ }
95
+ }
96
+ }
97
+ s .statd .Gauge ("node-by-caps." + requiredCaps .Tag + ".available" , availableCount )
98
+ s .statd .Gauge ("node-by-caps." + requiredCaps .Tag + ".reserved" , reservedCount )
99
+ s .statd .Gauge ("node-by-caps." + requiredCaps .Tag + ".busy" , busyCount )
100
+ }
101
+ }
0 commit comments