Skip to content

Commit

Permalink
fix: handle error on metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Oct 12, 2023
1 parent 27e85f9 commit 4716bc9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
6 changes: 5 additions & 1 deletion p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ func NewExchange[H header.Header[H]](

var metrics *metrics
if params.metrics {
metrics = newExchangeMetrics()
var err error
metrics, err = newExchangeMetrics()
if err != nil {
return nil, err
}
}

ex := &Exchange[H]{
Expand Down
8 changes: 4 additions & 4 deletions p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ type metrics struct {
responseDuration metric.Float64Histogram
}

func newExchangeMetrics() *metrics {
func newExchangeMetrics() (*metrics, error) {
responseSize, err := meter.Float64Histogram(
"header_p2p_exchange_response_size",
metric.WithDescription("Size of get headers response in bytes"),
)
if err != nil {
panic(err)
return nil, err
}
responseDuration, err := meter.Float64Histogram(
"header_p2p_exchange_request_duration",
metric.WithDescription("Duration of get headers request in seconds"),
)
if err != nil {
panic(err)
return nil, err
}
return &metrics{
responseSize: responseSize,
responseDuration: responseDuration,
}
}, nil
}

func (m *metrics) observeResponse(ctx context.Context, size uint64, duration uint64, err error) {
Expand Down
4 changes: 2 additions & 2 deletions p2p/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewSubscriber[H header.Header[H]](
ps *pubsub.PubSub,
msgID pubsub.MsgIdFunction,
opts ...SubscriberOption,
) *Subscriber[H] {
) (*Subscriber[H], error) {
var params SubscriberParams
for _, opt := range opts {
opt(&params)
Expand All @@ -61,7 +61,7 @@ func NewSubscriber[H header.Header[H]](
pubsubTopicID: PubsubTopicID(params.networkID),
pubsub: ps,
msgID: msgID,
}
}, nil
}

// Start starts the Subscriber, registering a topic validator for the "header-sub"
Expand Down
6 changes: 4 additions & 2 deletions p2p/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestSubscriber(t *testing.T) {
require.NoError(t, err)

// create sub-service lifecycles for header service 1
p2pSub1 := NewSubscriber[*headertest.DummyHeader](pubsub1, pubsub.DefaultMsgIdFn, WithSubscriberNetworkID(networkID))
p2pSub1, err := NewSubscriber[*headertest.DummyHeader](pubsub1, pubsub.DefaultMsgIdFn, WithSubscriberNetworkID(networkID))
require.NoError(t, err)
err = p2pSub1.Start(context.Background())
require.NoError(t, err)

Expand All @@ -40,7 +41,8 @@ func TestSubscriber(t *testing.T) {
require.NoError(t, err)

// create sub-service lifecycles for header service 2
p2pSub2 := NewSubscriber[*headertest.DummyHeader](pubsub2, pubsub.DefaultMsgIdFn, WithSubscriberNetworkID(networkID))
p2pSub2, err := NewSubscriber[*headertest.DummyHeader](pubsub2, pubsub.DefaultMsgIdFn, WithSubscriberNetworkID(networkID))
require.NoError(t, err)
err = p2pSub2.Start(context.Background())
require.NoError(t, err)

Expand Down
8 changes: 4 additions & 4 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type metrics struct {
totalSyncedGauge metric.Float64ObservableGauge
}

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

m := &metrics{
Expand All @@ -34,10 +34,10 @@ func newMetrics() *metrics {
}
_, err = meter.RegisterCallback(callback, totalSynced)
if err != nil {
panic(err)
return nil, err
}

return m
return m, nil
}

// recordTotalSynced records the total amount of synced headers.
Expand Down
6 changes: 5 additions & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func NewSyncer[H header.Header[H]](

var metrics *metrics
if params.metrics {
metrics = newMetrics()
var err error
metrics, err = newMetrics()
if err != nil {
return nil, err
}
}
return &Syncer[H]{
sub: sub,
Expand Down

0 comments on commit 4716bc9

Please sign in to comment.