Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(metrics): add more transaction sender metrics (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha authored Mar 12, 2024
1 parent 20a221b commit 26ed379
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ var (
ProverSubmissionErrorCounter = metrics.NewRegisteredCounter("prover/proof/submission/error", nil)
ProverSgxProofGeneratedCounter = metrics.NewRegisteredCounter("prover/proof/sgx/generated", nil)
ProverPseProofGeneratedCounter = metrics.NewRegisteredCounter("prover/proof/pse/generated", nil)

// Transaction sender
TxSenderSentCounter = metrics.NewRegisteredCounter("sender/sent/txs", nil)
TxSenderConfirmedSuccessfulCounter = metrics.NewRegisteredCounter("sender/confirmed/successful/txs", nil)
TxSenderConfirmedFailedCounter = metrics.NewRegisteredCounter("sender/confirmed/failed/txs", nil)
TxSenderUnconfirmedCounter = metrics.NewRegisteredCounter("sender/unconfirmed/txs", nil)
TxSenderGasPriceGauge = metrics.NewRegisteredGauge("sender/gasPrice", nil)
TxSenderBlobGasPriceGauge = metrics.NewRegisteredGauge("sender/blob/gasPrice", nil)
TxSenderTxIncludedTimeGauge = metrics.NewRegisteredGauge("sender/tx/includedTime", nil)
)

// Serve starts the metrics server on the given address, will be closed when the given
Expand Down
23 changes: 21 additions & 2 deletions pkg/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/pborman/uuid"

"github.com/taikoxyz/taiko-client/internal/metrics"
"github.com/taikoxyz/taiko-client/pkg/rpc"
)

Expand Down Expand Up @@ -59,6 +60,7 @@ type TxToConfirm struct {
Retrys uint64
CurrentTx *types.Transaction
Receipt *types.Receipt
CreatedAt time.Time

Err error
}
Expand Down Expand Up @@ -306,9 +308,10 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
s.mu.Lock()
defer s.mu.Unlock()

// Set the transaction ID
// Set the transaction ID and its creation time
if tx.ID == "" {
tx.ID = uuid.New()
tx.CreatedAt = time.Now()
}

originalTx := tx.originalTx
Expand Down Expand Up @@ -371,6 +374,8 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
)
return err
}

metrics.TxSenderSentCounter.Inc(1)
break
}
s.nonce++
Expand Down Expand Up @@ -427,6 +432,7 @@ func (s *Sender) resendUnconfirmedTxs() {
continue
}
if err := s.send(unconfirmedTx, true); err != nil {
metrics.TxSenderUnconfirmedCounter.Inc(1)
log.Warn(
"Failed to resend the transaction",
"txId", id,
Expand All @@ -449,7 +455,8 @@ func (s *Sender) checkPendingTransactionsConfirmation() {
// Ignore the transaction if it is pending.
tx, isPending, err := s.client.TransactionByHash(s.ctx, pendingTx.CurrentTx.Hash())
if err != nil {
log.Warn("Failed to fetch transaction",
log.Warn(
"Failed to fetch transaction",
"txId", pendingTx.ID,
"nonce", pendingTx.CurrentTx.Nonce(),
"hash", pendingTx.CurrentTx.Hash(),
Expand All @@ -474,15 +481,27 @@ func (s *Sender) checkPendingTransactionsConfirmation() {
log.Warn("Failed to get the transaction receipt", "hash", pendingTx.CurrentTx.Hash(), "err", err)
continue
}

// Record the gas fee metrics.
if receipt.BlobGasUsed == 0 {
metrics.TxSenderGasPriceGauge.Update(receipt.EffectiveGasPrice.Int64())
} else {
metrics.TxSenderBlobGasPriceGauge.Update(receipt.BlobGasPrice.Int64())
}

metrics.TxSenderTxIncludedTimeGauge.Update(int64(time.Since(pendingTx.CreatedAt).Seconds()))

pendingTx.Receipt = receipt
if receipt.Status != types.ReceiptStatusSuccessful {
pendingTx.Err = fmt.Errorf("transaction status is failed, hash: %s", receipt.TxHash)
metrics.TxSenderConfirmedFailedCounter.Inc(1)
s.releaseUnconfirmedTx(id)
continue
}
}
pendingTx.confirmations = s.head.Number.Uint64() - pendingTx.Receipt.BlockNumber.Uint64()
if pendingTx.confirmations >= s.ConfirmationDepth {
metrics.TxSenderConfirmedSuccessfulCounter.Inc(1)
s.releaseUnconfirmedTx(id)
}
}
Expand Down

0 comments on commit 26ed379

Please sign in to comment.