Skip to content

Commit

Permalink
add client latency requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed Jun 10, 2024
1 parent 8043c7b commit d1f322b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/solana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (c *Client) latency(name string) func() {
}

func (c *Client) Balance(addr solana.PublicKey) (uint64, error) {
defer c.latency("balance")()

ctx, cancel := context.WithTimeout(context.Background(), c.contextDuration)
defer cancel()

Expand All @@ -105,6 +107,8 @@ func (c *Client) SlotHeight() (uint64, error) {
}

func (c *Client) SlotHeightWithCommitment(commitment rpc.CommitmentType) (uint64, error) {
defer c.latency("slot_height")()

ctx, cancel := context.WithTimeout(context.Background(), c.contextDuration)
defer cancel()
v, err, _ := c.requestGroup.Do("GetSlotHeight", func() (interface{}, error) {
Expand All @@ -114,13 +118,17 @@ func (c *Client) SlotHeightWithCommitment(commitment rpc.CommitmentType) (uint64
}

func (c *Client) GetAccountInfoWithOpts(ctx context.Context, addr solana.PublicKey, opts *rpc.GetAccountInfoOpts) (*rpc.GetAccountInfoResult, error) {
defer c.latency("account_info")()

ctx, cancel := context.WithTimeout(ctx, c.contextDuration)
defer cancel()
opts.Commitment = c.commitment // overrides passed in value - use defined client commitment type
return c.rpc.GetAccountInfoWithOpts(ctx, addr, opts)
}

func (c *Client) LatestBlockhash() (*rpc.GetLatestBlockhashResult, error) {
defer c.latency("latest_blockhash")()

ctx, cancel := context.WithTimeout(context.Background(), c.contextDuration)
defer cancel()

Expand All @@ -131,6 +139,8 @@ func (c *Client) LatestBlockhash() (*rpc.GetLatestBlockhashResult, error) {
}

func (c *Client) ChainID() (string, error) {
defer c.latency("chain_id")()

ctx, cancel := context.WithTimeout(context.Background(), c.contextDuration)
defer cancel()
v, err, _ := c.requestGroup.Do("GetGenesisHash", func() (interface{}, error) {
Expand All @@ -157,6 +167,8 @@ func (c *Client) ChainID() (string, error) {
}

func (c *Client) GetFeeForMessage(msg string) (uint64, error) {
defer c.latency("fee_for_message")()

// msg is base58 encoded data

ctx, cancel := context.WithTimeout(context.Background(), c.contextDuration)
Expand All @@ -174,6 +186,8 @@ func (c *Client) GetFeeForMessage(msg string) (uint64, error) {

// https://docs.solana.com/developing/clients/jsonrpc-api#getsignaturestatuses
func (c *Client) SignatureStatuses(ctx context.Context, sigs []solana.Signature) ([]*rpc.SignatureStatusesResult, error) {
defer c.latency("signature_statuses")()

ctx, cancel := context.WithTimeout(ctx, c.contextDuration)
defer cancel()

Expand All @@ -192,6 +206,8 @@ func (c *Client) SignatureStatuses(ctx context.Context, sigs []solana.Signature)
// https://docs.solana.com/developing/clients/jsonrpc-api#simulatetransaction
// opts - (optional) use `nil` to use defaults
func (c *Client) SimulateTx(ctx context.Context, tx *solana.Transaction, opts *rpc.SimulateTransactionOpts) (*rpc.SimulateTransactionResult, error) {
defer c.latency("simulate_tx")()

ctx, cancel := context.WithTimeout(ctx, c.contextDuration)
defer cancel()

Expand All @@ -215,6 +231,8 @@ func (c *Client) SimulateTx(ctx context.Context, tx *solana.Transaction, opts *r
}

func (c *Client) SendTx(ctx context.Context, tx *solana.Transaction) (solana.Signature, error) {
defer c.latency("send_tx")()

ctx, cancel := context.WithTimeout(ctx, c.txTimeout)
defer cancel()

Expand All @@ -235,6 +253,7 @@ func (c *Client) GetLatestBlock() (*rpc.GetBlockResult, error) {
}

// get block based on slot
defer c.latency("latest_block")()
ctx, cancel := context.WithTimeout(context.Background(), c.txTimeout)
defer cancel()
v, err, _ := c.requestGroup.Do("GetBlockWithOpts", func() (interface{}, error) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/solana/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
"github.com/google/uuid"
"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/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/monitor"
)

func TestClient_Reader_Integration(t *testing.T) {
Expand Down Expand Up @@ -292,3 +295,21 @@ func TestClient_SendTxDuplicates_Integration(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(5_000), initBal-endBal)
}

func TestClientLatency(t *testing.T) {
c := Client{}
v := 100
n := t.Name() + uuid.NewString()
f := func() {
defer c.latency(n)()
time.Sleep(time.Duration(v) * time.Millisecond)
}
f()
g, err := monitor.GetClientLatency(n, c.url)
require.NoError(t, err)
val := testutil.ToFloat64(g)

// check within expected range
assert.GreaterOrEqual(t, val, float64(v))
assert.LessOrEqual(t, val, float64(v)*1.05)
}
7 changes: 7 additions & 0 deletions pkg/solana/monitor/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ func SetClientLatency(d time.Duration, request, url string) {
"url": url,
}).Set(float64(d.Milliseconds()))
}

func GetClientLatency(request, url string) (prometheus.Gauge, error) {
return promClientReq.GetMetricWith(prometheus.Labels{
"request": request,
"url": url,
})
}

0 comments on commit d1f322b

Please sign in to comment.