diff --git a/sync/metrics.go b/sync/metrics.go index e2d75024..a0916720 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -30,6 +30,7 @@ type metrics struct { syncStartedTs time.Time requestRangeTimeHist metric.Float64Histogram + requestRangeStartTs time.Time blockTime metric.Float64Histogram prevHeader time.Time @@ -165,9 +166,9 @@ func (m *metrics) subjectiveInitialization(ctx context.Context) { }) } -func (m *metrics) getRangeRequestTime(ctx context.Context, duration time.Duration, amount int, failed bool) { +func (m *metrics) updateGetRangeRequestInfo(ctx context.Context, amount int, failed bool) { m.observe(ctx, func(ctx context.Context) { - m.requestRangeTimeHist.Record(ctx, duration.Seconds(), + m.requestRangeTimeHist.Record(ctx, time.Since(m.requestRangeStartTs).Seconds(), metric.WithAttributes( attribute.Int("headers amount", amount), attribute.Bool("request failed", failed), @@ -185,6 +186,20 @@ func (m *metrics) newSubjectiveHead(ctx context.Context, height uint64, timestam }) } +func (m *metrics) rangeRequestStart() { + if m == nil { + return + } + m.requestRangeStartTs = time.Now() +} + +func (m *metrics) rangeRequestStop() { + if m == nil { + return + } + m.requestRangeStartTs = time.Time{} +} + func (m *metrics) observe(ctx context.Context, observeFn func(context.Context)) { if m == nil { return diff --git a/sync/sync.go b/sync/sync.go index ffd0d5f9..bfb3bf6f 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -316,10 +316,11 @@ func (s *Syncer[H]) requestHeaders( size = amount } - now := time.Now() to := fromHead.Height() + size + 1 + s.metrics.rangeRequestStart() headers, err := s.getter.GetRangeByHeight(ctx, fromHead, to) - s.metrics.getRangeRequestTime(s.ctx, time.Since(now), int(size)/100, err != nil) + s.metrics.updateGetRangeRequestInfo(s.ctx, int(size)/100, err != nil) + s.metrics.rangeRequestStop() if err != nil { return err }