-
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.
- Loading branch information
Showing
5 changed files
with
171 additions
and
75 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
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,82 @@ | ||
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 NewReportObservationsFactory( | ||
log commonMonitoring.Logger, | ||
metrics metrics.ReportObservations, | ||
) commonMonitoring.ExporterFactory { | ||
return &reportObservationsFactory{ | ||
log, | ||
metrics, | ||
} | ||
} | ||
|
||
type reportObservationsFactory struct { | ||
log commonMonitoring.Logger | ||
metrics metrics.ReportObservations | ||
} | ||
|
||
func (p *reportObservationsFactory) NewExporter( | ||
params commonMonitoring.ExporterParams, | ||
) (commonMonitoring.Exporter, error) { | ||
return &reportObservations{ | ||
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(), | ||
}, | ||
p.log, | ||
p.metrics, | ||
}, nil | ||
} | ||
|
||
type reportObservations struct { | ||
label metrics.FeedInput // static for each feed | ||
log commonMonitoring.Logger | ||
metrics metrics.ReportObservations | ||
} | ||
|
||
func (p *reportObservations) 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 | ||
} | ||
|
||
// sanity check: find non-empty detail | ||
// assumption: details ordered from latest -> earliest | ||
var latest types.TxDetails | ||
for _, d := range details { | ||
if !d.Empty() { | ||
latest = d | ||
break | ||
} | ||
} | ||
if latest.Empty() { | ||
p.log.Errorw("exporter could not find non-empty TxDetails", "feed", p.label.ToPromLabels()) | ||
return | ||
} | ||
|
||
p.metrics.SetCount(latest.ObservationCount, p.label) | ||
} | ||
|
||
func (p *reportObservations) Cleanup(_ context.Context) { | ||
p.metrics.Cleanup(p.label) | ||
} |
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 ( | ||
"testing" | ||
|
||
"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" | ||
"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 TestReportObservations(t *testing.T) { | ||
ctx := utils.Context(t) | ||
lgr, logs := logger.TestObserved(t, zapcore.ErrorLevel) | ||
m := mocks.NewReportObservations(t) | ||
m.On("SetCount", mock.Anything, mock.Anything).Once() | ||
m.On("Cleanup", mock.Anything).Once() | ||
|
||
factory := NewReportObservationsFactory(lgr, m) | ||
|
||
chainConfig := testutils.GenerateChainConfig() | ||
feedConfig := testutils.GenerateFeedConfig() | ||
exporter, err := factory.NewExporter(commonMonitoring.ExporterParams{ChainConfig: chainConfig, FeedConfig: feedConfig, Nodes: []commonMonitoring.NodeConfig{}}) | ||
require.NoError(t, err) | ||
|
||
// happy path | ||
exporter.Export(ctx, []types.TxDetails{{ObservationCount: 10}}) | ||
exporter.Cleanup(ctx) | ||
|
||
// 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{}) | ||
|
||
// empty txdetails | ||
exporter.Export(ctx, []types.TxDetails{{}}) | ||
assert.Equal(t, 1, logs.FilterMessage("exporter could not find non-empty TxDetails").Len()) | ||
|
||
// multiple TxDetails should only call for the first non-empty one | ||
m.On("SetCount", uint8(1), mock.Anything).Once() | ||
exporter.Export(ctx, []types.TxDetails{{}, {ObservationCount: 1}, {ObservationCount: 10}}) | ||
} |