diff --git a/sync/metrics.go b/sync/metrics.go index 1f2c99f7..d722ae90 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -11,6 +11,9 @@ import ( var meter = otel.Meter("header/sync") type metrics struct { + networkHead atomic.Int64 + networkHeadGauge metric.Int64ObservableGauge + totalSynced atomic.Int64 totalSyncedGauge metric.Float64ObservableGauge } @@ -23,16 +26,33 @@ func newMetrics() (*metrics, error) { if err != nil { return nil, err } + netHead, err := meter.Int64ObservableGauge( + "network_head_gauge", + metric.WithDescription("network head"), + ) + if err != nil { + return nil, err + } m := &metrics{ totalSyncedGauge: totalSynced, + networkHeadGauge: netHead, } - callback := func(ctx context.Context, observer metric.Observer) error { + totalSyncedCallback := func(ctx context.Context, observer metric.Observer) error { observer.ObserveFloat64(totalSynced, float64(m.totalSynced.Load())) return nil } - _, err = meter.RegisterCallback(callback, totalSynced) + _, err = meter.RegisterCallback(totalSyncedCallback, totalSynced) + if err != nil { + return nil, err + } + + netHeadCallback := func(ctx context.Context, observer metric.Observer) error { + observer.ObserveInt64(netHead, m.networkHead.Load()) + return nil + } + _, err = meter.RegisterCallback(netHeadCallback, netHead) if err != nil { return nil, err } @@ -48,3 +68,11 @@ func (m *metrics) recordTotalSynced(totalSynced int) { m.totalSynced.Add(int64(totalSynced)) } + +func (m *metrics) recordNetworkHead(netHead uint64) { + if m == nil { + return + } + + m.networkHead.Store(int64(netHead)) +} diff --git a/sync/sync_head.go b/sync/sync_head.go index 66a497f6..76bc7308 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -121,11 +121,14 @@ func (s *Syncer[H]) setSubjectiveHead(ctx context.Context, netHead H) { "err", err) } + defer s.metrics.recordNetworkHead(netHead.Height()) + storeHead, err := s.store.Head(ctx) if err == nil && storeHead.Height() >= netHead.Height() { // we already synced it up - do nothing return } + // and if valid, set it as new subjective head s.pending.Add(netHead) s.wantSync()