Skip to content

Commit

Permalink
refactor(sync): use options to init metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Oct 12, 2023
1 parent 26daa91 commit e30550a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
27 changes: 18 additions & 9 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,40 @@ import (
var meter = otel.Meter("header/sync")

type metrics struct {
totalSynced int64
totalSynced atomic.Int64
totalSyncedGauge metric.Float64ObservableGauge
}

func (s *Syncer[H]) InitMetrics() error {
s.metrics = &metrics{}

func newMetrics() *metrics {
totalSynced, err := meter.Float64ObservableGauge(
"total_synced_headers",
metric.WithDescription("total synced headers"),
)
if err != nil {
return err
panic(err)
}

m := &metrics{
totalSyncedGauge: totalSynced,
}

callback := func(ctx context.Context, observer metric.Observer) error {
observer.ObserveFloat64(totalSynced, float64(atomic.LoadInt64(&s.metrics.totalSynced)))
observer.ObserveFloat64(totalSynced, float64(m.totalSynced.Load()))
return nil
}
_, err = meter.RegisterCallback(callback, totalSynced)
return err
if err != nil {
panic(err)
}

return m
}

// recordTotalSynced records the total amount of synced headers.
func (m *metrics) recordTotalSynced(totalSynced int) {
if m != nil {
atomic.AddInt64(&m.totalSynced, int64(totalSynced))
if m == nil {
return
}

m.totalSynced.Store(int64(totalSynced))

This comment has been minimized.

Copy link
@renaynay

renaynay Oct 18, 2023

Member

Wow we no longer increment here, we just override the value right?

}
9 changes: 9 additions & 0 deletions sync/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Parameters struct {
// recencyThreshold describes the time period for which a header is
// considered "recent". The default is blockTime + 5 seconds.
recencyThreshold time.Duration
// metrics is a flag that enables metrics collection.
metrics bool
}

// DefaultParameters returns the default params to configure the syncer.
Expand All @@ -44,6 +46,13 @@ func (p *Parameters) Validate() error {
return nil
}

// WithMetrics enables Metrics on Syncer.
func WithMetrics() Option {
return func(p *Parameters) {
p.metrics = true
}
}

// WithBlockTime is a functional option that configures the
// `blockTime` parameter.
func WithBlockTime(duration time.Duration) Option {
Expand Down
5 changes: 5 additions & 0 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ func NewSyncer[H header.Header[H]](
return nil, err
}

var metrics *metrics
if params.metrics {
metrics = newMetrics()
}
return &Syncer[H]{
sub: sub,
store: syncStore[H]{Store: store},
getter: syncGetter[H]{Getter: getter},
metrics: metrics,
triggerSync: make(chan struct{}, 1), // should be buffered
Params: &params,
}, nil
Expand Down

0 comments on commit e30550a

Please sign in to comment.