/
prometheus.go
272 lines (248 loc) · 7.76 KB
/
prometheus.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
271
272
package prometheus
import (
"fmt"
"github.com/NodeFactoryIo/vedran/internal/stats"
"os"
"runtime"
"strconv"
"time"
"github.com/NodeFactoryIo/vedran/internal/configuration"
"github.com/NodeFactoryIo/vedran/internal/payout"
"github.com/NodeFactoryIo/vedran/internal/repositories"
schedulepayout "github.com/NodeFactoryIo/vedran/internal/schedule/payout"
"github.com/NodeFactoryIo/vedran/pkg/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
)
const (
nextPayoutDateLayout = "Mon, Jan 2 2006."
FeeStatsIntervalEnv = "PROM_FEE_STATS_INTERVAL"
DefaultFeeStatsCollectionInterval = 12 * time.Hour
NodeStatsIntervalEnv = "PROM_NODE_STATS_INTERVAL"
DefaultNodeStatsCollectionInterval = 15 * time.Second
RequestStatsIntervalEnv = "PROM_REQUEST_STATS_INTERVAL"
DefaultRecordStatsCollectionInterval = 15 * time.Second
PayoutStatsIntervalEnv = "PROM_PAYOUT_STATS_INTERVAL"
DefaultPayoutStatsCollectionInterval = 1 * time.Minute
)
var feeStatsCollectionInterval time.Duration
var nodeStatsCollectionInterval time.Duration
var requestStatsCollectionInterval time.Duration
var payoutStatsCollectionInterval time.Duration
var (
activeNodes = promauto.NewGauge(prometheus.GaugeOpts{
Name: "vedran_number_of_active_nodes",
Help: "The total number of active nodes serving requests",
})
penalizedNodes = promauto.NewGauge(prometheus.GaugeOpts{
Name: "vedran_number_of_penalized_nodes",
Help: "The total number of nodes which are on cooldown",
})
successfulRequests = promauto.NewGauge(prometheus.GaugeOpts{
Name: "vedran_number_of_successful_requests",
Help: "The total number of successful requests served via vedran",
})
failedRequests = promauto.NewGauge(prometheus.GaugeOpts{
Name: "vedran_number_of_failed_requests",
Help: "The total number of successful requests served via vedran",
})
payoutDistribution = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "vedran_payout_distribution",
Help: "Payout distribution per polkadot address",
},
[]string{"address"},
)
payoutDate = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "vedran_payout_date",
Help: "Payout date of next scheduled payout",
},
[]string{"date"},
)
payoutFeeAmount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "vedran_lb_payout_fee",
Help: "Payout fee for each last payout",
},
[]string{"date"})
totalFee = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "vedran_lb_payout_fee_total",
Help: "",
})
nodeFees = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "vedran_nodes_fee",
Help: "Payout fee for each last payout",
},
[]string{"node"})
)
// RecordMetrics starts goroutines for recording metrics
func RecordMetrics(repos repositories.Repos) {
version := promauto.NewGauge(
prometheus.GaugeOpts{
Name: "vedran_version",
Help: "App and golang version of vedran",
ConstLabels: map[string]string{
"go_version": runtime.Version(),
"vedran_version": version.Version,
},
},
)
version.Set(1)
feeAsPercString := fmt.Sprintf(
"%s%%",
strconv.FormatFloat(float64(configuration.Config.Fee*100), 'f', -1, 32),
)
fee := promauto.NewGauge(
prometheus.GaugeOpts{
Name: "vedran_lb_payout_fee_total_percentage",
Help: "Percentage of fee that goes to load balancer",
ConstLabels: map[string]string{
"lb_fee": feeAsPercString,
},
})
fee.Set(float64(configuration.Config.Fee))
setUpCollectionIntervals()
go recordPayoutDistribution(repos)
go recordActiveNodeCount(repos.NodeRepo)
go recordPenalizedNodeCount(repos.NodeRepo)
go recordSuccessfulRequestCount(repos.RecordRepo)
go recordFailedRequestCount(repos.RecordRepo)
go recordPayoutDate(repos)
go recordLbFeeAmount(repos.PayoutRepo)
go recordNodeFees(repos.FeeRepo)
}
func recordNodeFees(repos repositories.FeeRepository) {
for {
fees, err := repos.GetAllFees()
if err != nil {
log.Errorf("Failed to fetch stats for fees because of: %v", err)
time.Sleep(15 * time.Minute)
continue
}
for _, fee := range *fees {
nodeFees.With(prometheus.Labels{"node": fee.NodeId}).Set(float64(fee.TotalFee))
}
time.Sleep(feeStatsCollectionInterval)
}
}
func recordLbFeeAmount(payoutRepo repositories.PayoutRepository) {
for {
payouts, err := payoutRepo.GetAll()
if err != nil {
log.Errorf("Failed recording stats for payout because of: %v", err)
time.Sleep(15 * time.Minute)
continue
}
totalFeeCollected := float64(0)
for _, p := range *payouts {
if p.LbFee != 0 {
payoutFeeAmount.With(prometheus.Labels{
"date": p.Timestamp.Format("2006-January-02"),
}).Set(p.LbFee)
}
totalFeeCollected += p.LbFee
}
totalFee.Set(totalFeeCollected)
time.Sleep(feeStatsCollectionInterval)
}
}
func recordPayoutDistribution(repos repositories.Repos) {
for {
statistics, err := stats.CalculateStatisticsFromLastPayout(repos, time.Now())
if err != nil {
log.Errorf("Failed recording stats for payout because of: %v", err)
time.Sleep(15 * time.Minute)
continue
}
distributionByNode := payout.CalculatePayoutDistributionByNode(
statistics,
100,
payout.LoadBalancerDistributionConfiguration{
FeePercentage: float64(configuration.Config.Fee),
PayoutAddress: "",
DifferentFeeAddress: false,
},
)
for address, distribution := range distributionByNode {
floatDistribution, _ := strconv.ParseFloat(distribution.String(), 64)
payoutDistribution.With(
prometheus.Labels{"address": address},
).Set(
floatDistribution,
)
}
time.Sleep(payoutStatsCollectionInterval)
}
}
func recordPayoutDate(repos repositories.Repos) {
for {
date, err := schedulepayout.GetNextPayoutDate(configuration.Config.PayoutConfiguration, repos)
if err != nil {
payoutDate.With(prometheus.Labels{"date": "Scheduled payout not configured"}).Set(1)
} else {
payoutDate.With(prometheus.Labels{"date": date.Format(nextPayoutDateLayout)}).Set(1)
}
time.Sleep(feeStatsCollectionInterval)
}
}
func recordActiveNodeCount(nodeRepo repositories.NodeRepository) {
for {
activeNodes.Set(float64(len(*nodeRepo.GetAllActiveNodes())))
time.Sleep(nodeStatsCollectionInterval)
}
}
func recordPenalizedNodeCount(nodeRepo repositories.NodeRepository) {
for {
nodes, _ := nodeRepo.GetPenalizedNodes()
penalizedNodes.Set(float64(len(*nodes)))
time.Sleep(nodeStatsCollectionInterval)
}
}
func recordSuccessfulRequestCount(recordRepo repositories.RecordRepository) {
for {
count, _ := recordRepo.CountSuccessfulRequests()
successfulRequests.Set(float64(count))
time.Sleep(requestStatsCollectionInterval)
}
}
func recordFailedRequestCount(recordRepo repositories.RecordRepository) {
for {
count, _ := recordRepo.CountFailedRequests()
failedRequests.Set(float64(count))
time.Sleep(requestStatsCollectionInterval)
}
}
func setUpCollectionIntervals() {
fsi := os.Getenv(FeeStatsIntervalEnv)
if fsi != "" {
feeStatsCollectionInterval, _ = time.ParseDuration(fsi)
}
if feeStatsCollectionInterval == 0 {
feeStatsCollectionInterval = DefaultFeeStatsCollectionInterval
}
nsi := os.Getenv(NodeStatsIntervalEnv)
if nsi != "" {
nodeStatsCollectionInterval, _ = time.ParseDuration(nsi)
}
if nodeStatsCollectionInterval == 0 {
nodeStatsCollectionInterval = DefaultNodeStatsCollectionInterval
}
rsi := os.Getenv(RequestStatsIntervalEnv)
if rsi != "" {
requestStatsCollectionInterval, _ = time.ParseDuration(rsi)
}
if requestStatsCollectionInterval == 0 {
requestStatsCollectionInterval = DefaultRecordStatsCollectionInterval
}
psi := os.Getenv(PayoutStatsIntervalEnv)
if psi != "" {
payoutStatsCollectionInterval, _ = time.ParseDuration(psi)
}
if payoutStatsCollectionInterval == 0 {
payoutStatsCollectionInterval = DefaultPayoutStatsCollectionInterval
}
}