Skip to content

Commit

Permalink
add metric to track insufficient balance errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhfang committed Nov 16, 2024
1 parent c5f6435 commit 374f59d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
5 changes: 5 additions & 0 deletions hyperlane/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"encoding/hex"
"errors"
"fmt"
<<<<<<< HEAD

Check failure on line 7 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)

Check failure on line 7 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / build

missing import path

Check failure on line 7 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / test

missing import path
"github.com/skip-mev/go-fast-solver/shared/metrics"
"math/big"
=======

Check failure on line 10 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)
dbtypes "github.com/skip-mev/go-fast-solver/db"
>>>>>>> 42dca9e (add metric to track insufficient balance errors)

Check failure on line 12 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)

"strings"

Check failure on line 14 in hyperlane/relayer.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found "strings" (typecheck)

Expand Down Expand Up @@ -150,6 +154,7 @@ func (r *relayer) Relay(ctx context.Context, originChainID string, initiateTxHas
if err != nil {
return "", "", fmt.Errorf("processing message on domain %s: %w", dispatch.DestinationDomain, err)
}
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, destinationChainID, dbtypes.TxTypeHyperlaneMessageDelivery)

destinationChainID, err = config.GetConfigReader(ctx).GetChainIDByHyperlaneDomain(dispatch.DestinationDomain)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (r *orderFulfillmentHandler) FillOrder(
}

txHash, rawTx, _, err := destinationChainBridgeClient.FillOrder(ctx, order, destinationChainGatewayContractAddress)
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, order.SourceChainID, order.DestinationChainID)
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, order.DestinationChainID, dbtypes.TxTypeOrderFill)
if err != nil {
return "", fmt.Errorf("filling order on destination chain at address %s: %w", destinationChainGatewayContractAddress, err)
}
Expand Down Expand Up @@ -235,6 +235,10 @@ func (r *orderFulfillmentHandler) checkOrderAssetBalance(ctx context.Context, de
}
if balance.Cmp(new(big.Int).SetUint64(transferAmount)) < 0 {
lmt.Logger(ctx).Warn("insufficient balance", zap.String("balance", balance.String()), zap.Uint64("transferAmount", transferAmount))
metrics.FromContext(ctx).ObserveInsufficientBalanceError(
destinationChainConfig.ChainID,
new(big.Int).Sub(new(big.Int).SetUint64(transferAmount), balance).Uint64(),
)
return false, nil
}
return true, nil
Expand Down Expand Up @@ -429,7 +433,7 @@ func (r *orderFulfillmentHandler) InitiateTimeout(ctx context.Context, order db.
}

txHash, rawTx, _, err := destinationChainBridgeClient.InitiateTimeout(ctx, order, destinationChainGatewayContractAddress)
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, order.SourceChainID, order.DestinationChainID)
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, order.DestinationChainID, dbtypes.TxTypeInitiateTimeout)
if err != nil {
return "", fmt.Errorf("initiating timeout: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions ordersettler/ordersettler.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,7 @@ func (r *OrderSettler) SettleBatch(ctx context.Context, batch types.SettlementBa
return "", fmt.Errorf("getting destination bridge client: %w", err)
}
txHash, rawTx, err := destinationBridgeClient.InitiateBatchSettlement(ctx, batch)
metrics.FromContext(ctx).IncTransactionSubmitted(
err == nil,
batch.SourceChainID(),
batch.DestinationChainID(),
)
metrics.FromContext(ctx).IncTransactionSubmitted(err == nil, batch.DestinationChainID(), dbtypes.TxTypeSettlement)
if err != nil {
return "", fmt.Errorf("initiating batch settlement on chain %s: %w", batch.DestinationChainID(), err)
}
Expand Down
42 changes: 29 additions & 13 deletions shared/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const (
orderStatusLabel = "order_status"
transferStatusLabel = "transfer_status"
settlementStatusLabel = "settlement_status"
operationLabel = "operation"
transactionTypeLabel = "transaction_type"
)

type Metrics interface {
IncTransactionSubmitted(success bool, sourceChainID, destinationChainID string)
IncTransactionSubmitted(success bool, chainID, transactionType string)
IncTransactionVerified(success bool, chainID string)

IncFillOrders(sourceChainID, destinationChainID, orderStatus string)
Expand All @@ -43,6 +43,7 @@ type Metrics interface {

ObserveTransferSizeOutOfRange(sourceChainID, destinationChainID string, amountExceededBy int64)
ObserveFeeBpsRejection(sourceChainID, destinationChainID string, feeBpsExceededBy int64)
ObserveInsufficientBalanceError(chainID string, amountInsufficientBy uint64)
}

type metricsContextKey struct{}
Expand Down Expand Up @@ -78,10 +79,9 @@ type PromMetrics struct {
hplCheckpointingErrors metrics.Counter
hplLatency metrics.Histogram

transferSizeOutOfRange metrics.Histogram
feeBpsRejections metrics.Histogram

databaseErrors metrics.Counter
transferSizeOutOfRange metrics.Histogram
feeBpsRejections metrics.Histogram
insufficientBalanceErrors metrics.Histogram
}

func NewPromMetrics() Metrics {
Expand Down Expand Up @@ -162,16 +162,23 @@ func NewPromMetrics() Metrics {
Help: "histogram of fee bps that were rejected for being too low",
Buckets: []float64{1, 5, 10, 25, 50, 100, 200, 500, 1000},
}, []string{sourceChainIDLabel, destinationChainIDLabel}),
databaseErrors: prom.NewCounterFrom(stdprom.CounterOpts{
insufficientBalanceErrors: prom.NewHistogramFrom(stdprom.HistogramOpts{
Namespace: "solver",
Name: "database_errors_total",
Help: "number of errors encountered when making database calls",
}, []string{}),
Name: "insufficient_balance_errors",
Help: "histogram of fill orders that exceeded available balance",
Buckets: []float64{
100000000, // 100 USDC
1000000000, // 1,000 USDC
10000000000, // 10,000 USDC
100000000000, // 100,000 USDC
1000000000000, // 1,000,000 USDC
},
}, []string{chainIDLabel}),
}
}

func (m *PromMetrics) IncTransactionSubmitted(success bool, sourceChainID, destinationChainID string) {
m.totalTransactionSubmitted.With(successLabel, fmt.Sprint(success), sourceChainIDLabel, sourceChainID, destinationChainIDLabel, destinationChainID).Add(1)
func (m *PromMetrics) IncTransactionSubmitted(success bool, chainID, transactionType string) {
m.totalTransactionSubmitted.With(successLabel, fmt.Sprint(success), chainIDLabel, chainID, transactionTypeLabel, transactionType).Add(1)
}

func (m *PromMetrics) IncTransactionVerified(success bool, chainID string) {
Expand Down Expand Up @@ -238,9 +245,18 @@ func (m *PromMetrics) ObserveFeeBpsRejection(sourceChainID, destinationChainID s
).Observe(float64(feeBps))
}

func (m *PromMetrics) ObserveInsufficientBalanceError(chainID string, amountInsufficientBy uint64) {
m.amountTransferSizeExceeded.With(

Check failure on line 249 in shared/metrics/metrics.go

View workflow job for this annotation

GitHub Actions / lint

m.amountTransferSizeExceeded undefined (type *PromMetrics has no field or method amountTransferSizeExceeded) (typecheck)

Check failure on line 249 in shared/metrics/metrics.go

View workflow job for this annotation

GitHub Actions / lint

m.amountTransferSizeExceeded undefined (type *PromMetrics has no field or method amountTransferSizeExceeded)) (typecheck)

Check failure on line 249 in shared/metrics/metrics.go

View workflow job for this annotation

GitHub Actions / lint

m.amountTransferSizeExceeded undefined (type *PromMetrics has no field or method amountTransferSizeExceeded)) (typecheck)
chainIDLabel, chainID,
).Observe(float64(amountInsufficientBy))
}

type NoOpMetrics struct{}

func (n NoOpMetrics) IncTransactionSubmitted(success bool, sourceChainID, destinationChainID string) {
func (n NoOpMetrics) ObserveInsufficientBalanceError(chainID string, amountInsufficientBy uint64) {
}

func (n NoOpMetrics) IncTransactionSubmitted(success bool, chainID, transactionType string) {
}
func (n NoOpMetrics) IncTransactionVerified(success bool, chainID string) {
}
Expand Down

0 comments on commit 374f59d

Please sign in to comment.