Skip to content

Commit d1a2d3d

Browse files
committed
fix(sync/metrics): fix blockTime metric
1 parent 030380e commit d1a2d3d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

sync/metrics.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type metrics struct {
3939
requestRangeTimeHist metric.Float64Histogram
4040
requestRangeStartTS time.Time
4141

42-
blockTime metric.Float64Histogram
43-
prevHeader time.Time
42+
blockTime metric.Float64Histogram
43+
prevHeaderTs time.Time
4444
}
4545

4646
func newMetrics() (*metrics, error) {
@@ -213,12 +213,15 @@ func (m *metrics) updateGetRangeRequestInfo(ctx context.Context, amount uint64,
213213
})
214214
}
215215

216-
func (m *metrics) newSubjectiveHead(ctx context.Context, height uint64, timestamp time.Time) {
216+
func (m *metrics) newNetHead(ctx context.Context, height uint64, timestamp time.Time) {
217217
m.observe(ctx, func(ctx context.Context) {
218218
m.subjectiveHead.Store(height)
219219

220-
if !m.prevHeader.IsZero() {
221-
m.blockTime.Record(ctx, timestamp.Sub(m.prevHeader).Seconds())
220+
if timestamp.After(m.prevHeaderTs) {
221+
if !m.prevHeaderTs.IsZero() {
222+
m.blockTime.Record(ctx, timestamp.Sub(m.prevHeaderTs).Seconds())
223+
}
224+
m.prevHeaderTs = timestamp
222225
}
223226
})
224227
}

sync/syncer_head.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ func (s *Syncer[H]) setLocalHead(ctx context.Context, netHead H) {
201201
"hash", netHead.Hash().String(),
202202
"err", err)
203203
}
204-
s.metrics.newSubjectiveHead(s.ctx, netHead.Height(), netHead.Time())
205204

206205
storeHead, err := s.store.Head(ctx)
207206
if err == nil && storeHead.Height() >= netHead.Height() {
@@ -227,6 +226,7 @@ func (s *Syncer[H]) incomingNetworkHead(ctx context.Context, head H) error {
227226
}
228227

229228
s.setLocalHead(ctx, head)
229+
s.metrics.newNetHead(s.ctx, head.Height(), head.Time())
230230
return nil
231231
}
232232

0 commit comments

Comments
 (0)