-
Notifications
You must be signed in to change notification settings - Fork 75
/
metrics.go
69 lines (55 loc) · 1.26 KB
/
metrics.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
package relay
import (
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/sonm-io/core/proto"
"go.uber.org/atomic"
)
type metrics struct {
ConnCurrent *atomic.Uint64
mu sync.Mutex
net map[common.Address]*netMetrics
}
func newMetrics() *metrics {
return &metrics{
ConnCurrent: atomic.NewUint64(0),
net: map[common.Address]*netMetrics{},
}
}
func (m *metrics) NetMetrics(addr common.Address) *netMetrics {
m.mu.Lock()
defer m.mu.Unlock()
metrics, ok := m.net[addr]
if !ok {
metrics = newNetMetrics()
m.net[addr] = metrics
}
return metrics
}
func (m *metrics) Dump() *sonm.RelayMetrics {
m.mu.Lock()
defer m.mu.Unlock()
netMetrics := map[string]*sonm.NetMetrics{}
for addr, metrics := range m.net {
netMetrics[addr.Hex()] = &sonm.NetMetrics{
TxBytes: metrics.TxBytes.Load(),
RxBytes: metrics.RxBytes.Load(),
}
}
return &sonm.RelayMetrics{
ConnCurrent: m.ConnCurrent.Load(),
Net: netMetrics,
}
}
type netMetrics struct {
// TxBytes shows the number of bytes sent from a server.
TxBytes *atomic.Uint64
// RxBytes shows the number of bytes received by a server.
RxBytes *atomic.Uint64
}
func newNetMetrics() *netMetrics {
return &netMetrics{
TxBytes: atomic.NewUint64(0),
RxBytes: atomic.NewUint64(0),
}
}