Skip to content

Commit

Permalink
add slot height metric + SlotHeight type
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed Apr 17, 2024
1 parent 5e3906f commit 99ea507
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/monitoring/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func init() {
},
feedLabels,
)

// init gauge for slot height
gauges[types.SlotHeightMetric] = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: types.SlotHeightMetric,
},
[]string{"chain", "url"},
)
}

type FeedInput struct {
Expand Down
38 changes: 38 additions & 0 deletions pkg/monitoring/metrics/mocks/SlotHeight.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions pkg/monitoring/metrics/slotheight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"

"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types"
)

//go:generate mockery --name SlotHeight --output ./mocks/

type SlotHeight interface {
Set(slot types.SlotHeight, chain, url string)
Cleanup()
}

var _ SlotHeight = (*slotHeight)(nil)

type slotHeight struct {
simpleGauge
labels prometheus.Labels
}

func NewSlotHeight(log commonMonitoring.Logger) *slotHeight {
return &slotHeight{
simpleGauge: newSimpleGauge(log, types.SlotHeightMetric),
}
}

func (sh *slotHeight) Set(slot types.SlotHeight, chain, url string) {
sh.labels = prometheus.Labels{"chain": chain, "url": url}
sh.set(float64(slot), sh.labels)
}

func (sh *slotHeight) Cleanup() {
sh.delete(sh.labels)
}
38 changes: 38 additions & 0 deletions pkg/monitoring/metrics/slotheight_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package metrics

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"

"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types"
)

func TestSlotHeight(t *testing.T) {
lgr := logger.Test(t)
m := NewSlotHeight(lgr)

// fetching gauges
g, ok := gauges[types.SlotHeightMetric]
require.True(t, ok)

v := 100

// set gauge
assert.NotPanics(t, func() { m.Set(types.SlotHeight(v), t.Name(), t.Name()+"_url") })
promBal := testutil.ToFloat64(g.With(prometheus.Labels{
"chain": t.Name(),
"url": t.Name() + "_url",
}))
assert.Equal(t, float64(v), promBal)

// cleanup gauges
assert.Equal(t, 1, testutil.CollectAndCount(g))
assert.NotPanics(t, func() { m.Cleanup() })
assert.Equal(t, 0, testutil.CollectAndCount(g))
}
3 changes: 2 additions & 1 deletion pkg/monitoring/source_slotheight.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ type slotHeightSource struct {
}

func (t *slotHeightSource) Fetch(ctx context.Context) (interface{}, error) {
return t.client.GetSlot(ctx) // TODO: wrap the type to make it clear which type it is?
slot, err := t.client.GetSlot(ctx)
return types.SlotHeight(slot), err
}
8 changes: 7 additions & 1 deletion pkg/monitoring/types/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package types

// types.go contains simple types, more complex types should have a separate file
const (
SlotHeightType = "slot_height"
SlotHeightType = "slot_height"
SlotHeightMetric = "sol_" + SlotHeightType
)

// SlotHeight type wraps the uint64 type returned by the RPC call
// this helps to delineate types when sending to the exporter
type SlotHeight uint64

0 comments on commit 99ea507

Please sign in to comment.