|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "github.com/ethereum/go-ethereum/ethclient" |
| 7 | + "github.com/ethereum/go-ethereum/log" |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 10 | + "gorm.io/gorm" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + TxSignerBalance = promauto.NewGauge(prometheus.GaugeOpts{ |
| 16 | + Name: "opbnb_bridge_bot_tx_signer_balance", |
| 17 | + Help: "The balance of the tx signer", |
| 18 | + }) |
| 19 | + |
| 20 | + ScannedBlockNumber = promauto.NewGauge(prometheus.GaugeOpts{ |
| 21 | + Name: "opbnb_bridge_bot_scanned_block_number", |
| 22 | + Help: "The block number that has been scanned", |
| 23 | + }) |
| 24 | + |
| 25 | + UnprovenWithdrawals = promauto.NewGauge(prometheus.GaugeOpts{ |
| 26 | + Name: "opbnb_bridge_bot_unproven_withdrawals", |
| 27 | + Help: "The number of unproven withdrawals", |
| 28 | + }) |
| 29 | + UnfinalizedWithdrawals = promauto.NewGauge(prometheus.GaugeOpts{ |
| 30 | + Name: "opbnb_bridge_bot_unfinalized_withdrawals", |
| 31 | + Help: "The number of unfinalized withdrawals", |
| 32 | + }) |
| 33 | + EarliestUnProvenWithdrawalBlockNumber = promauto.NewGauge(prometheus.GaugeOpts{ |
| 34 | + Name: "opbnb_bridge_bot_earliest_unproven_withdrawal_block_number", |
| 35 | + Help: "The earliest block number of unproven withdrawals", |
| 36 | + }) |
| 37 | + EarliestUnfinalizedWithdrawalBlockNumber = promauto.NewGauge(prometheus.GaugeOpts{ |
| 38 | + Name: "opbnb_bridge_bot_earliest_unfinalized_withdrawal_block_number", |
| 39 | + Help: "The earliest block number of unfinalized withdrawals", |
| 40 | + }) |
| 41 | + |
| 42 | + FailedWithdrawals = promauto.NewGauge(prometheus.GaugeOpts{ |
| 43 | + Name: "opbnb_bridge_bot_failed_withdrawals", |
| 44 | + Help: "The number of failed withdrawals", |
| 45 | + }) |
| 46 | +) |
| 47 | + |
| 48 | +func StartMetrics(ctx context.Context, cfg *Config, l1Client *ethclient.Client, db *gorm.DB, logger log.Logger) { |
| 49 | + ticker := time.NewTicker(5 * time.Second) |
| 50 | + for range ticker.C { |
| 51 | + _, signerAddress, _ := cfg.SignerKeyPair() |
| 52 | + balance, err := l1Client.BalanceAt(ctx, *signerAddress, nil) |
| 53 | + if err != nil { |
| 54 | + logger.Error("failed to get signer balance", "error", err) |
| 55 | + } |
| 56 | + TxSignerBalance.Set(float64(balance.Int64())) |
| 57 | + |
| 58 | + var scannedBlock L2ScannedBlock |
| 59 | + result := db.Last(&scannedBlock) |
| 60 | + if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { |
| 61 | + logger.Error("failed to query scanned block", "error", result.Error) |
| 62 | + } |
| 63 | + ScannedBlockNumber.Set(float64(scannedBlock.Number)) |
| 64 | + |
| 65 | + var unprovenCnt int64 |
| 66 | + result = db.Table("bot_delegated_withdrawals").Where("proven_time IS NULL AND failure_reason IS NULL").Count(&unprovenCnt) |
| 67 | + if result.Error != nil { |
| 68 | + logger.Error("failed to count withdrawals", "error", result.Error) |
| 69 | + } |
| 70 | + UnprovenWithdrawals.Set(float64(unprovenCnt)) |
| 71 | + |
| 72 | + var unfinalizedCnt int64 |
| 73 | + result = db.Table("bot_delegated_withdrawals").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").Count(&unfinalizedCnt) |
| 74 | + if result.Error != nil { |
| 75 | + logger.Error("failed to count withdrawals", "error", result.Error) |
| 76 | + } |
| 77 | + UnfinalizedWithdrawals.Set(float64(unfinalizedCnt)) |
| 78 | + |
| 79 | + var failedCnt int64 |
| 80 | + result = db.Table("bot_delegated_withdrawals").Where("failure_reason IS NOT NULL").Count(&failedCnt) |
| 81 | + if result.Error != nil { |
| 82 | + logger.Error("failed to count withdrawals", "error", result.Error) |
| 83 | + } |
| 84 | + FailedWithdrawals.Set(float64(failedCnt)) |
| 85 | + |
| 86 | + firstUnproven := BotDelegatedWithdrawal{} |
| 87 | + result = db.Table("bot_delegated_withdrawals").Order("id asc").Where("proven_time IS NULL AND failure_reason IS NULL").First(&firstUnproven) |
| 88 | + if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { |
| 89 | + logger.Error("failed to query withdrawals", "error", result.Error) |
| 90 | + } |
| 91 | + EarliestUnProvenWithdrawalBlockNumber.Set(float64(firstUnproven.InitiatedBlockNumber)) |
| 92 | + |
| 93 | + firstUnfinalized := BotDelegatedWithdrawal{} |
| 94 | + result = db.Table("bot_delegated_withdrawals").Order("id asc").Where("finalized_time IS NULL AND proven_time IS NOT NULL AND failure_reason IS NULL").First(&firstUnfinalized) |
| 95 | + if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { |
| 96 | + logger.Error("failed to query withdrawals", "error", result.Error) |
| 97 | + } |
| 98 | + EarliestUnfinalizedWithdrawalBlockNumber.Set(float64(firstUnfinalized.InitiatedBlockNumber)) |
| 99 | + } |
| 100 | +} |
0 commit comments