Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mempool metrics for number of pending tx and expired txs #189

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -215,6 +215,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
}
Loading