@@ -3,6 +3,7 @@ package prometheus
33import (
44 "fmt"
55 "github.com/NodeFactoryIo/vedran/internal/stats"
6+ "os"
67 "runtime"
78 "strconv"
89 "time"
@@ -19,8 +20,22 @@ import (
1920
2021const (
2122 nextPayoutDateLayout = "Mon, Jan 2 2006."
23+
24+ FeeStatsIntervalEnv = "PROM_FEE_STATS_INTERVAL"
25+ DefaultFeeStatsCollectionInterval = 12 * time .Hour
26+ NodeStatsIntervalEnv = "PROM_NODE_STATS_INTERVAL"
27+ DefaultNodeStatsCollectionInterval = 15 * time .Second
28+ RequestStatsIntervalEnv = "PROM_REQUEST_STATS_INTERVAL"
29+ DefaultRecordStatsCollectionInterval = 15 * time .Second
30+ PayoutStatsIntervalEnv = "PROM_PAYOUT_STATS_INTERVAL"
31+ DefaultPayoutStatsCollectionInterval = 1 * time .Minute
2232)
2333
34+ var feeStatsCollectionInterval time.Duration
35+ var nodeStatsCollectionInterval time.Duration
36+ var requestStatsCollectionInterval time.Duration
37+ var payoutStatsCollectionInterval time.Duration
38+
2439var (
2540 activeNodes = promauto .NewGauge (prometheus.GaugeOpts {
2641 Name : "vedran_number_of_active_nodes" ,
@@ -101,6 +116,8 @@ func RecordMetrics(repos repositories.Repos) {
101116 })
102117 fee .Set (float64 (configuration .Config .Fee ))
103118
119+ setUpCollectionIntervals ()
120+
104121 go recordPayoutDistribution (repos )
105122 go recordActiveNodeCount (repos .NodeRepo )
106123 go recordPenalizedNodeCount (repos .NodeRepo )
@@ -122,7 +139,7 @@ func recordNodeFees(repos repositories.FeeRepository) {
122139 for _ , fee := range * fees {
123140 nodeFees .With (prometheus.Labels {"node" : fee .NodeId }).Set (float64 (fee .TotalFee ))
124141 }
125- time .Sleep (12 * time . Hour )
142+ time .Sleep (feeStatsCollectionInterval )
126143 }
127144}
128145
@@ -144,7 +161,7 @@ func recordLbFeeAmount(payoutRepo repositories.PayoutRepository) {
144161 totalFeeCollected += p .LbFee
145162 }
146163 totalFee .Set (totalFeeCollected )
147- time .Sleep (12 * time . Hour )
164+ time .Sleep (feeStatsCollectionInterval )
148165 }
149166}
150167
@@ -176,7 +193,7 @@ func recordPayoutDistribution(repos repositories.Repos) {
176193 )
177194 }
178195
179- time .Sleep (1 * time . Minute )
196+ time .Sleep (payoutStatsCollectionInterval )
180197 }
181198}
182199
@@ -188,37 +205,68 @@ func recordPayoutDate(repos repositories.Repos) {
188205 } else {
189206 payoutDate .With (prometheus.Labels {"date" : date .Format (nextPayoutDateLayout )}).Set (1 )
190207 }
191- time .Sleep (12 * time . Hour )
208+ time .Sleep (feeStatsCollectionInterval )
192209 }
193210}
194211
195212func recordActiveNodeCount (nodeRepo repositories.NodeRepository ) {
196213 for {
197214 activeNodes .Set (float64 (len (* nodeRepo .GetAllActiveNodes ())))
198- time .Sleep (15 * time . Second )
215+ time .Sleep (nodeStatsCollectionInterval )
199216 }
200217}
201218
202219func recordPenalizedNodeCount (nodeRepo repositories.NodeRepository ) {
203220 for {
204221 nodes , _ := nodeRepo .GetPenalizedNodes ()
205222 penalizedNodes .Set (float64 (len (* nodes )))
206- time .Sleep (15 * time . Second )
223+ time .Sleep (nodeStatsCollectionInterval )
207224 }
208225}
209226
210227func recordSuccessfulRequestCount (recordRepo repositories.RecordRepository ) {
211228 for {
212229 count , _ := recordRepo .CountSuccessfulRequests ()
213230 successfulRequests .Set (float64 (count ))
214- time .Sleep (15 * time . Second )
231+ time .Sleep (requestStatsCollectionInterval )
215232 }
216233}
217234
218235func recordFailedRequestCount (recordRepo repositories.RecordRepository ) {
219236 for {
220237 count , _ := recordRepo .CountFailedRequests ()
221238 failedRequests .Set (float64 (count ))
222- time .Sleep (15 * time .Second )
239+ time .Sleep (requestStatsCollectionInterval )
240+ }
241+ }
242+
243+ func setUpCollectionIntervals () {
244+ fsi := os .Getenv (FeeStatsIntervalEnv )
245+ if fsi != "" {
246+ feeStatsCollectionInterval , _ = time .ParseDuration (fsi )
247+ }
248+ if feeStatsCollectionInterval == 0 {
249+ feeStatsCollectionInterval = DefaultFeeStatsCollectionInterval
250+ }
251+ nsi := os .Getenv (NodeStatsIntervalEnv )
252+ if nsi != "" {
253+ nodeStatsCollectionInterval , _ = time .ParseDuration (nsi )
254+ }
255+ if nodeStatsCollectionInterval == 0 {
256+ nodeStatsCollectionInterval = DefaultNodeStatsCollectionInterval
257+ }
258+ rsi := os .Getenv (RequestStatsIntervalEnv )
259+ if rsi != "" {
260+ requestStatsCollectionInterval , _ = time .ParseDuration (rsi )
261+ }
262+ if requestStatsCollectionInterval == 0 {
263+ requestStatsCollectionInterval = DefaultRecordStatsCollectionInterval
264+ }
265+ psi := os .Getenv (PayoutStatsIntervalEnv )
266+ if psi != "" {
267+ payoutStatsCollectionInterval , _ = time .ParseDuration (psi )
268+ }
269+ if payoutStatsCollectionInterval == 0 {
270+ payoutStatsCollectionInterval = DefaultPayoutStatsCollectionInterval
223271 }
224272}
0 commit comments