Skip to content

Commit

Permalink
chore: add fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Dec 15, 2023
1 parent bd7bb1f commit 121111d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ type metrics struct {
headerTimestamp metric.Float64Histogram

headerReceived time.Time
prevHeader time.Time

headersThreshHold time.Duration
}

func newMetrics() (*metrics, error) {
func newMetrics(headersThreshHold time.Duration) (*metrics, error) {
totalSynced, err := meter.Float64ObservableGauge(
"total_synced_headers",
metric.WithDescription("total synced headers"),
Expand Down Expand Up @@ -69,6 +72,7 @@ func newMetrics() (*metrics, error) {
trustedPeersOutOfSync: trustedPeersOutOfSync,
laggingHeadersStart: laggingHeadersStart,
headerTimestamp: headerTimestamp,
headersThreshHold: headersThreshHold,
}

callback := func(ctx context.Context, observer metric.Observer) error {
Expand Down Expand Up @@ -112,20 +116,22 @@ func (m *metrics) observeNewHead(height int64) {
m.subjectiveHead.Store(height)
}

func (m *metrics) observeLaggingHeader(threshold time.Duration, receivedAt time.Time) {
func (m *metrics) observeLaggingHeader() {
if m == nil {
return
}
if !m.headerReceived.IsZero() &&
float64(receivedAt.Second()-m.headerReceived.Second()) > threshold.Seconds() {
if time.Since(m.headerReceived) > m.headersThreshHold {
m.laggingHeadersStart.Add(m.ctx, 1)
}
m.headerReceived = receivedAt
m.headerReceived = time.Now()
}

func (m *metrics) observeHeaderTimestamp(timestamp time.Time) {
if m == nil {
return
}
m.headerTimestamp.Record(m.ctx, float64(timestamp.Second()))
if !m.prevHeader.IsZero() {
m.headerTimestamp.Record(m.ctx, timestamp.Sub(m.prevHeader).Seconds())
}
m.prevHeader = timestamp
}
2 changes: 1 addition & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewSyncer[H header.Header[H]](
var metrics *metrics
if params.metrics {
var err error
metrics, err = newMetrics()
metrics, err = newMetrics(params.blockTime)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *Syncer[H]) setSubjectiveHead(ctx context.Context, netHead H) {
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.Params.blockTime, time.Now())
s.metrics.observeLaggingHeader()
}

// incomingNetworkHead processes new potential network headers.
Expand Down

0 comments on commit 121111d

Please sign in to comment.