Skip to content

Commit

Permalink
op-batcher: Add metrics for pending L2 transaction data size
Browse files Browse the repository at this point in the history
  • Loading branch information
trianglesphere committed May 31, 2023
1 parent df11f76 commit 6872908
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions op-batcher/batcher/channel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (s *channelManager) processBlocks() error {
}
blocksAdded += 1
latestL2ref = l2BlockRefFromBlockAndL1Info(block, l1info)
s.metr.RecordL2BlockInChannel(block)
// current block got added but channel is now full
if s.pendingChannel.IsFull() {
break
Expand Down Expand Up @@ -341,6 +342,8 @@ func (s *channelManager) AddL2Block(block *types.Block) error {
if s.tip != (common.Hash{}) && s.tip != block.ParentHash() {
return ErrReorg
}

s.metr.RecordL2BlockInPendingQueue(block)
s.blocks = append(s.blocks, block)
s.tip = block.Hash()

Expand Down
45 changes: 43 additions & 2 deletions op-batcher/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -30,6 +31,8 @@ type Metricer interface {
RecordL2BlocksLoaded(l2ref eth.L2BlockRef)
RecordChannelOpened(id derive.ChannelID, numPendingBlocks int)
RecordL2BlocksAdded(l2ref eth.L2BlockRef, numBlocksAdded, numPendingBlocks, inputBytes, outputComprBytes int)
RecordL2BlockInPendingQueue(block *types.Block)
RecordL2BlockInChannel(block *types.Block)
RecordChannelClosed(id derive.ChannelID, numPendingBlocks int, numFrames int, inputBytes int, outputComprBytes int, reason error)
RecordChannelFullySubmitted(id derive.ChannelID)
RecordChannelTimedOut(id derive.ChannelID)
Expand All @@ -55,8 +58,10 @@ type Metrics struct {
// label by openend, closed, fully_submitted, timed_out
channelEvs opmetrics.EventVec

pendingBlocksCount prometheus.GaugeVec
blocksAddedCount prometheus.Gauge
pendingBlocksCount prometheus.GaugeVec
pendingBlocksBytesTotal prometheus.Counter
pendingBlocksBytesCurrent prometheus.Gauge
blocksAddedCount prometheus.Gauge

channelInputBytes prometheus.GaugeVec
channelReadyBytes prometheus.Gauge
Expand Down Expand Up @@ -109,6 +114,16 @@ func NewMetrics(procName string) *Metrics {
Name: "pending_blocks_count",
Help: "Number of pending blocks, not added to a channel yet.",
}, []string{"stage"}),
pendingBlocksBytesTotal: factory.NewCounter(prometheus.CounterOpts{
Namespace: ns,
Name: "pending_blocks_bytes_total",
Help: "Total size of transactions in pending blocks as they are fetched from L2",
}),
pendingBlocksBytesCurrent: factory.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "pending_blocks_bytes_current",
Help: "Current size of transactions in the pending (fetched from L2 but not in a channel) stage.",
}),
blocksAddedCount: factory.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "blocks_added_count",
Expand Down Expand Up @@ -243,6 +258,18 @@ func (m *Metrics) RecordChannelClosed(id derive.ChannelID, numPendingBlocks int,
m.channelClosedReason.Set(float64(ClosedReasonToNum(reason)))
}

func (m *Metrics) RecordL2BlockInPendingQueue(block *types.Block) {
size := float64(estimateBatchSize(block))
m.pendingBlocksBytesTotal.Add(size)
m.pendingBlocksBytesCurrent.Add(size)
}

func (m *Metrics) RecordL2BlockInChannel(block *types.Block) {
size := float64(estimateBatchSize(block))
m.pendingBlocksBytesCurrent.Add(-1 * size)
// Refer to RecordL2BlocksAdded to see the current + count of bytes added to a channel
}

func ClosedReasonToNum(reason error) int {
// CLI-3640
return 0
Expand All @@ -267,3 +294,17 @@ func (m *Metrics) RecordBatchTxSuccess() {
func (m *Metrics) RecordBatchTxFailed() {
m.batcherTxEvs.Record(TxStageFailed)
}

// estimateBatchSize estimates the size of the batch
func estimateBatchSize(block *types.Block) uint64 {
size := uint64(70) // estimated overhead of batch metadata
for _, tx := range block.Transactions() {
// Don't include deposit transactions in the batch.
if tx.IsDepositTx() {
continue
}
// Add 2 for the overhead of encoding the tx bytes in a RLP list
size += tx.Size() + 2
}
return size
}
3 changes: 3 additions & 0 deletions op-batcher/metrics/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
txmetrics "github.com/ethereum-optimism/optimism/op-service/txmgr/metrics"
"github.com/ethereum/go-ethereum/core/types"
)

type noopMetrics struct {
Expand All @@ -23,6 +24,8 @@ func (*noopMetrics) RecordLatestL1Block(l1ref eth.L1BlockRef) {}
func (*noopMetrics) RecordL2BlocksLoaded(eth.L2BlockRef) {}
func (*noopMetrics) RecordChannelOpened(derive.ChannelID, int) {}
func (*noopMetrics) RecordL2BlocksAdded(eth.L2BlockRef, int, int, int, int) {}
func (*noopMetrics) RecordL2BlockInPendingQueue(*types.Block) {}
func (*noopMetrics) RecordL2BlockInChannel(*types.Block) {}

func (*noopMetrics) RecordChannelClosed(derive.ChannelID, int, int, int, int, error) {}

Expand Down

0 comments on commit 6872908

Please sign in to comment.