diff --git a/sync/metrics.go b/sync/metrics.go index 42317e26..844833a0 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -12,8 +12,6 @@ import ( var meter = otel.Meter("header/sync") type metrics struct { - ctx context.Context - totalSynced atomic.Int64 totalSyncedGauge metric.Float64ObservableGauge @@ -21,16 +19,16 @@ type metrics struct { trustedPeersOutOfSync metric.Int64Counter laggingHeadersStart metric.Int64Counter - subjectiveHead atomic.Int64 - headerTimestamp metric.Float64Histogram + subjectiveHead atomic.Int64 + blockTime metric.Float64Histogram headerReceived time.Time prevHeader time.Time - headersThreshHold time.Duration + headersThreshold time.Duration } -func newMetrics(headersThreshHold time.Duration) (*metrics, error) { +func newMetrics(headersThreshold time.Duration) (*metrics, error) { totalSynced, err := meter.Float64ObservableGauge( "total_synced_headers", metric.WithDescription("total synced headers"), @@ -39,40 +37,53 @@ func newMetrics(headersThreshHold time.Duration) (*metrics, error) { return nil, err } - syncLoopStarted, err := meter.Int64Counter("sync_loop_started", metric.WithDescription("sync loop started")) + syncLoopStarted, err := meter.Int64Counter( + "sync_loop_started", + metric.WithDescription("sync loop started"), + ) if err != nil { return nil, err } - trustedPeersOutOfSync, err := meter.Int64Counter("tr_peers_out_of_sync", metric.WithDescription("trusted peers out of sync")) + trustedPeersOutOfSync, err := meter.Int64Counter( + "tr_peers_out_of_sync", + metric.WithDescription("trusted peers out of sync"), + ) if err != nil { return nil, err } - laggingHeadersStart, err := meter.Int64Counter("sync_lagging_hdr_start", metric.WithDescription("lagging header start")) + laggingHeadersStart, err := meter.Int64Counter( + "sync_lagging_hdr_start", + metric.WithDescription("lagging header start"), + ) if err != nil { return nil, err } - subjectiveHead, err := meter.Int64ObservableGauge("sync_subjective_head", metric.WithDescription("subjective head height")) + subjectiveHead, err := meter.Int64ObservableGauge( + "sync_subjective_head", + metric.WithDescription("subjective head height"), + ) if err != nil { return nil, err } - headerTimestamp, err := meter.Float64Histogram("sync_subjective_head_ts", - metric.WithDescription("subjective_head_timestamp")) + blockTime, err := meter.Float64Histogram( + "sync_actual_blockTime_ts", + metric.WithDescription("subjective_head_timestamp"), + ) if err != nil { return nil, err } m := &metrics{ - ctx: context.Background(), totalSyncedGauge: totalSynced, syncLoopStarted: syncLoopStarted, trustedPeersOutOfSync: trustedPeersOutOfSync, laggingHeadersStart: laggingHeadersStart, - headerTimestamp: headerTimestamp, - headersThreshHold: headersThreshHold, + blockTime: blockTime, + headersThreshold: headersThreshold, } callback := func(ctx context.Context, observer metric.Observer) error { @@ -99,39 +110,30 @@ func (m *metrics) recordSyncLoopStarted() { if m == nil { return } - m.syncLoopStarted.Add(m.ctx, 1) + m.syncLoopStarted.Add(context.Background(), 1) } func (m *metrics) recordTrustedPeersOutOfSync() { if m == nil { return } - m.trustedPeersOutOfSync.Add(m.ctx, 1) + m.trustedPeersOutOfSync.Add(context.Background(), 1) } -func (m *metrics) observeNewHead(height int64) { +func (m *metrics) observeNewSubjectiveHead(height int64, timestamp time.Time) { if m == nil { return } m.subjectiveHead.Store(height) -} - -func (m *metrics) observeLaggingHeader() { - if m == nil { - return - } - if time.Since(m.headerReceived) > m.headersThreshHold { - m.laggingHeadersStart.Add(m.ctx, 1) - } - m.headerReceived = time.Now() -} -func (m *metrics) observeHeaderTimestamp(timestamp time.Time) { - if m == nil { - return - } + ctx := context.Background() if !m.prevHeader.IsZero() { - m.headerTimestamp.Record(m.ctx, timestamp.Sub(m.prevHeader).Seconds()) + m.blockTime.Record(ctx, timestamp.Sub(m.prevHeader).Seconds()) } m.prevHeader = timestamp + + if time.Since(m.headerReceived) > m.headersThreshold { + m.laggingHeadersStart.Add(ctx, 1) + } + m.headerReceived = time.Now() } diff --git a/sync/sync_head.go b/sync/sync_head.go index da8d6b16..d6af45a7 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -131,9 +131,7 @@ func (s *Syncer[H]) setSubjectiveHead(ctx context.Context, netHead H) { s.pending.Add(netHead) s.wantSync() log.Infow("new network head", "height", netHead.Height(), "hash", netHead.Hash()) - s.metrics.observeNewHead(int64(netHead.Height())) - s.metrics.observeHeaderTimestamp(netHead.Time()) - s.metrics.observeLaggingHeader() + s.metrics.observeNewSubjectiveHead(int64(netHead.Height()), netHead.Time()) } // incomingNetworkHead processes new potential network headers.