-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(promexporter): relayer balances #3016
Changes from 10 commits
21e472c
6d2b1cb
91b37a1
68ae907
c43fc87
309a00f
87d3264
c3eb04c
b5454b2
8e0accb
8478400
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,11 @@ type submitterMetadata struct { | |
balance float64 | ||
} | ||
|
||
type relayerMetadata struct { | ||
address common.Address | ||
balance float64 | ||
} | ||
Comment on lines
+23
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The new Do you want me to generate test cases for this new struct or open a GitHub issue to track this task? |
||
|
||
//go:generate go run github.com/vburenin/ifacemaker -f otel.go -s otelRecorder -i iOtelRecorder -p exporters -o otel_generated.go -c "autogenerated file" | ||
type otelRecorder struct { | ||
metrics metrics.Handler | ||
|
@@ -48,19 +53,24 @@ type otelRecorder struct { | |
submitters *hashmap.Map[int, []submitterMetadata] | ||
balanceGauge metric.Float64ObservableGauge | ||
nonceGauge metric.Int64ObservableGauge | ||
|
||
// relayer stats | ||
relayerBalance *hashmap.Map[int, []relayerMetadata] | ||
relayerBalanceGauge metric.Float64ObservableGauge | ||
Comment on lines
+58
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The Do you want me to generate test cases for this new field or open a GitHub issue to track this task? |
||
} | ||
|
||
// TODO: unexport all methods. | ||
// nolint: cyclop | ||
func newOtelRecorder(meterHandler metrics.Handler) iOtelRecorder { | ||
otr := otelRecorder{ | ||
metrics: meterHandler, | ||
meter: meterHandler.Meter(meterName), | ||
stuckHeroes: hashmap.New[string, int64](), | ||
vPrice: hashmap.New[int, float64](), | ||
gasBalance: hashmap.New[int, float64](), | ||
td: hashmap.New[int, []tokenData](), | ||
submitters: hashmap.New[int, []submitterMetadata](), | ||
metrics: meterHandler, | ||
meter: meterHandler.Meter(meterName), | ||
stuckHeroes: hashmap.New[string, int64](), | ||
vPrice: hashmap.New[int, float64](), | ||
gasBalance: hashmap.New[int, float64](), | ||
td: hashmap.New[int, []tokenData](), | ||
submitters: hashmap.New[int, []submitterMetadata](), | ||
relayerBalance: hashmap.New[int, []relayerMetadata](), | ||
} | ||
|
||
var err error | ||
|
@@ -96,6 +106,10 @@ func newOtelRecorder(meterHandler metrics.Handler) iOtelRecorder { | |
log.Warnf("failed to create stuckHeroes gauge: %v", err) | ||
} | ||
|
||
if otr.relayerBalanceGauge, err = otr.meter.Float64ObservableGauge("relayer_balance"); err != nil { | ||
log.Warnf("failed to create relayerBalance gauge: %v", err) | ||
} | ||
Comment on lines
+109
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The creation of Do you want me to generate test cases for this gauge or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
|
||
// Register VPrice callback | ||
if _, err = otr.meter.RegisterCallback(otr.recordVpriceGauge, otr.vpriceGauge); err != nil { | ||
log.Warnf("failed to register callback for vprice metrics: %v", err) | ||
|
@@ -125,6 +139,11 @@ func newOtelRecorder(meterHandler metrics.Handler) iOtelRecorder { | |
log.Warnf("failed to register callback for bridge gas balance metrics: %v", err) | ||
} | ||
|
||
// The Relayer code of interest. | ||
if _, err = otr.meter.RegisterCallback(otr.recordRelayerBalance, otr.relayerBalanceGauge); err != nil { | ||
log.Warnf("failed to register callback for relayer balance metrics: %v", err) | ||
} | ||
Comment on lines
+143
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The registration of the Do you want me to generate test cases for this callback registration or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
|
||
return &otr | ||
} | ||
|
||
|
@@ -195,6 +214,7 @@ func (o *otelRecorder) RecordTokenBalance( | |
|
||
o.td.Set(chainID, td) | ||
} | ||
|
||
func (o *otelRecorder) recordTokenBalance( | ||
_ context.Context, | ||
observer metric.Observer, | ||
|
@@ -310,3 +330,36 @@ func (o *otelRecorder) recordSubmitterStats( | |
}) | ||
return nil | ||
} | ||
|
||
// RELAYER CODE. | ||
func (o *otelRecorder) RecordRelayerBalance(chainID int, relayer relayerMetadata) { | ||
relayerBalances, _ := o.relayerBalance.Get(chainID) | ||
relayerBalances = append(relayerBalances, relayer) | ||
o.relayerBalance.Set(chainID, relayerBalances) | ||
} | ||
Comment on lines
+335
to
+339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This method might overwrite existing relayer data for the same chainID. Consider using a map with relayer address as key instead of a slice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explain more?
Comment on lines
+335
to
+339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The Do you want me to generate test cases for this method or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
|
||
func (o *otelRecorder) recordRelayerBalance( | ||
_ context.Context, | ||
observer metric.Observer, | ||
) (err error) { | ||
if o.metrics == nil || o.relayerBalance == nil { | ||
return nil | ||
} | ||
|
||
o.relayerBalance.Range(func(chainID int, relayerBalances []relayerMetadata) bool { | ||
for _, relayer := range relayerBalances { | ||
observer.ObserveFloat64( | ||
o.relayerBalanceGauge, | ||
relayer.balance, | ||
metric.WithAttributes( | ||
attribute.Int(metrics.ChainID, chainID), | ||
attribute.String("relayer_address", relayer.address.String()), | ||
), | ||
) | ||
} | ||
|
||
return true | ||
}) | ||
|
||
return nil | ||
} | ||
Comment on lines
+341
to
+365
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for The Do you want me to generate test cases for this method or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package exporters | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"math/big" | ||
"net/http" | ||
"slices" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/lmittmann/w3/module/eth" | ||
"github.com/lmittmann/w3/w3types" | ||
rfqAPIModel "github.com/synapsecns/sanguine/services/rfq/api/model" | ||
) | ||
|
||
func (e *exporter) fetchRelayerBalances(ctx context.Context, url string) error { | ||
// Fetch relayer addresses | ||
quotes, err := e.fetchAllQuotes(ctx, url) | ||
if err != nil { | ||
return fmt.Errorf("could not fetch relayer addresses: %w", err) | ||
} | ||
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding test coverage for The function Would you like assistance in creating tests for this function? ToolsGitHub Check: codecov/patch
|
||
|
||
// chainIDs is a map of chain ID to relayer addresses | ||
chainIDToRelayers := make(map[int][]string) | ||
|
||
// Get all chain IDs | ||
for _, quote := range quotes { | ||
if !slices.Contains(chainIDToRelayers[quote.OriginChainID], quote.RelayerAddr) { | ||
chainIDToRelayers[quote.OriginChainID] = append(chainIDToRelayers[quote.OriginChainID], quote.RelayerAddr) | ||
} | ||
|
||
if !slices.Contains(chainIDToRelayers[quote.DestChainID], quote.RelayerAddr) { | ||
chainIDToRelayers[quote.DestChainID] = append(chainIDToRelayers[quote.DestChainID], quote.RelayerAddr) | ||
} | ||
} | ||
|
||
for chainID, relayers := range chainIDToRelayers { | ||
client, err := e.omnirpcClient.GetConfirmationsClient(ctx, chainID, 1) | ||
if err != nil { | ||
return fmt.Errorf("could not get confirmations client: %w", err) | ||
} | ||
|
||
var relayerBalances []*big.Int | ||
for range relayers { | ||
relayerBalances = append(relayerBalances, new(big.Int)) | ||
} | ||
|
||
var callsForCurrentChainID []w3types.Caller | ||
for i, relayer := range relayers { | ||
callsForCurrentChainID = append( | ||
callsForCurrentChainID, | ||
eth.Balance(common.HexToAddress(relayer), nil).Returns(relayerBalances[i]), | ||
) | ||
} | ||
|
||
_ = e.batchCalls(ctx, client, callsForCurrentChainID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Ignoring the error returned by e.batchCalls may lead to silent failures. Consider handling or logging this error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its fine |
||
|
||
for i, balanceOfRelayer := range relayerBalances { | ||
balanceFloat, _ := new(big.Float).SetInt(balanceOfRelayer).Float64() | ||
relayerMetadata := relayerMetadata{ | ||
address: common.HexToAddress(relayers[i]), | ||
balance: balanceFloat / params.Ether, | ||
} | ||
// the line of interest, where we record each relayer data for the respective chainID | ||
e.otelRecorder.RecordRelayerBalance(chainID, relayerMetadata) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e *exporter) fetchAllQuotes(ctx context.Context, url string) ([]rfqAPIModel.GetQuoteResponse, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get quotes: %w", err) | ||
} | ||
|
||
res, err := e.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get quotes: %w", err) | ||
} | ||
defer func() { | ||
_ = res.Body.Close() | ||
}() | ||
Comment on lines
+85
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider using |
||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read body: %w", err) | ||
} | ||
|
||
var quotes []rfqAPIModel.GetQuoteResponse | ||
err = json.Unmarshal(body, "es) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not unmarshal quotes: %w", err) | ||
} | ||
|
||
return quotes, nil | ||
} | ||
Comment on lines
+75
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding test coverage for The function Would you like assistance in creating tests for this function? ToolsGitHub Check: codecov/patch
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,6 @@ require ( | |
require ( | ||
dario.cat/mergo v1.0.0 // indirect | ||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Dependency on github.com/BurntSushi/toml removed. Verify if TOML parsing is still needed and replace with an alternative if necessary. |
||
github.com/BurntSushi/toml v1.4.0 // indirect | ||
github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3 // indirect | ||
github.com/KyleBanks/depth v1.2.1 // indirect | ||
github.com/Microsoft/go-winio v0.6.1 // indirect | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure test coverage for new error handling logic.
The error handling logic for fetching relayer balances in
collectMetrics
lacks test coverage. Consider adding tests to verify its behavior under different scenarios.Would you like assistance in creating tests for this error handling logic?
Tools
GitHub Check: codecov/patch