From 8f4c3752e0fa242612b157558eccdd5362cce2da Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Thu, 22 Jun 2023 14:27:55 +0400 Subject: [PATCH] client: External statistic in accounting Signed-off-by: Evgenii Baidakov --- client/accounting.go | 15 ++++++++++++--- pool/accounting.go | 10 ++-------- pool/pool_aio_test.go | 8 ++++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/client/accounting.go b/client/accounting.go index 3b09ebd2..6e8baa41 100644 --- a/client/accounting.go +++ b/client/accounting.go @@ -2,12 +2,14 @@ package client import ( "context" + "time" v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting" "github.com/nspcc-dev/neofs-api-go/v2/refs" rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-sdk-go/accounting" + "github.com/nspcc-dev/neofs-sdk-go/stat" "github.com/nspcc-dev/neofs-sdk-go/user" ) @@ -36,13 +38,20 @@ func (x *PrmBalanceGet) SetAccount(id user.ID) { // Return errors: // - [ErrMissingAccount] // - [ErrMissingSigner] -func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) { +func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (res accounting.Decimal, err error) { + ts := time.Now() + defer func() { + c.sendStatistic(stat.MethodBalanceGet, time.Since(ts), err) + }() + switch { case !prm.accountSet: + err = ErrMissingAccount return accounting.Decimal{}, ErrMissingAccount } if c.prm.signer == nil { + err = ErrMissingSigner return accounting.Decimal{}, ErrMissingSigner } @@ -61,8 +70,7 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting. // init call context var ( - cc contextCall - res accounting.Decimal + cc contextCall ) c.initCallContext(&cc) @@ -90,6 +98,7 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting. // process call if !cc.processCall() { + err = cc.err return accounting.Decimal{}, cc.err } diff --git a/pool/accounting.go b/pool/accounting.go index 96db58fe..9ff246ce 100644 --- a/pool/accounting.go +++ b/pool/accounting.go @@ -2,7 +2,6 @@ package pool import ( "context" - "time" "github.com/nspcc-dev/neofs-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/client" @@ -12,15 +11,10 @@ import ( // // See details in [client.Client.BalanceGet]. func (p *Pool) BalanceGet(ctx context.Context, prm client.PrmBalanceGet) (accounting.Decimal, error) { - c, statUpdater, err := p.sdkClient() + c, _, err := p.sdkClient() if err != nil { return accounting.Decimal{}, err } - start := time.Now() - acc, err := c.BalanceGet(ctx, prm) - statUpdater.incRequests(time.Since(start), methodBalanceGet) - statUpdater.updateErrorRate(err) - - return acc, err + return c.BalanceGet(ctx, prm) } diff --git a/pool/pool_aio_test.go b/pool/pool_aio_test.go index c9b87dfe..997e699a 100644 --- a/pool/pool_aio_test.go +++ b/pool/pool_aio_test.go @@ -27,6 +27,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/session" + "github.com/nspcc-dev/neofs-sdk-go/stat" "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/nspcc-dev/neofs-sdk-go/waiter" "github.com/stretchr/testify/require" @@ -115,12 +116,15 @@ func TestPoolInterfaceWithAIO(t *testing.T) { nodeAddr := "grpc://localhost:8080" + agg := stat.NewAggregator() + opts := InitParameters{ signer: signer, nodeParams: []NodeParam{ {1, nodeAddr, 1}, }, clientRebalanceInterval: 30 * time.Second, + statisticCallback: agg.Collect, } pool, err := NewPool(opts) @@ -140,8 +144,8 @@ func TestPoolInterfaceWithAIO(t *testing.T) { _, err = pool.BalanceGet(ctx, cmd) require.NoError(t, err) - stat := pool.Statistic() - nodeStat, err := stat.Node(nodeAddr) + st := pool.Statistic() + nodeStat, err := st.Node(nodeAddr) require.NoError(t, err) require.Equal(t, uint64(1), nodeStat.methods[methodBalanceGet].allRequests)