@@ -3,27 +3,42 @@ package metrics
33import (
44 log "github.com/Sirupsen/logrus"
55 "github.com/qa-dev/jsonwire-grid/pool"
6+ "github.com/qa-dev/jsonwire-grid/pool/capabilities"
67 "gopkg.in/alexcesaro/statsd.v2"
78 "time"
89)
910
1011// Sender - metrics sender.
1112type 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"`
1523}
1624
1725// 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 }
2034}
2135
2236// NewSender - sends metrics of nodes availability.
2337func (s * Sender ) SendAll () {
2438 for {
2539 s .countAvailableNodes ()
2640 s .countTotalNodes ()
41+ s .countByCapabilities ()
2742 time .Sleep (s .duration )
2843 }
2944}
@@ -46,3 +61,41 @@ func (s *Sender) countAvailableNodes() {
4661 }
4762 s .statd .Gauge ("node.available" , count )
4863}
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