Skip to content

Commit

Permalink
feat(core): track only last timestamp of when sub was stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Oct 27, 2023
1 parent 3b60074 commit ea1bf55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (cl *Listener) Start(context.Context) error {
func (cl *Listener) Stop(context.Context) error {
cl.cancel()
cl.cancel = nil
return nil
return cl.metrics.Close()
}

// runSubscriber runs a subscriber to receive event data of new signed blocks. It will attempt to
Expand Down
34 changes: 26 additions & 8 deletions core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

var meter = otel.Meter("core")

var subscriptionStuckTimestampKey = "subscription_stuck_timestamp"

type metrics struct {
blockTime time.Time
blockTimeInst metric.Float64Histogram

lastTimeSubscriptionStuck time.Time
lastTimeSubscriptionStuckInst metric.Int64Observable
lastTimeSubscriptionStuckReg metric.Registration

subscriptionStuckInst metric.Int64Counter
}

Expand All @@ -40,6 +41,14 @@ func newMetrics() (*metrics, error) {
return nil, err
}

m.lastTimeSubscriptionStuckReg, err = meter.RegisterCallback(
m.observeLastTimeStuckCallback,
m.lastTimeSubscriptionStuckInst,
)
if err != nil {
return nil, err
}

return m, nil
}

Expand Down Expand Up @@ -69,10 +78,19 @@ func (m *metrics) observeBlockTime(ctx context.Context) {

func (m *metrics) subscriptionStuck(ctx context.Context) {
m.observe(ctx, func(ctx context.Context) {
m.subscriptionStuckInst.Add(
ctx,
1,
metric.WithAttributes(attribute.String(subscriptionStuckTimestampKey, time.Now().String())),
)
m.subscriptionStuckInst.Add(ctx, 1)
})
}

func (m *metrics) observeLastTimeStuckCallback(_ context.Context, obs metric.Observer) error {
obs.ObserveInt64(m.lastTimeSubscriptionStuckInst, m.lastTimeSubscriptionStuck.Unix())
return nil
}

func (m *metrics) Close() error {
if m == nil {
return nil
}

return m.lastTimeSubscriptionStuckReg.Unregister()
}
2 changes: 1 addition & 1 deletion nodebuilder/p2p/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package p2p

import (
"fmt"
"slices"

p2pconfig "github.com/libp2p/go-libp2p/config"
hst "github.com/libp2p/go-libp2p/core/host"
ma "github.com/multiformats/go-multiaddr"
"slices"
)

// Listen returns invoke function that starts listening for inbound connections with libp2p.Host.
Expand Down

0 comments on commit ea1bf55

Please sign in to comment.