-
Notifications
You must be signed in to change notification settings - Fork 146
/
chain_pipe.go
154 lines (131 loc) · 3.92 KB
/
chain_pipe.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package metrics
import (
"fmt"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/iotaledger/wasp/packages/isc"
)
type IChainPipeMetrics interface {
TrackPipeLen(name string, lenFunc func() int)
TrackPipeLenMax(name string, key string, lenFunc func() int)
ForgetPipeLenMax(name string, key string)
}
type emptyChainPipeMetrics struct{}
func NewEmptyChainPipeMetrics() IChainPipeMetrics { return &emptyChainPipeMetrics{} }
func (m *emptyChainPipeMetrics) TrackPipeLen(name string, lenFunc func() int) {}
func (m *emptyChainPipeMetrics) TrackPipeLenMax(name string, key string, lenFunc func() int) {}
func (m *emptyChainPipeMetrics) ForgetPipeLenMax(name string, key string) {}
type chainPipeMetrics struct {
chainID isc.ChainID
provider *ChainMetricsProvider
lenMetrics map[string]prometheus.Collector
maxMetrics map[string]*chainPipeMaxCollector
regLock *sync.RWMutex
}
type chainPipeMaxCollector struct {
collector prometheus.Collector
valueFuncs map[string]func() int
}
func newChainPipeMetric(provider *ChainMetricsProvider, chainID isc.ChainID) *chainPipeMetrics {
return &chainPipeMetrics{
chainID: chainID,
provider: provider,
lenMetrics: map[string]prometheus.Collector{},
maxMetrics: map[string]*chainPipeMaxCollector{},
regLock: &sync.RWMutex{},
}
}
func (m *chainPipeMetrics) cleanup() {
m.regLock.Lock()
defer m.regLock.Unlock()
reg := m.provider.pipeLenRegistry
if reg == nil {
return
}
for _, collector := range m.lenMetrics {
reg.Unregister(collector)
}
m.lenMetrics = map[string]prometheus.Collector{}
for _, maxCollector := range m.maxMetrics {
reg.Unregister(maxCollector.collector)
}
m.maxMetrics = map[string]*chainPipeMaxCollector{}
}
func (m *chainPipeMetrics) makeLabels(pipeName string) prometheus.Labels {
return prometheus.Labels{
labelNameChain: m.chainID.String(),
labelNamePipeName: pipeName,
}
}
func (m *chainPipeMetrics) TrackPipeLen(name string, lenFunc func() int) {
m.regLock.Lock()
defer m.regLock.Unlock()
reg := m.provider.pipeLenRegistry
if reg == nil {
return
}
if oldCollector, ok := m.lenMetrics[name]; ok {
reg.Unregister(oldCollector)
}
collector := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "iota_wasp",
Subsystem: "chain_pipe",
Name: "len",
Help: "Length of a pipe",
ConstLabels: m.makeLabels(name),
}, func() float64 { return float64(lenFunc()) })
m.lenMetrics[name] = collector
if err := reg.Register(collector); err != nil {
panic(fmt.Errorf("failed to register pipe %v len metric for chain %v: %w", name, m.chainID, err))
}
}
func (m *chainPipeMetrics) TrackPipeLenMax(name string, key string, lenFunc func() int) {
m.regLock.Lock()
defer m.regLock.Unlock()
reg := m.provider.pipeLenRegistry
if reg == nil {
return
}
maxCollector, found := m.maxMetrics[name]
if !found {
valueFuncs := map[string]func() int{}
collector := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "iota_wasp",
Subsystem: "chain_pipe",
Name: "len",
Help: "Length of a pipe",
ConstLabels: m.makeLabels(name),
}, func() float64 {
max := 0
for _, f := range valueFuncs {
fVal := f()
if max < fVal {
max = fVal
}
}
return float64(max)
})
if err := reg.Register(collector); err != nil {
panic(fmt.Errorf("failed to register pipe %v max len metric for chain %v: %w", name, m.chainID, err))
}
maxCollector = &chainPipeMaxCollector{
collector: collector,
valueFuncs: valueFuncs,
}
m.maxMetrics[name] = maxCollector
}
maxCollector.valueFuncs[key] = lenFunc
}
func (m *chainPipeMetrics) ForgetPipeLenMax(name string, key string) {
m.regLock.Lock()
defer m.regLock.Unlock()
reg := m.provider.pipeLenRegistry
if reg == nil {
return
}
maxCollector, found := m.maxMetrics[name]
if !found {
return
}
delete(maxCollector.valueFuncs, key)
}