-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: slot height tracking * add slot height metric + SlotHeight type * implement exporter * fix: file naming * share naming + test cov
- Loading branch information
Showing
12 changed files
with
343 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package exporter | ||
|
||
import ( | ||
"context" | ||
|
||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/metrics" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
func NewSlotHeightFactory( | ||
_ commonMonitoring.Logger, | ||
metrics metrics.SlotHeight, | ||
) commonMonitoring.ExporterFactory { | ||
return &slotHeightFactory{ | ||
metrics, | ||
} | ||
} | ||
|
||
type slotHeightFactory struct { | ||
metrics metrics.SlotHeight | ||
} | ||
|
||
func (p *slotHeightFactory) NewExporter( | ||
params commonMonitoring.ExporterParams, | ||
) (commonMonitoring.Exporter, error) { | ||
return &slotHeight{ | ||
params.ChainConfig.GetNetworkName(), | ||
params.ChainConfig.GetRPCEndpoint(), | ||
p.metrics, | ||
}, nil | ||
} | ||
|
||
type slotHeight struct { | ||
chain, url string | ||
metrics metrics.SlotHeight | ||
} | ||
|
||
func (p *slotHeight) Export(ctx context.Context, data interface{}) { | ||
slot, ok := data.(types.SlotHeight) | ||
if !ok { | ||
return // skip if input could not be parsed | ||
} | ||
|
||
p.metrics.Set(slot, p.chain, p.url) | ||
} | ||
|
||
func (p *slotHeight) Cleanup(_ context.Context) { | ||
p.metrics.Cleanup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package exporter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/metrics/mocks" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/testutils" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
func TestSlotHeight(t *testing.T) { | ||
ctx := utils.Context(t) | ||
m := mocks.NewSlotHeight(t) | ||
m.On("Set", mock.Anything, mock.Anything, mock.Anything).Once() | ||
m.On("Cleanup").Once() | ||
|
||
factory := NewSlotHeightFactory(logger.Test(t), m) | ||
|
||
chainConfig := testutils.GenerateChainConfig() | ||
exporter, err := factory.NewExporter(commonMonitoring.ExporterParams{ChainConfig: chainConfig}) | ||
require.NoError(t, err) | ||
|
||
// happy path | ||
exporter.Export(ctx, types.SlotHeight(10)) | ||
exporter.Cleanup(ctx) | ||
|
||
// test passing uint64 instead of SlotHeight - should not call mock | ||
// SlotHeight alias of uint64 | ||
exporter.Export(ctx, uint64(10)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package monitoring | ||
|
||
import ( | ||
"context" | ||
|
||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
func NewSlotHeightSourceFactory( | ||
client ChainReader, | ||
log commonMonitoring.Logger, | ||
) commonMonitoring.NetworkSourceFactory { | ||
return &slotHeightSourceFactory{ | ||
client, | ||
log, | ||
} | ||
} | ||
|
||
type slotHeightSourceFactory struct { | ||
client ChainReader | ||
log commonMonitoring.Logger | ||
} | ||
|
||
func (s *slotHeightSourceFactory) NewSource( | ||
_ commonMonitoring.ChainConfig, | ||
_ []commonMonitoring.NodeConfig, | ||
) (commonMonitoring.Source, error) { | ||
return &slotHeightSource{s.client}, nil | ||
} | ||
|
||
func (s *slotHeightSourceFactory) GetType() string { | ||
return types.SlotHeightType | ||
} | ||
|
||
type slotHeightSource struct { | ||
client ChainReader | ||
} | ||
|
||
func (t *slotHeightSource) Fetch(ctx context.Context) (interface{}, error) { | ||
slot, err := t.client.GetSlot(ctx) | ||
return types.SlotHeight(slot), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package monitoring | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/mocks" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
func TestSlotHeightSource(t *testing.T) { | ||
cr := mocks.NewChainReader(t) | ||
lgr := logger.Test(t) | ||
ctx := utils.Context(t) | ||
|
||
factory := NewSlotHeightSourceFactory(cr, lgr) | ||
assert.Equal(t, types.SlotHeightType, factory.GetType()) | ||
|
||
// generate source | ||
source, err := factory.NewSource(nil, nil) | ||
cr.On("GetSlot", mock.Anything, mock.Anything, mock.Anything).Return(uint64(1), nil).Once() | ||
|
||
// happy path | ||
out, err := source.Fetch(ctx) | ||
require.NoError(t, err) | ||
slot, ok := out.(types.SlotHeight) | ||
require.True(t, ok) | ||
assert.Equal(t, types.SlotHeight(1), slot) | ||
} |
Oops, something went wrong.