Skip to content

Commit

Permalink
Add mempool metrics for number of pending tx and expired txs (#189)
Browse files Browse the repository at this point in the history
* Add metrics for mempool pending transaction size

* Add expired tx count metrics
  • Loading branch information
yzang2019 authored and udpatil committed Mar 26, 2024
1 parent 78db601 commit b7c4269
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func (txmp *TxMempool) Size() int {
return txSize + pendingSize
}

// PendingSize returns the number of pending transactions in the mempool.
func (txmp *TxMempool) PendingSize() int {
return txmp.pendingTxs.Size()
}

// SizeBytes return the total sum in bytes of all the valid transactions in the
// mempool. It is thread-safe.
func (txmp *TxMempool) SizeBytes() int64 {
Expand Down Expand Up @@ -292,6 +297,7 @@ func (txmp *TxMempool) CheckTx(
timestamp: time.Now().UTC(),
height: txmp.height,
expiredCallback: func(removeFromCache bool) {
txmp.metrics.ExpiredTxs.Add(1)
if removeFromCache {
txmp.cache.Remove(tx)
}
Expand Down Expand Up @@ -519,6 +525,7 @@ func (txmp *TxMempool) Update(
}

txmp.metrics.Size.Set(float64(txmp.Size()))
txmp.metrics.PendingSize.Set(float64(txmp.PendingSize()))
return nil
}

Expand Down Expand Up @@ -638,6 +645,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, res *abci.ResponseCheck

txmp.metrics.TxSizeBytes.Observe(float64(wtx.Size()))
txmp.metrics.Size.Set(float64(txmp.Size()))
txmp.metrics.PendingSize.Set(float64(txmp.PendingSize()))

txmp.insertTx(wtx)
txmp.logger.Debug(
Expand Down Expand Up @@ -745,6 +753,7 @@ func (txmp *TxMempool) handleRecheckResult(tx types.Tx, res *abci.ResponseCheckT
}

txmp.metrics.Size.Set(float64(txmp.Size()))
txmp.metrics.PendingSize.Set(float64(txmp.PendingSize()))
}

// updateReCheckTxs updates the recheck cursors using the gossipIndex. For
Expand Down
1 change: 1 addition & 0 deletions internal/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func TestTxMempool_Size(t *testing.T) {
txmp := setup(t, client, 0)
txs := checkTxs(ctx, t, txmp, 100, 0)
require.Equal(t, len(txs), txmp.Size())
require.Equal(t, 0, txmp.PendingSize())
require.Equal(t, int64(5690), txmp.SizeBytes())

rawTxs := make([]types.Tx, len(txs))
Expand Down
14 changes: 14 additions & 0 deletions internal/mempool/metrics.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/mempool/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Metrics struct {
// Number of uncommitted transactions in the mempool.
Size metrics.Gauge

// Number of pending transactions in mempool
PendingSize metrics.Gauge

// Histogram of transaction sizes in bytes.
TxSizeBytes metrics.Histogram `metrics_buckettype:"exp" metrics_bucketsizes:"1,3,7"`

Expand All @@ -38,6 +41,12 @@ type Metrics struct {
//metrics:Number of evicted transactions.
EvictedTxs metrics.Counter

// ExpiredTxs defines the number of expired transactions. These are valid
// transactions that passed CheckTx and existed in the mempool but were not
// get picked up in time and eventually got expired and removed from mempool
//metrics:Number of expired transactions.
ExpiredTxs metrics.Counter

// Number of times transactions are rechecked in the mempool.
RecheckTimes metrics.Counter
}

0 comments on commit b7c4269

Please sign in to comment.