-
Notifications
You must be signed in to change notification settings - Fork 203
/
metrics_provider.go
87 lines (79 loc) · 2.53 KB
/
metrics_provider.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
package metrics
import (
"sync"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
"github.com/prometheus/client_golang/prometheus"
)
const (
AvailabilityLabel = "availability"
SyncLabel = "sync/freshness"
LatencyLabel = "latency"
)
type ProviderMetrics struct {
specID string
apiInterface string
lock sync.Mutex
totalCUServicedMetric *prometheus.CounterVec
totalCUPaidMetric *prometheus.CounterVec
totalRelaysServicedMetric *prometheus.CounterVec
totalErroredMetric *prometheus.CounterVec
consumerQoSMetric *prometheus.GaugeVec
}
func (pm *ProviderMetrics) AddRelay(consumerAddress string, cu uint64, qos *pairingtypes.QualityOfServiceReport) {
if pm == nil {
return
}
pm.lock.Lock()
defer pm.lock.Unlock()
pm.totalCUServicedMetric.WithLabelValues(pm.specID, pm.apiInterface).Add(float64(cu))
pm.totalRelaysServicedMetric.WithLabelValues(pm.specID, pm.apiInterface).Add(1)
if qos == nil {
return
}
availability, err := qos.Availability.Float64()
if err == nil {
pm.consumerQoSMetric.WithLabelValues(pm.specID, consumerAddress, AvailabilityLabel).Set(availability)
}
sync, err := qos.Sync.Float64()
if err == nil {
pm.consumerQoSMetric.WithLabelValues(pm.specID, consumerAddress, SyncLabel).Set(sync)
}
latency, err := qos.Latency.Float64()
if err == nil {
pm.consumerQoSMetric.WithLabelValues(pm.specID, consumerAddress, LatencyLabel).Set(latency)
}
}
func (pm *ProviderMetrics) AddPayment(cu uint64) {
if pm == nil {
return
}
pm.lock.Lock()
defer pm.lock.Unlock()
pm.totalCUPaidMetric.WithLabelValues(pm.specID).Add(float64(cu))
}
func (pm *ProviderMetrics) AddError() {
if pm == nil {
return
}
pm.lock.Lock()
defer pm.lock.Unlock()
pm.totalErroredMetric.WithLabelValues(pm.specID, pm.apiInterface).Add(1)
}
func NewProviderMetrics(specID, apiInterface string, totalCUServicedMetric *prometheus.CounterVec,
totalCUPaidMetric *prometheus.CounterVec,
totalRelaysServicedMetric *prometheus.CounterVec,
totalErroredMetric *prometheus.CounterVec,
consumerQoSMetric *prometheus.GaugeVec,
) *ProviderMetrics {
pm := &ProviderMetrics{
specID: specID,
apiInterface: apiInterface,
lock: sync.Mutex{},
totalCUServicedMetric: totalCUServicedMetric,
totalCUPaidMetric: totalCUPaidMetric,
totalRelaysServicedMetric: totalRelaysServicedMetric,
totalErroredMetric: totalErroredMetric,
consumerQoSMetric: consumerQoSMetric,
}
return pm
}