Skip to content

Commit

Permalink
Add proposervm slot metrics (#3048)
Browse files Browse the repository at this point in the history
Signed-off-by: Tsachi Herman <24438559+tsachiherman@users.noreply.github.com>
  • Loading branch information
tsachiherman authored Jun 6, 2024
1 parent 7dca396 commit 59bc3cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
8 changes: 8 additions & 0 deletions vms/proposervm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ func (p *postForkCommonComponents) verifyPostDurangoBlockDelay(
currentSlot = proposer.TimeToSlot(parentTimestamp, blkTimestamp)
proposerID = blk.Proposer()
)
// populate the slot for the block.
blk.slot = &currentSlot

// find the expected proposer
expectedProposerID, err := p.vm.Windower.ExpectedProposer(
ctx,
blkHeight,
Expand Down Expand Up @@ -452,6 +455,11 @@ func (p *postForkCommonComponents) shouldBuildSignedBlockPostDurango(
)
return false, err
}

// report the build slot to the metrics.
p.vm.proposerBuildSlotGauge.Set(float64(proposer.TimeToSlot(parentTimestamp, nextStartTime)))

// set the scheduler to let us know when the next block need to be built.
p.vm.Scheduler.SetBuildBlockTime(nextStartTime)

// In case the inner VM only issued one pendingTxs message, we should
Expand Down
5 changes: 3 additions & 2 deletions vms/proposervm/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,9 @@ func TestPostDurangoBuildChildResetScheduler(t *testing.T) {
ValidatorState: vdrState,
Log: logging.NoLog{},
},
Windower: windower,
Scheduler: scheduler,
Windower: windower,
Scheduler: scheduler,
proposerBuildSlotGauge: prometheus.NewGauge(prometheus.GaugeOpts{}),
}
vm.Clock.Set(now)

Expand Down
13 changes: 12 additions & 1 deletion vms/proposervm/post_fork_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var _ PostForkBlock = (*postForkBlock)(nil)
type postForkBlock struct {
block.SignedBlock
postForkCommonComponents

// slot of the proposer that produced this block.
// It is populated in verifyPostDurangoBlockDelay.
// It is used to report metrics during Accept.
slot *uint64
}

// Accept:
Expand All @@ -27,7 +32,13 @@ func (b *postForkBlock) Accept(ctx context.Context) error {
if err := b.acceptOuterBlk(); err != nil {
return err
}
return b.acceptInnerBlk(ctx)
if err := b.acceptInnerBlk(ctx); err != nil {
return err
}
if b.slot != nil {
b.vm.acceptedBlocksSlotHistogram.Observe(float64(*b.slot))
}
return nil
}

func (b *postForkBlock) acceptOuterBlk() error {
Expand Down
39 changes: 36 additions & 3 deletions vms/proposervm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/cache"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
Expand Down Expand Up @@ -97,6 +99,14 @@ type VM struct {

// lastAcceptedHeight is set to the last accepted PostForkBlock's height.
lastAcceptedHeight uint64

// proposerBuildSlotGauge reports the slot index when this node may attempt
// to build a block.
proposerBuildSlotGauge prometheus.Gauge

// acceptedBlocksSlotHistogram reports the slots that accepted blocks were
// proposed in.
acceptedBlocksSlotHistogram prometheus.Histogram
}

// New performs best when [minBlkDelay] is whole seconds. This is because block
Expand Down Expand Up @@ -206,7 +216,28 @@ func (vm *VM) Initialize(
default:
return err
}
return nil

vm.proposerBuildSlotGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "block_building_slot",
Help: "the slot that this node may attempt to build a block",
})
vm.acceptedBlocksSlotHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "accepted_blocks_slot",
Help: "the slot accepted blocks were proposed in",
// define the following ranges:
// (-inf, 0]
// (0, 1]
// (1, 2]
// (2, inf)
// the usage of ".5" before was to ensure we work around the limitation
// of comparing floating point of the same numerical value.
Buckets: []float64{0.5, 1.5, 2.5},
})

return utils.Err(
vm.Config.Registerer.Register(vm.proposerBuildSlotGauge),
vm.Config.Registerer.Register(vm.acceptedBlocksSlotHistogram),
)
}

// shutdown ops then propagate shutdown to innerVM
Expand Down Expand Up @@ -294,13 +325,15 @@ func (vm *VM) SetPreference(ctx context.Context, preferred ids.ID) error {
)
if vm.IsDurangoActivated(parentTimestamp) {
currentTime := vm.Clock.Time().Truncate(time.Second)
nextStartTime, err = vm.getPostDurangoSlotTime(
if nextStartTime, err = vm.getPostDurangoSlotTime(
ctx,
childBlockHeight,
pChainHeight,
proposer.TimeToSlot(parentTimestamp, currentTime),
parentTimestamp,
)
); err == nil {
vm.proposerBuildSlotGauge.Set(float64(proposer.TimeToSlot(parentTimestamp, nextStartTime)))
}
} else {
nextStartTime, err = vm.getPreDurangoSlotTime(
ctx,
Expand Down

0 comments on commit 59bc3cf

Please sign in to comment.