Skip to content

Commit d8f649a

Browse files
ARR4NDarioush Jalaliqdm12
authored
chore(proposervm): timestamp metrics for block acceptance (#3680)
Signed-off-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com> Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org> Co-authored-by: Quentin McGaw <quentin.mcgaw@avalabs.org>
1 parent 9503b38 commit d8f649a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

vms/proposervm/post_fork_block.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package proposervm
55

66
import (
77
"context"
8+
"time"
89

910
"github.com/ava-labs/avalanchego/ids"
1011
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
@@ -37,9 +38,21 @@ func (b *postForkBlock) Accept(ctx context.Context) error {
3738
if b.slot != nil {
3839
b.vm.acceptedBlocksSlotHistogram.Observe(float64(*b.slot))
3940
}
41+
b.updateLastAcceptedTimestampMetric(outerBlockTypeMetricLabel, b.Timestamp())
42+
b.updateLastAcceptedTimestampMetric(innerBlockTypeMetricLabel, b.innerBlk.Timestamp())
4043
return nil
4144
}
4245

46+
const (
47+
innerBlockTypeMetricLabel = "inner"
48+
outerBlockTypeMetricLabel = "proposervm"
49+
)
50+
51+
func (b *postForkBlock) updateLastAcceptedTimestampMetric(blockTypeLabel string, t time.Time) {
52+
g := b.vm.lastAcceptedTimestampGaugeVec.WithLabelValues(blockTypeLabel)
53+
g.Set(float64(t.Unix()))
54+
}
55+
4356
func (b *postForkBlock) acceptOuterBlk() error {
4457
// Update in-memory references
4558
b.vm.lastAcceptedTime = b.Timestamp()

vms/proposervm/vm.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ type VM struct {
105105
// acceptedBlocksSlotHistogram reports the slots that accepted blocks were
106106
// proposed in.
107107
acceptedBlocksSlotHistogram prometheus.Histogram
108+
109+
// lastAcceptedTimestampGaugeVec reports timestamps for the last-accepted
110+
// [postForkBlock] and its inner block.
111+
lastAcceptedTimestampGaugeVec *prometheus.GaugeVec
108112
}
109113

110114
// New performs best when [minBlkDelay] is whole seconds. This is because block
@@ -231,10 +235,18 @@ func (vm *VM) Initialize(
231235
// of comparing floating point of the same numerical value.
232236
Buckets: []float64{0.5, 1.5, 2.5},
233237
})
238+
vm.lastAcceptedTimestampGaugeVec = prometheus.NewGaugeVec(
239+
prometheus.GaugeOpts{
240+
Name: "last_accepted_timestamp",
241+
Help: "timestamp of the last block accepted",
242+
},
243+
[]string{"block_type"},
244+
)
234245

235246
return errors.Join(
236247
vm.Config.Registerer.Register(vm.proposerBuildSlotGauge),
237248
vm.Config.Registerer.Register(vm.acceptedBlocksSlotHistogram),
249+
vm.Config.Registerer.Register(vm.lastAcceptedTimestampGaugeVec),
238250
)
239251
}
240252

vms/proposervm/vm_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/prometheus/client_golang/prometheus"
16+
"github.com/prometheus/client_golang/prometheus/testutil"
1617
"github.com/stretchr/testify/require"
1718
"go.uber.org/mock/gomock"
1819

@@ -2530,3 +2531,43 @@ func TestLocalParse(t *testing.T) {
25302531
})
25312532
}
25322533
}
2534+
2535+
func TestTimestampMetrics(t *testing.T) {
2536+
ctx := context.Background()
2537+
2538+
coreVM, _, proVM, _ := initTestProposerVM(t, time.Unix(0, 0), mockable.MaxTime, 0)
2539+
defer func() {
2540+
require.NoError(t, proVM.Shutdown(ctx))
2541+
}()
2542+
2543+
innerBlock := snowmantest.BuildChild(snowmantest.Genesis)
2544+
2545+
outerTime := time.Unix(314159, 0)
2546+
innerTime := time.Unix(142857, 0)
2547+
proVM.Clock.Set(outerTime)
2548+
innerBlock.TimestampV = innerTime
2549+
2550+
coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) {
2551+
return innerBlock, nil
2552+
}
2553+
outerBlock, err := proVM.BuildBlock(ctx)
2554+
require.NoError(t, err)
2555+
require.IsType(t, &postForkBlock{}, outerBlock)
2556+
require.NoError(t, outerBlock.Accept(ctx))
2557+
2558+
gaugeVec := proVM.lastAcceptedTimestampGaugeVec
2559+
tests := []struct {
2560+
blockType string
2561+
want time.Time
2562+
}{
2563+
{innerBlockTypeMetricLabel, innerTime},
2564+
{outerBlockTypeMetricLabel, outerTime},
2565+
}
2566+
for _, tt := range tests {
2567+
t.Run(tt.blockType, func(t *testing.T) {
2568+
gauge, err := gaugeVec.GetMetricWithLabelValues(tt.blockType)
2569+
require.NoError(t, err)
2570+
require.Equal(t, float64(tt.want.Unix()), testutil.ToFloat64(gauge))
2571+
})
2572+
}
2573+
}

0 commit comments

Comments
 (0)