|
| 1 | +package monitoring |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/rsksmart/liquidity-provider-server/internal/entities" |
| 7 | + "github.com/rsksmart/liquidity-provider-server/internal/entities/cold_wallet" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +type ColdWalletMetricsWatcher struct { |
| 12 | + appMetrics *Metrics |
| 13 | + eventBus entities.EventBus |
| 14 | + closeChannel chan struct{} |
| 15 | +} |
| 16 | + |
| 17 | +func NewColdWalletMetricsWatcher( |
| 18 | + appMetrics *Metrics, |
| 19 | + eventBus entities.EventBus, |
| 20 | +) *ColdWalletMetricsWatcher { |
| 21 | + closeChannel := make(chan struct{}, 1) |
| 22 | + return &ColdWalletMetricsWatcher{ |
| 23 | + appMetrics: appMetrics, |
| 24 | + eventBus: eventBus, |
| 25 | + closeChannel: closeChannel, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (watcher *ColdWalletMetricsWatcher) Prepare(ctx context.Context) error { |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// nolint: cyclop |
| 34 | +func (watcher *ColdWalletMetricsWatcher) Start() { |
| 35 | + rbtcThresholdChannel := watcher.eventBus.Subscribe(cold_wallet.RbtcTransferredDueToThresholdEventId) |
| 36 | + btcThresholdChannel := watcher.eventBus.Subscribe(cold_wallet.BtcTransferredDueToThresholdEventId) |
| 37 | + rbtcTimeForcingChannel := watcher.eventBus.Subscribe(cold_wallet.RbtcTransferredDueToTimeForcingEventId) |
| 38 | + btcTimeForcingChannel := watcher.eventBus.Subscribe(cold_wallet.BtcTransferredDueToTimeForcingEventId) |
| 39 | + |
| 40 | +metricLoop: |
| 41 | + for { |
| 42 | + select { |
| 43 | + case event := <-rbtcThresholdChannel: |
| 44 | + if typedEvent, ok := event.(cold_wallet.RbtcTransferredDueToThresholdEvent); ok { |
| 45 | + watcher.appMetrics.ColdWalletTransfersMetric.WithLabelValues("rbtc", "threshold").Inc() |
| 46 | + watcher.appMetrics.ColdWalletLastAmountMetric.WithLabelValues("rbtc").Set(weiToBtcFloat64(typedEvent.Amount)) |
| 47 | + } |
| 48 | + case event := <-btcThresholdChannel: |
| 49 | + if typedEvent, ok := event.(cold_wallet.BtcTransferredDueToThresholdEvent); ok { |
| 50 | + watcher.appMetrics.ColdWalletTransfersMetric.WithLabelValues("btc", "threshold").Inc() |
| 51 | + watcher.appMetrics.ColdWalletLastAmountMetric.WithLabelValues("btc").Set(weiToBtcFloat64(typedEvent.Amount)) |
| 52 | + } |
| 53 | + case event := <-rbtcTimeForcingChannel: |
| 54 | + if typedEvent, ok := event.(cold_wallet.RbtcTransferredDueToTimeForcingEvent); ok { |
| 55 | + watcher.appMetrics.ColdWalletTransfersMetric.WithLabelValues("rbtc", "time_forcing").Inc() |
| 56 | + watcher.appMetrics.ColdWalletLastAmountMetric.WithLabelValues("rbtc").Set(weiToBtcFloat64(typedEvent.Amount)) |
| 57 | + } |
| 58 | + case event := <-btcTimeForcingChannel: |
| 59 | + if typedEvent, ok := event.(cold_wallet.BtcTransferredDueToTimeForcingEvent); ok { |
| 60 | + watcher.appMetrics.ColdWalletTransfersMetric.WithLabelValues("btc", "time_forcing").Inc() |
| 61 | + watcher.appMetrics.ColdWalletLastAmountMetric.WithLabelValues("btc").Set(weiToBtcFloat64(typedEvent.Amount)) |
| 62 | + } |
| 63 | + case <-watcher.closeChannel: |
| 64 | + close(watcher.closeChannel) |
| 65 | + break metricLoop |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (watcher *ColdWalletMetricsWatcher) Shutdown(closeChannel chan<- bool) { |
| 71 | + watcher.closeChannel <- struct{}{} |
| 72 | + closeChannel <- true |
| 73 | + log.Debug("Cold wallet metrics watcher shutdown completed") |
| 74 | +} |
0 commit comments