-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOM: nop success count per feed (#720)
* SOM: nop success count per feed * fix logger name
- Loading branch information
Showing
9 changed files
with
319 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package exporter | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/config" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/metrics" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
func NewNodeSuccessFactory( | ||
log commonMonitoring.Logger, | ||
metrics metrics.NodeSuccess, | ||
) commonMonitoring.ExporterFactory { | ||
return &nodeSuccessFactory{ | ||
log, | ||
metrics, | ||
} | ||
} | ||
|
||
type nodeSuccessFactory struct { | ||
log commonMonitoring.Logger | ||
metrics metrics.NodeSuccess | ||
} | ||
|
||
func (p *nodeSuccessFactory) NewExporter( | ||
params commonMonitoring.ExporterParams, | ||
) (commonMonitoring.Exporter, error) { | ||
nodes, err := config.MakeSolanaNodeConfigs(params.Nodes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nodesMap := map[solana.PublicKey]string{} | ||
for _, v := range nodes { | ||
pubkey, err := v.PublicKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
nodesMap[pubkey] = v.GetName() | ||
} | ||
|
||
return &nodeSuccess{ | ||
metrics.FeedInput{ | ||
AccountAddress: params.FeedConfig.GetContractAddress(), | ||
FeedID: params.FeedConfig.GetContractAddress(), | ||
ChainID: params.ChainConfig.GetChainID(), | ||
ContractStatus: params.FeedConfig.GetContractStatus(), | ||
ContractType: params.FeedConfig.GetContractType(), | ||
FeedName: params.FeedConfig.GetName(), | ||
FeedPath: params.FeedConfig.GetPath(), | ||
NetworkID: params.ChainConfig.GetNetworkID(), | ||
NetworkName: params.ChainConfig.GetNetworkName(), | ||
}, | ||
nodesMap, | ||
p.log, | ||
p.metrics, | ||
}, nil | ||
} | ||
|
||
type nodeSuccess struct { | ||
feedLabel metrics.FeedInput // static for each feed | ||
nodes map[solana.PublicKey]string | ||
log commonMonitoring.Logger | ||
metrics metrics.NodeSuccess | ||
} | ||
|
||
func (p *nodeSuccess) Export(ctx context.Context, data interface{}) { | ||
details, err := types.MakeTxDetails(data) | ||
if err != nil { | ||
return // skip if input could not be parsed | ||
} | ||
|
||
// skip on no updates | ||
if len(details) == 0 { | ||
return | ||
} | ||
|
||
// calculate count | ||
count := map[solana.PublicKey]int{} | ||
for _, d := range details { | ||
count[d.Sender]++ | ||
} | ||
|
||
for k, v := range count { | ||
name, isOperator := p.nodes[k] | ||
if !isOperator { | ||
p.log.Debugw("Sender does not match known operator", "sender", k) | ||
continue // skip if not known operator | ||
} | ||
|
||
p.metrics.Add(v, metrics.NodeFeedInput{ | ||
NodeAddress: k.String(), | ||
NodeOperator: name, | ||
FeedInput: p.feedLabel, | ||
}) | ||
} | ||
} | ||
|
||
func (p *nodeSuccess) Cleanup(_ context.Context) { | ||
for k, v := range p.nodes { | ||
p.metrics.Cleanup(metrics.NodeFeedInput{ | ||
NodeAddress: k.String(), | ||
NodeOperator: v, | ||
FeedInput: p.feedLabel, | ||
}) | ||
} | ||
} |
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,55 @@ | ||
package exporter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/config" | ||
"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 TestNodeSuccess(t *testing.T) { | ||
zeroAddress := solana.PublicKey{} | ||
ctx := tests.Context(t) | ||
lgr, logs := logger.TestObserved(t, zapcore.DebugLevel) | ||
m := mocks.NewNodeSuccess(t) | ||
m.On("Add", mock.Anything, mock.Anything).Once() | ||
m.On("Cleanup", mock.Anything).Once() | ||
|
||
factory := NewNodeSuccessFactory(lgr, m) | ||
|
||
chainConfig := testutils.GenerateChainConfig() | ||
feedConfig := testutils.GenerateFeedConfig() | ||
exporter, err := factory.NewExporter(commonMonitoring.ExporterParams{ChainConfig: chainConfig, | ||
FeedConfig: feedConfig, | ||
Nodes: []commonMonitoring.NodeConfig{ | ||
config.SolanaNodeConfig{ | ||
NodeAddress: []string{zeroAddress.String()}}, | ||
}}) | ||
require.NoError(t, err) | ||
|
||
// happy path - only one call (only 1 address is recognized) | ||
exporter.Export(ctx, []types.TxDetails{ | ||
{Sender: zeroAddress}, | ||
{Sender: solana.PublicKey{1}}, | ||
}) | ||
exporter.Cleanup(ctx) | ||
assert.Equal(t, 1, logs.FilterMessageSnippet("Sender does not match known operator").Len()) | ||
|
||
// not txdetails type - no calls to mock | ||
assert.NotPanics(t, func() { exporter.Export(ctx, 1) }) | ||
|
||
// zero txdetails - no calls to mock | ||
exporter.Export(ctx, []types.TxDetails{}) | ||
} |
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
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,32 @@ | ||
package metrics | ||
|
||
import ( | ||
commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" | ||
) | ||
|
||
//go:generate mockery --name NodeSuccess --output ./mocks/ | ||
|
||
type NodeSuccess interface { | ||
Add(count int, i NodeFeedInput) | ||
Cleanup(i NodeFeedInput) | ||
} | ||
|
||
var _ NodeSuccess = (*nodeSuccess)(nil) | ||
|
||
type nodeSuccess struct { | ||
simpleGauge | ||
} | ||
|
||
func NewNodeSuccess(log commonMonitoring.Logger) *nodeSuccess { | ||
return &nodeSuccess{newSimpleGauge(log, types.NodeSuccessMetric)} | ||
} | ||
|
||
func (ro *nodeSuccess) Add(count int, i NodeFeedInput) { | ||
ro.add(float64(count), i.ToPromLabels()) | ||
} | ||
|
||
func (ro *nodeSuccess) Cleanup(i NodeFeedInput) { | ||
ro.delete(i.ToPromLabels()) | ||
} |
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 metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
"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 TestNodeSuccess(t *testing.T) { | ||
lgr := logger.Test(t) | ||
m := NewNodeSuccess(lgr) | ||
|
||
// fetching gauges | ||
g, ok := gauges[types.NodeSuccessMetric] | ||
require.True(t, ok) | ||
|
||
v := 100 | ||
inputs := NodeFeedInput{FeedInput: FeedInput{NetworkName: t.Name()}} | ||
|
||
// set gauge | ||
assert.NotPanics(t, func() { m.Add(v, inputs) }) | ||
assert.NotPanics(t, func() { m.Add(v, inputs) }) | ||
promBal := testutil.ToFloat64(g.With(inputs.ToPromLabels())) | ||
assert.Equal(t, float64(2*v), promBal) | ||
|
||
// cleanup gauges | ||
assert.Equal(t, 1, testutil.CollectAndCount(g)) | ||
assert.NotPanics(t, func() { m.Cleanup(inputs) }) | ||
assert.Equal(t, 0, testutil.CollectAndCount(g)) | ||
} |
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