-
Notifications
You must be signed in to change notification settings - Fork 203
/
metrics_provider_manager.go
270 lines (238 loc) · 9.68 KB
/
metrics_provider_manager.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package metrics
import (
"net/http"
"sync"
"time"
"github.com/lavanet/lava/utils"
spectypes "github.com/lavanet/lava/x/spec/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
MetricsListenFlagName = "metrics-listen-address"
DisabledFlagOption = "disabled"
)
type ProviderMetricsManager struct {
providerMetrics map[string]*ProviderMetrics
lock sync.RWMutex
totalCUServicedMetric *prometheus.CounterVec
totalCUPaidMetric *prometheus.CounterVec
totalRelaysServicedMetric *prometheus.CounterVec
totalErroredMetric *prometheus.CounterVec
consumerQoSMetric *prometheus.GaugeVec
blockMetric *prometheus.GaugeVec
lastServicedBlockTimeMetric *prometheus.GaugeVec
disabledChainsMetric *prometheus.GaugeVec
fetchLatestFailedMetric *prometheus.CounterVec
fetchBlockFailedMetric *prometheus.CounterVec
fetchLatestSuccessMetric *prometheus.CounterVec
fetchBlockSuccessMetric *prometheus.CounterVec
protocolVersionMetric *prometheus.GaugeVec
virtualEpochMetric *prometheus.GaugeVec
}
func NewProviderMetricsManager(networkAddress string) *ProviderMetricsManager {
if networkAddress == DisabledFlagOption {
utils.LavaFormatWarning("prometheus endpoint inactive, option is disabled", nil)
return nil
}
totalCUServicedMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_total_cu_serviced",
Help: "The total number of CUs serviced by the provider over time.",
}, []string{"spec", "apiInterface"})
// Create a new GaugeVec metric to represent the TotalRelaysServiced over time.
totalRelaysServicedMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_total_relays_serviced",
Help: "The total number of relays serviced by the provider over time.",
}, []string{"spec", "apiInterface"})
// Create a new GaugeVec metric to represent the TotalErrored over time.
totalErroredMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_total_errored",
Help: "The total number of errors encountered by the provider over time.",
}, []string{"spec", "apiInterface"})
consumerQoSMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lava_consumer_QoS",
Help: "The latest QoS score from a consumer",
}, []string{"spec", "consumer_address", "qos_metric"})
blockMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lava_latest_block",
Help: "The latest block measured",
}, []string{"spec"})
// Create a new GaugeVec metric to represent the TotalCUPaid over time.
totalCUPaidMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_total_cu_paid",
Help: "The total number of CUs paid to the provider over time.",
}, []string{"spec"})
// Create a new GaugeVec metric to represent the last block update time.
lastServicedBlockTimeMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lava_provider_last_serviced_block_update_time_seconds",
Help: "Timestamp of the last block update received from the serviced node.",
}, []string{"spec"})
disabledChainsMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lava_provider_disabled_chains",
Help: "value of 1 for each disabled chain at measurement time",
}, []string{"chainID", "apiInterface"})
fetchLatestFailedMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_fetch_latest_fails",
Help: "The total number of get latest block queries that errored by chainfetcher",
}, []string{"spec"})
fetchBlockFailedMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_fetch_block_fails",
Help: "The total number of get specific block queries that errored by chainfetcher",
}, []string{"spec"})
fetchLatestSuccessMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_fetch_latest_success",
Help: "The total number of get latest block queries that succeeded by chainfetcher",
}, []string{"spec"})
fetchBlockSuccessMetric := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "lava_provider_fetch_block_success",
Help: "The total number of get specific block queries that succeeded by chainfetcher",
}, []string{"spec"})
virtualEpochMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "virtual_epoch",
Help: "The current virtual epoch measured",
}, []string{"spec"})
protocolVersionMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "lava_provider_protocol_version",
Help: "The current running lavap version for the process. major := version / 1000000, minor := (version / 1000) % 1000 patch := version % 1000",
}, []string{"version"})
// Register the metrics with the Prometheus registry.
prometheus.MustRegister(totalCUServicedMetric)
prometheus.MustRegister(totalCUPaidMetric)
prometheus.MustRegister(totalRelaysServicedMetric)
prometheus.MustRegister(totalErroredMetric)
prometheus.MustRegister(consumerQoSMetric)
prometheus.MustRegister(blockMetric)
prometheus.MustRegister(lastServicedBlockTimeMetric)
prometheus.MustRegister(disabledChainsMetric)
prometheus.MustRegister(fetchLatestFailedMetric)
prometheus.MustRegister(fetchBlockFailedMetric)
prometheus.MustRegister(fetchLatestSuccessMetric)
prometheus.MustRegister(fetchBlockSuccessMetric)
prometheus.MustRegister(virtualEpochMetric)
prometheus.MustRegister(protocolVersionMetric)
http.Handle("/metrics", promhttp.Handler())
go func() {
utils.LavaFormatInfo("prometheus endpoint listening", utils.Attribute{Key: "Listen Address", Value: networkAddress})
http.ListenAndServe(networkAddress, nil)
}()
return &ProviderMetricsManager{
providerMetrics: map[string]*ProviderMetrics{},
totalCUServicedMetric: totalCUServicedMetric,
totalCUPaidMetric: totalCUPaidMetric,
totalRelaysServicedMetric: totalRelaysServicedMetric,
totalErroredMetric: totalErroredMetric,
consumerQoSMetric: consumerQoSMetric,
blockMetric: blockMetric,
lastServicedBlockTimeMetric: lastServicedBlockTimeMetric,
disabledChainsMetric: disabledChainsMetric,
fetchLatestFailedMetric: fetchLatestFailedMetric,
fetchBlockFailedMetric: fetchBlockFailedMetric,
fetchLatestSuccessMetric: fetchLatestSuccessMetric,
fetchBlockSuccessMetric: fetchBlockSuccessMetric,
virtualEpochMetric: virtualEpochMetric,
protocolVersionMetric: protocolVersionMetric,
}
}
func (pme *ProviderMetricsManager) getProviderMetric(specID, apiInterface string) *ProviderMetrics {
pme.lock.RLock()
defer pme.lock.RUnlock()
return pme.providerMetrics[specID+apiInterface]
}
func (pme *ProviderMetricsManager) setProviderMetric(metrics *ProviderMetrics) {
pme.lock.Lock()
defer pme.lock.Unlock()
specID := metrics.specID
apiInterface := metrics.apiInterface
pme.providerMetrics[specID+apiInterface] = metrics
}
func (pme *ProviderMetricsManager) AddProviderMetrics(specID, apiInterface string) *ProviderMetrics {
if pme == nil {
return nil
}
if pme.getProviderMetric(specID, apiInterface) == nil {
providerMetric := NewProviderMetrics(specID, apiInterface, pme.totalCUServicedMetric, pme.totalCUPaidMetric, pme.totalRelaysServicedMetric, pme.totalErroredMetric, pme.consumerQoSMetric)
pme.setProviderMetric(providerMetric)
}
return pme.getProviderMetric(specID, apiInterface)
}
func (pme *ProviderMetricsManager) SetLatestBlock(specID string, block uint64) {
if pme == nil {
return
}
pme.lock.Lock()
defer pme.lock.Unlock()
pme.blockMetric.WithLabelValues(specID).Set(float64(block))
pme.lastServicedBlockTimeMetric.WithLabelValues(specID).Set(float64(time.Now().Unix()))
}
func (pme *ProviderMetricsManager) AddPayment(specID string, cu uint64) {
if pme == nil {
return
}
availableAPIInterface := []string{
spectypes.APIInterfaceJsonRPC,
spectypes.APIInterfaceTendermintRPC,
spectypes.APIInterfaceRest,
spectypes.APIInterfaceGrpc,
}
for _, apiInterface := range availableAPIInterface {
providerMetrics := pme.getProviderMetric(specID, apiInterface)
if providerMetrics != nil {
go providerMetrics.AddPayment(cu)
break // we need to increase the metric only once
}
}
}
func (pme *ProviderMetricsManager) SetBlock(latestLavaBlock int64) {
if pme == nil {
return
}
pme.blockMetric.WithLabelValues("lava").Set(float64(latestLavaBlock))
}
func (pme *ProviderMetricsManager) SetDisabledChain(specID string, apInterface string) {
if pme == nil {
return
}
pme.disabledChainsMetric.WithLabelValues(specID, apInterface).Set(1)
}
func (pme *ProviderMetricsManager) SetEnabledChain(specID string, apInterface string) {
if pme == nil {
return
}
pme.disabledChainsMetric.WithLabelValues(specID, apInterface).Set(0)
}
func (pme *ProviderMetricsManager) SetLatestBlockFetchError(specID string) {
if pme == nil {
return
}
pme.fetchLatestFailedMetric.WithLabelValues(specID).Add(1)
}
func (pme *ProviderMetricsManager) SetSpecificBlockFetchError(specID string) {
if pme == nil {
return
}
pme.fetchBlockFailedMetric.WithLabelValues(specID).Add(1)
}
func (pme *ProviderMetricsManager) SetLatestBlockFetchSuccess(specID string) {
if pme == nil {
return
}
pme.fetchLatestSuccessMetric.WithLabelValues(specID).Add(1)
}
func (pme *ProviderMetricsManager) SetSpecificBlockFetchSuccess(specID string) {
if pme == nil {
return
}
pme.fetchBlockSuccessMetric.WithLabelValues(specID).Add(1)
}
func (pme *ProviderMetricsManager) SetVirtualEpoch(virtualEpoch uint64) {
if pme == nil {
return
}
pme.virtualEpochMetric.WithLabelValues("lava").Set(float64(virtualEpoch))
}
func (pme *ProviderMetricsManager) SetVersion(version string) {
if pme == nil {
return
}
SetVersionInner(pme.protocolVersionMetric, version)
}