Skip to content

Commit

Permalink
feat: expose rank & active set metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Sep 14, 2023
1 parent 4f0d3e8 commit c3cf083
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ValidatorStatus struct {
Label string
Bonded bool
Signed bool
Rank int
}

func (e *Exporter) handleBlock(block *types.Block) {
Expand All @@ -90,6 +91,7 @@ func (e *Exporter) handleBlock(block *types.Block) {

e.latestBlockHeight = block.Header.Height
e.cfg.Metrics.BlockHeight.Set(float64(block.Header.Height))
e.cfg.Metrics.ActiveSet.Set(float64(len(block.LastCommit.Signatures)))
e.cfg.Metrics.TrackedBlocks.Inc()

result := BlockResult{
Expand All @@ -108,13 +110,16 @@ func (e *Exporter) handleBlock(block *types.Block) {
for _, val := range e.cfg.TrackedValidators {
bonded := false
signed := false
rank := 0
for i, sig := range block.LastCommit.Signatures {
if sig.ValidatorAddress.String() == "" {
log.Warn().Msgf("empty validator address at pos %d", i)
}
if val.Address == sig.ValidatorAddress.String() {
bonded = true
signed = !sig.Absent()
rank = i + 1
e.cfg.Metrics.Rank.WithLabelValues(val.Address, val.Name).Set(float64(rank))
}
if signed {
break
Expand All @@ -125,6 +130,7 @@ func (e *Exporter) handleBlock(block *types.Block) {
Label: val.Name,
Bonded: bonded,
Signed: signed,
Rank: rank,
})
}

Expand Down
44 changes: 31 additions & 13 deletions pkg/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (

func TestExporter(t *testing.T) {
var (
address = "3DC4DD610817606AD4A8F9D762A068A81E8741E2"
name = "Kiln"
kilnAddress = "3DC4DD610817606AD4A8F9D762A068A81E8741E2"
kilnName = "Kiln"

miscAddress = "1234567890ABCDEF10817606AD4A8FD7620A81E4"
)

exporter := New(&Config{
Expand All @@ -29,8 +31,8 @@ func TestExporter(t *testing.T) {
ValidatorsChan: make(chan []stakingtypes.Validator),
TrackedValidators: []TrackedValidator{
{
Address: address,
Name: name,
Address: kilnAddress,
Name: kilnName,
},
},
})
Expand Down Expand Up @@ -67,7 +69,7 @@ func TestExporter(t *testing.T) {
Signatures: []types.CommitSig{
{
BlockIDFlag: types.BlockIDFlagAbsent,
ValidatorAddress: MustParseAddress(address),
ValidatorAddress: MustParseAddress(kilnAddress),
},
},
},
Expand All @@ -76,9 +78,13 @@ func TestExporter(t *testing.T) {
Header: types.Header{Height: 41},
LastCommit: &types.Commit{
Signatures: []types.CommitSig{
{
BlockIDFlag: types.BlockIDFlagAbsent,
ValidatorAddress: MustParseAddress(miscAddress),
},
{
BlockIDFlag: types.BlockIDFlagCommit,
ValidatorAddress: MustParseAddress(address),
ValidatorAddress: MustParseAddress(kilnAddress),
},
},
},
Expand All @@ -89,7 +95,11 @@ func TestExporter(t *testing.T) {
Signatures: []types.CommitSig{
{
BlockIDFlag: types.BlockIDFlagCommit,
ValidatorAddress: MustParseAddress(address),
ValidatorAddress: MustParseAddress(miscAddress),
},
{
BlockIDFlag: types.BlockIDFlagCommit,
ValidatorAddress: MustParseAddress(kilnAddress),
},
},
},
Expand All @@ -104,8 +114,8 @@ func TestExporter(t *testing.T) {
strings.Join([]string{
`#35 0/1 validators ⚪️ Kiln`,
`#40 0/1 validators ❌ Kiln`,
`#41 1/1 validators ✅ Kiln`,
`#42 1/1 validators ✅ Kiln`,
`#41 1/2 validators ✅ Kiln`,
`#42 2/2 validators ✅ Kiln`,
}, "\n")+"\n",
exporter.cfg.Writer.(*bytes.Buffer).String(),
)
Expand All @@ -114,6 +124,10 @@ func TestExporter(t *testing.T) {
`gauge:<value:42 > `,
ReadMetric(exporter.cfg.Metrics.BlockHeight),
)
assert.Equal(t,
`gauge:<value:2 > `,
ReadMetric(exporter.cfg.Metrics.ActiveSet),
)
assert.Equal(t,
`counter:<value:4 > `,
ReadMetric(exporter.cfg.Metrics.TrackedBlocks),
Expand All @@ -124,11 +138,15 @@ func TestExporter(t *testing.T) {
)
assert.Equal(t,
`label:<name:"address" value:"3DC4DD610817606AD4A8F9D762A068A81E8741E2" > label:<name:"name" value:"Kiln" > counter:<value:2 > `,
ReadMetric(exporter.cfg.Metrics.ValidatedBlocks.WithLabelValues(address, name)),
ReadMetric(exporter.cfg.Metrics.Rank.WithLabelValues(kilnAddress, kilnName)),
)
assert.Equal(t,
`label:<name:"address" value:"3DC4DD610817606AD4A8F9D762A068A81E8741E2" > label:<name:"name" value:"Kiln" > counter:<value:2 > `,
ReadMetric(exporter.cfg.Metrics.ValidatedBlocks.WithLabelValues(kilnAddress, kilnName)),
)
assert.Equal(t,
`label:<name:"address" value:"3DC4DD610817606AD4A8F9D762A068A81E8741E2" > label:<name:"name" value:"Kiln" > counter:<value:1 > `,
ReadMetric(exporter.cfg.Metrics.MissedBlocks.WithLabelValues(address, name)),
ReadMetric(exporter.cfg.Metrics.MissedBlocks.WithLabelValues(kilnAddress, kilnName)),
)
})

Expand Down Expand Up @@ -156,11 +174,11 @@ func TestExporter(t *testing.T) {

assert.Equal(t,
`label:<name:"address" value:"3DC4DD610817606AD4A8F9D762A068A81E8741E2" > label:<name:"name" value:"Kiln" > gauge:<value:1 > `,
ReadMetric(exporter.cfg.Metrics.ValidatorBonded.WithLabelValues(address, name)),
ReadMetric(exporter.cfg.Metrics.ValidatorBonded.WithLabelValues(kilnAddress, kilnName)),
)
assert.Equal(t,
`label:<name:"address" value:"3DC4DD610817606AD4A8F9D762A068A81E8741E2" > label:<name:"name" value:"Kiln" > gauge:<value:0 > `,
ReadMetric(exporter.cfg.Metrics.ValidatorJail.WithLabelValues(address, name)),
ReadMetric(exporter.cfg.Metrics.ValidatorJail.WithLabelValues(kilnAddress, kilnName)),
)
})
}
19 changes: 19 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "github.com/prometheus/client_golang/prometheus"
type Metrics struct {
// Exporter metrics
BlockHeight prometheus.Gauge
Rank *prometheus.GaugeVec
ActiveSet prometheus.Gauge
ValidatedBlocks *prometheus.CounterVec
MissedBlocks *prometheus.CounterVec
TrackedBlocks prometheus.Counter
Expand All @@ -26,6 +28,21 @@ func New(namespace string) *Metrics {
Help: "Latest known block height (all nodes mixed up)",
},
),
Rank: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "rank",
Help: "Rank of the validator (for a bonded validator)",
},
[]string{"address", "name"},
),
ActiveSet: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "active_set",
Help: "Number of validators in the active set",
},
),
ValidatedBlocks: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Expand Down Expand Up @@ -91,6 +108,8 @@ func New(namespace string) *Metrics {
}

prometheus.MustRegister(metrics.BlockHeight)
prometheus.MustRegister(metrics.Rank)
prometheus.MustRegister(metrics.ActiveSet)
prometheus.MustRegister(metrics.ValidatedBlocks)
prometheus.MustRegister(metrics.MissedBlocks)
prometheus.MustRegister(metrics.TrackedBlocks)
Expand Down

0 comments on commit c3cf083

Please sign in to comment.