Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sync): networkHead callback metric #131

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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))
}
3 changes: 3 additions & 0 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down