Skip to content
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

metric fixes #2821

Merged
merged 23 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9cf433b
record oldest pending tx (fixes #2819)
trajan0x Jun 29, 2024
96a490e
refactor metrics address #2823, #2822, #2820 and #2819
trajan0x Jun 29, 2024
3ed2ea1
cleanup [goreleaser]
trajan0x Jun 29, 2024
4e2d5c8
address https://github.com/synapsecns/sanguine/pull/2821#discussion_r…
trajan0x Jun 29, 2024
dcb40c2
[goreleaser] log outersection
trajan0x Jun 29, 2024
ec3dc92
Revert "[goreleaser] log outersection"
trajan0x Jun 29, 2024
f5f1bfe
[goreleaser] log record
trajan0x Jun 29, 2024
85fbda0
record log [goreleaser]
trajan0x Jun 29, 2024
5cb5cde
Revert "record log [goreleaser]"
trajan0x Jun 29, 2024
beb2bd9
[goreleaser] update fork
trajan0x Jun 29, 2024
a82182e
Revert "[goreleaser] update fork"
trajan0x Jun 29, 2024
86c3d70
[goreleaser]
trajan0x Jun 29, 2024
cd249c5
Revert "[goreleaser]"
trajan0x Jun 29, 2024
689120a
Revert "[goreleaser] log record"
trajan0x Jun 29, 2024
41410d0
REVERT ME cleanup [goreleaser]
trajan0x Jun 29, 2024
65edc1a
Revert "REVERT ME cleanup [goreleaser]"
trajan0x Jun 29, 2024
5a49e7b
Reapply "REVERT ME cleanup [goreleaser]"
trajan0x Jun 29, 2024
dfabfe7
try things (in the arena) [goreleaser]
trajan0x Jun 29, 2024
e41b117
try MORE thigns [goreleaser]
trajan0x Jun 29, 2024
780c097
Revert "try MORE thigns [goreleaser]"
trajan0x Jun 29, 2024
2524735
Revert "try things (in the arena) [goreleaser]"
trajan0x Jun 29, 2024
aee3f7d
Revert "Reapply "REVERT ME cleanup [goreleaser]""
trajan0x Jun 29, 2024
3c88ae3
attach metric handler :( [goreleaser]
trajan0x Jun 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 4 additions & 72 deletions ethergo/submitter/chain_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/ethergo/submitter/db"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -66,8 +65,9 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
span.SetAttributes(attribute.Int("nonce", int(currentNonce)))

// record metrics for txes.
t.currentNonces.Set(uint32(chainID.Int64()), currentNonce)
t.numPendingTxes.Set(uint32(chainID.Int64()), calculatePendingTxes(txes, currentNonce))
t.otelRecorder.RecordNonceForChain(uint32(chainID.Int64()), currentNonce)
t.otelRecorder.RecordNumPendingTxes(uint32(chainID.Int64()), calculatePendingTxes(txes, currentNonce))
t.otelRecorder.RecordOldestPendingTx(uint32(chainID.Int64()), time.Since(fetchOldestPendingTx(txes, currentNonce)))

wg := &sync.WaitGroup{}

Expand All @@ -88,7 +88,7 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
if err != nil {
return fmt.Errorf("could not get gas balance: %w", err)
}
t.currentGasBalances.Set(uint32(chainID.Int64()), core.CopyBigInt(gasBalance))
t.otelRecorder.RecordGasBalanceForChain(uint32(chainID.Int64()), core.CopyBigInt(gasBalance))
span.SetAttributes(attribute.String("gas_balance", gasBalance.String()))

for i := range txes {
Expand Down Expand Up @@ -142,74 +142,6 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
return nil
}

// calculatePendingTxes calculates the number of pending txes in the queue.
func calculatePendingTxes(txes []db.TX, nonce uint64) int {
realPendingCount := 0
for _, tx := range txes {
// current nonce is going to be transaction count (nonces are offset by -1 since first nonce is 0)
// so we use equal to here
if tx.Nonce() >= nonce {
realPendingCount++
}
}

return realPendingCount

}

func (t *txSubmitterImpl) recordNumPending(_ context.Context, observer metric.Observer) (err error) {
if t.metrics == nil || t.numPendingGauge == nil || t.numPendingTxes == nil {
return nil
}

t.numPendingTxes.Range(func(chainID uint32, numPending int) bool {
opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, int(chainID)),
attribute.String("wallet", t.signer.Address().Hex()),
)
observer.ObserveInt64(t.numPendingGauge, int64(numPending), opts)

return true
})

return nil
}

func (t *txSubmitterImpl) recordNonces(_ context.Context, observer metric.Observer) (err error) {
if t.metrics == nil || t.nonceGauge == nil || t.currentNonces == nil {
return nil
}

t.currentNonces.Range(func(chainID uint32, nonce uint64) bool {
opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, int(chainID)),
attribute.String("wallet", t.signer.Address().Hex()),
)
observer.ObserveInt64(t.nonceGauge, int64(nonce), opts)
return true
})

return nil
}

func (t *txSubmitterImpl) recordBalance(_ context.Context, observer metric.Observer) (err error) {
if t.metrics == nil || t.gasBalanceGauge == nil {
return nil
}

t.currentGasBalances.Range(func(chainID uint32, gasPrice *big.Int) bool {
opts := metric.WithAttributes(
attribute.Int(metrics.ChainID, int(chainID)),
attribute.String("wallet", t.signer.Address().Hex()),
)

observer.ObserveFloat64(t.gasBalanceGauge, toFloat(gasPrice), opts)
return true
})

return nil
}

func toFloat(wei *big.Int) float64 {
// Convert wei to float64
weiFloat := new(big.Float).SetInt(wei)
Expand Down
23 changes: 23 additions & 0 deletions ethergo/submitter/db/mocks/service.go

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

2 changes: 2 additions & 0 deletions ethergo/submitter/db/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Service interface {
GetChainIDsByStatus(ctx context.Context, fromAddress common.Address, matchStatuses ...Status) (chainIDs []*big.Int, err error)
// DeleteTXS deletes txs that are older than a given duration.
DeleteTXS(ctx context.Context, maxAge time.Duration, matchStatuses ...Status) error
// GetDistinctChainIDs gets the distinct chain ids for all txs.
GetDistinctChainIDs(ctx context.Context) ([]*big.Int, error)
}

// TransactionFunc is a function that can be passed to DBTransaction.
Expand Down
20 changes: 20 additions & 0 deletions ethergo/submitter/db/txdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@
return txs, nil
}

// GetDistinctChainIDs gets a list of all chains that have been used.
func (s *Store) GetDistinctChainIDs(ctx context.Context) ([]*big.Int, error) {
var chainIDs []string
err := s.db.WithContext(ctx).
Model(&ETHTX{}).
Distinct(chainIDFieldName).
Pluck(chainIDFieldName, &chainIDs).Error
if err != nil {
return nil, err
}

Check warning on line 112 in ethergo/submitter/db/txdb/store.go

View check run for this annotation

Codecov / codecov/patch

ethergo/submitter/db/txdb/store.go#L104-L112

Added lines #L104 - L112 were not covered by tests

var result []*big.Int
for _, chainIDStr := range chainIDs {
chainID := new(big.Int)
chainID.SetString(chainIDStr, 10)
result = append(result, chainID)
}
return result, nil

Check warning on line 120 in ethergo/submitter/db/txdb/store.go

View check run for this annotation

Codecov / codecov/patch

ethergo/submitter/db/txdb/store.go#L114-L120

Added lines #L114 - L120 were not covered by tests
}
trajan0x marked this conversation as resolved.
Show resolved Hide resolved

// GetTXS returns all transactions for a given address on a given (or any) chain id that match a given status.
// there is a limit of 50 transactions per chain id. The limit does not make any guarantees about the number of nonces per chain.
// the submitter will get only the most recent tx submitted for each chain so this can be used for gas pricing.
Expand Down
5 changes: 5 additions & 0 deletions ethergo/submitter/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (t *TXSubmitterDBSuite) TestGetNonceForChainID() {
}
}
}

distinctChains, err := testDB.GetDistinctChainIDs(t.GetTestContext())
t.Require().NoError(err)

t.Require().Equal(len(t.testBackends), len(distinctChains))
})
}

Expand Down
10 changes: 10 additions & 0 deletions ethergo/submitter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,13 @@ func (t *txSubmitterImpl) GetNonce(parentCtx context.Context, chainID *big.Int,
func (t *txSubmitterImpl) CheckAndSetConfirmation(ctx context.Context, chainClient client.EVM, txes []db.TX) error {
return t.checkAndSetConfirmation(ctx, chainClient, txes)
}

// Outersection exports outersection for testing.
func Outersection(set, superset []*big.Int) []*big.Int {
return outersection(set, superset)
}

// MapToBigIntSlice exports mapToBigIntSlice for testing.
func MapToBigIntSlice[T any](m map[uint64]T) []*big.Int {
return mapToBigIntSlice(m)
}
Loading
Loading