Skip to content

Commit 788fdf0

Browse files
authored
feat: initialize metrics on startup (#1812)
1 parent 79c1f8c commit 788fdf0

7 files changed

Lines changed: 99 additions & 3 deletions

File tree

common/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"runtime/debug"
66
)
77

8-
var tag = "v4.7.15"
8+
var tag = "v4.7.16"
99

1010
var commit = func() string {
1111
if info, ok := debug.ReadBuildInfo(); ok {

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
194194
return nil, fmt.Errorf("failed to initialize and commit genesis batch, err: %v", err)
195195
}
196196
layer2Relayer.metrics = initL2RelayerMetrics(reg)
197+
layer2Relayer.initializeMetrics(ctx)
197198

198199
switch serviceType {
199200
case ServiceTypeL2RollupRelayer:
@@ -205,6 +206,28 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
205206
return layer2Relayer, nil
206207
}
207208

209+
// initializeMetrics seeds the commit block height gauge from DB so that a restart
210+
// does not leave it at 0, which would otherwise make the commit-lag alert fire
211+
// until the next batch is committed.
212+
func (r *Layer2Relayer) initializeMetrics(ctx context.Context) {
213+
latestBatch, err := r.batchOrm.GetLatestCommittedBatch(ctx)
214+
if err != nil {
215+
log.Warn("failed to initialize commit block height metric", "err", err)
216+
return
217+
}
218+
if latestBatch == nil {
219+
return
220+
}
221+
endChunk, err := r.chunkOrm.GetChunkByIndex(ctx, latestBatch.EndChunkIndex)
222+
if err != nil {
223+
log.Warn("failed to initialize commit block height metric", "err", err)
224+
return
225+
}
226+
if endChunk != nil {
227+
r.metrics.rollupL2RelayerCommitBlockHeight.Set(float64(endChunk.EndBlockNumber))
228+
}
229+
}
230+
208231
func (r *Layer2Relayer) initializeGenesis() error {
209232
if count, err := r.batchOrm.GetBatchCount(r.ctx); err != nil {
210233
return fmt.Errorf("failed to get batch count: %v", err)

rollup/internal/controller/watcher/batch_proposer.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package watcher
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

@@ -119,9 +120,33 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, minC
119120
}),
120121
}
121122

123+
p.initializeMetrics(ctx)
124+
122125
return p
123126
}
124127

128+
// initializeMetrics seeds the propose block height gauge from DB so that a restart
129+
// does not leave it at 0, which would otherwise make the propose-lag alert fire
130+
// until the next batch is proposed.
131+
func (p *BatchProposer) initializeMetrics(ctx context.Context) {
132+
latestBatch, err := p.batchOrm.GetLatestBatch(ctx)
133+
if err != nil {
134+
if errors.Is(err, gorm.ErrRecordNotFound) {
135+
return // no batches proposed yet, nothing to seed
136+
}
137+
log.Warn("failed to initialize batch propose block height metric", "err", err)
138+
return
139+
}
140+
endChunk, err := p.chunkOrm.GetChunkByIndex(ctx, latestBatch.EndChunkIndex)
141+
if err != nil {
142+
log.Warn("failed to initialize batch propose block height metric", "err", err)
143+
return
144+
}
145+
if endChunk != nil {
146+
p.batchProposeBlockHeight.Set(float64(endChunk.EndBlockNumber))
147+
}
148+
}
149+
125150
// SetReplayDB sets the replay database for the BatchProposer.
126151
// This is used for the proposer tool only, to change the l2_block data source.
127152
// This function is not thread-safe and should be called after initializing the BatchProposer and before starting to propose chunks.

rollup/internal/controller/watcher/chunk_proposer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,25 @@ func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, minC
127127
}),
128128
}
129129

130+
p.initializeMetrics(ctx)
131+
130132
return p
131133
}
132134

135+
// initializeMetrics seeds the propose block height gauge from DB so that a restart
136+
// does not leave it at 0, which would otherwise make the propose-lag alert fire
137+
// until the next chunk is proposed.
138+
func (p *ChunkProposer) initializeMetrics(ctx context.Context) {
139+
latestChunk, err := p.chunkOrm.GetLatestChunk(ctx)
140+
if err != nil {
141+
log.Warn("failed to initialize chunk propose block height metric", "err", err)
142+
return
143+
}
144+
if latestChunk != nil {
145+
p.chunkProposeBlockHeight.Set(float64(latestChunk.EndBlockNumber))
146+
}
147+
}
148+
133149
// SetReplayDB sets the replay database for the ChunkProposer.
134150
// This is used for the proposer tool only, to change the l2_block data source.
135151
// This function is not thread-safe and should be called after initializing the ChunkProposer and before starting to propose chunks.

rollup/internal/controller/watcher/l1_watcher.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewL1WatcherClient(ctx context.Context, rpcClient *rpc.Client, startHeight
4444
savedL1BlockHeight = startHeight
4545
}
4646

47-
return &L1WatcherClient{
47+
w := &L1WatcherClient{
4848
ctx: ctx,
4949
rpcClient: rpcClient,
5050
client: ethclient.NewClient(rpcClient),
@@ -53,6 +53,11 @@ func NewL1WatcherClient(ctx context.Context, rpcClient *rpc.Client, startHeight
5353
processedBlockHeight: savedL1BlockHeight,
5454
metrics: initL1WatcherMetrics(reg),
5555
}
56+
57+
// Seed the gauge from the resumed height instead of starting from 0.
58+
w.metrics.l1WatcherFetchBlockHeaderProcessedBlockHeight.Set(float64(w.processedBlockHeight))
59+
60+
return w
5661
}
5762

5863
// ProcessedBlockHeight get processedBlockHeight

rollup/internal/controller/watcher/l2_watcher.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type L2WatcherClient struct {
4343

4444
// NewL2WatcherClient take a l2geth instance to generate a l2watcherclient instance
4545
func NewL2WatcherClient(ctx context.Context, client *rpc.Client, confirmations rpc.BlockNumber, messageQueueAddress common.Address, withdrawTrieRootSlot common.Hash, chainCfg *params.ChainConfig, db *gorm.DB, validiumMode bool, reg prometheus.Registerer) *L2WatcherClient {
46-
return &L2WatcherClient{
46+
w := &L2WatcherClient{
4747
ctx: ctx,
4848
Client: ethclient.NewClient(client),
4949
rpcCli: client,
@@ -61,6 +61,15 @@ func NewL2WatcherClient(ctx context.Context, client *rpc.Client, confirmations r
6161

6262
chainCfg: chainCfg,
6363
}
64+
65+
// Seed the gauge from the latest stored height instead of starting from 0.
66+
if latestHeight, err := w.l2BlockOrm.GetL2BlocksLatestHeight(ctx); err != nil {
67+
log.Warn("failed to initialize l2 watcher fetched height metric", "err", err)
68+
} else {
69+
w.metrics.fetchRunningMissingBlocksHeight.Set(float64(latestHeight))
70+
}
71+
72+
return w
6473
}
6574

6675
const blocksFetchLimit = uint64(10)

rollup/internal/orm/batch.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ func (o *Batch) GetLatestBatch(ctx context.Context) (*Batch, error) {
155155
return &latestBatch, nil
156156
}
157157

158+
// GetLatestCommittedBatch retrieves the highest-index batch that has a commit tx hash set.
159+
// It returns nil if no such batch exists.
160+
func (o *Batch) GetLatestCommittedBatch(ctx context.Context) (*Batch, error) {
161+
db := o.db.WithContext(ctx)
162+
db = db.Model(&Batch{})
163+
db = db.Where("commit_tx_hash IS NOT NULL AND commit_tx_hash != ''")
164+
db = db.Order("index desc")
165+
166+
var latestBatch Batch
167+
if err := db.First(&latestBatch).Error; err != nil {
168+
if errors.Is(err, gorm.ErrRecordNotFound) {
169+
return nil, nil
170+
}
171+
return nil, fmt.Errorf("Batch.GetLatestCommittedBatch error: %w", err)
172+
}
173+
return &latestBatch, nil
174+
}
175+
158176
// GetFirstUnbatchedChunkIndex retrieves the first unbatched chunk index.
159177
func (o *Batch) GetFirstUnbatchedChunkIndex(ctx context.Context) (uint64, error) {
160178
// Get the latest batch

0 commit comments

Comments
 (0)