Skip to content

Commit 3ddf6ad

Browse files
author
Mikhail Podtserkovskiy
committed
QA-5339: metrics
1 parent 078a777 commit 3ddf6ad

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

config-sample.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"port": 8126,
5858
"protocol": "udp",
5959
"prefix": "products.tests.qa.debug-dev.jsonwire-grid.",
60-
"enable": false
60+
"enable": false,
61+
"selectors": [
62+
{"tag": "chrome", "capabilities": {"browserName": "chrome"}}
63+
]
6164
}
6265
}

config/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
log "github.com/Sirupsen/logrus"
7+
"github.com/qa-dev/jsonwire-grid/pool/metrics"
78
"os"
89
)
910

@@ -52,11 +53,12 @@ type DB struct {
5253

5354
// Statsd - Settings of metrics sender.
5455
type Statsd struct {
55-
Host string `json:"host"`
56-
Port int `json:"port"`
57-
Protocol string `json:"protocol"`
58-
Prefix string `json:"prefix"`
59-
Enable bool `json:"enable"`
56+
Host string `json:"host"`
57+
Port int `json:"port"`
58+
Protocol string `json:"protocol"`
59+
Prefix string `json:"prefix"`
60+
Enable bool `json:"enable"`
61+
CapabilitiesList []metrics.CapabilitiesSelector `json:"selectors"`
6062
}
6163

6264
// New - Constructor of config.

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func main() {
102102
cfg.Statsd.Protocol,
103103
cfg.Statsd.Prefix,
104104
cfg.Statsd.Enable)
105-
poolMetricsSender := poolMetrics.NewSender(statsdClient, poolInstance, time.Second*1) // todo: move to config
105+
// todo: move to config
106+
poolMetricsSender := poolMetrics.NewSender(statsdClient, poolInstance, time.Second*1, cfg.Statsd.CapabilitiesList, capsComparator)
106107
go poolMetricsSender.SendAll()
107108
if err != nil {
108109
log.Errorf("Statsd create socked error: %s", err)

pool/metrics/metrics.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@ package metrics
33
import (
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.
1112
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"`
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.
2337
func (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

Comments
 (0)