Skip to content

Commit

Permalink
feat(cannon): Create Slot Iterator metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Sep 13, 2023
1 parent 2bc59d7 commit 249849d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 82 deletions.
124 changes: 55 additions & 69 deletions pkg/cannon/cannon.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,110 +276,96 @@ func (c *Cannon) startBeaconBlockProcessor(ctx context.Context) error {
return err
}

slotIteratorMetrics, err := iterator.NewSlotMetrics("xatu_cannon")
if err != nil {
return err
}

attesterSlashingIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)

proposerSlashingIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)

voluntaryExitIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)

depositIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)

blsToExecutionChangeIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)
executionTransactionIterator := iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
)
slotIteratorMetrics := iterator.NewSlotMetrics("xatu_cannon")

eventDerivers := []deriver.EventDeriver{
v2.NewAttesterSlashingDeriver(
c.log,
&c.Config.Derivers.AttesterSlashingConfig,
attesterSlashingIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
v2.NewProposerSlashingDeriver(
c.log,
&c.Config.Derivers.ProposerSlashingConfig,
proposerSlashingIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
v2.NewVoluntaryExitDeriver(
c.log,
&c.Config.Derivers.VoluntaryExitConfig,
voluntaryExitIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
v2.NewDepositDeriver(
c.log,
&c.Config.Derivers.DepositConfig,
depositIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
v2.NewBLSToExecutionChangeDeriver(
c.log,
&c.Config.Derivers.BLSToExecutionConfig,
blsToExecutionChangeIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
v2.NewExecutionTransactionDeriver(
c.log,
&c.Config.Derivers.ExecutionTransactionConfig,
executionTransactionIterator,
iterator.NewSlotIterator(
c.log,
networkName,
networkID,
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION,
c.coordinatorClient,
wallclock,
&slotIteratorMetrics,
),
c.beacon,
clientMeta,
),
Expand Down
16 changes: 9 additions & 7 deletions pkg/cannon/iterator/slot_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (s *SlotIterator) Next(ctx context.Context) (*xatu.CannonLocation, error) {
return
}

s.metrics.SetCurrentSlot(s.cannonType.String(), s.networkName, float64(slot))

s.metrics.SetTrailingSlots(s.cannonType.String(), s.networkName, float64(headSlot.Number()-uint64(slot)))
}
}()
Expand All @@ -71,12 +73,12 @@ func (s *SlotIterator) Next(ctx context.Context) (*xatu.CannonLocation, error) {
// If location is empty we haven't started yet, start at the network default for the type. If the network default
// is empty, we'll start at slot 0.
if location == nil {
locaction, errr := s.createLocationFromSlotNumber(GetDefaultSlotLocationForNetworkAndType(s.networkID, s.cannonType))
if errr != nil {
location, err = s.createLocationFromSlotNumber(GetDefaultSlotLocationForNetworkAndType(s.networkID, s.cannonType))
if err != nil {
return nil, errors.Wrap(err, "failed to create location from slot number 0")
}

return locaction, nil
return location, nil
}

locationSlot, err := s.getSlotNumberFromLocation(location)
Expand All @@ -86,12 +88,12 @@ func (s *SlotIterator) Next(ctx context.Context) (*xatu.CannonLocation, error) {

// If the current wallclock slot is greater than the last slot we processed, return the next slot
if phase0.Slot(headSlot.Number()) > locationSlot {
loc, errr := s.createLocationFromSlotNumber(locationSlot + 1)
if errr != nil {
return nil, errors.Wrap(errr, fmt.Errorf("failed to create location from slot number: %d", locationSlot+1).Error())
location, err = s.createLocationFromSlotNumber(locationSlot + 1)
if err != nil {
return nil, errors.Wrap(err, fmt.Errorf("failed to create location from slot number: %d", locationSlot+1).Error())
}

return loc, nil
return location, nil
}

// Sleep until the next slot
Expand Down
23 changes: 17 additions & 6 deletions pkg/cannon/iterator/slot_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@ import "github.com/prometheus/client_golang/prometheus"

type SlotMetrics struct {
TrailingSlots *prometheus.GaugeVec
CurrentSlot *prometheus.GaugeVec
}

func NewSlotMetrics(namespace string) (SlotMetrics, error) {
func NewSlotMetrics(namespace string) SlotMetrics {
namespace += "_slot_iterator"

s := SlotMetrics{
TrailingSlots: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "slot_iterator_trailing_slots",
Name: "trailing_slots",
Help: "The number of slots that the iterator is trailing behind the head of the chain",
}, []string{"cannon_type", "network"}),
CurrentSlot: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "current_slot",
Help: "The current slot",
}, []string{"cannon_type", "network"}),
}

if err := prometheus.Register(s.TrailingSlots); err != nil && err.Error() != "duplicate metrics collector registration attempted" {
return s, err
}
prometheus.MustRegister(s.TrailingSlots)
prometheus.MustRegister(s.CurrentSlot)

return s, nil
return s
}

func (s *SlotMetrics) SetTrailingSlots(cannonType, network string, trailingSlots float64) {
s.TrailingSlots.WithLabelValues(cannonType, network).Set(trailingSlots)
}

func (s *SlotMetrics) SetCurrentSlot(cannonType, network string, currentSlot float64) {
s.CurrentSlot.WithLabelValues(cannonType, network).Set(currentSlot)
}

0 comments on commit 249849d

Please sign in to comment.