Skip to content

Commit

Permalink
Add ZebPay linking metrics (#2235)
Browse files Browse the repository at this point in the history
* feat: add zebpay count linking metric

* feat: add zebpay count linking metric

* rename metrics

* feat: add zebpay count linking metric

* rename test var
  • Loading branch information
clD11 authored Nov 30, 2023
1 parent 2e4988e commit a442e48
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 124 deletions.
9 changes: 4 additions & 5 deletions services/wallet/controllers_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,14 @@ func LinkZebPayDepositAccountV3(s *Service) func(w http.ResponseWriter, r *http.
})
}

xalr := &ZebPayLinkingRequest{}
if err := inputs.DecodeAndValidateReader(ctx, xalr, r.Body); err != nil {
zplReq := &ZebPayLinkingRequest{}
if err := inputs.DecodeAndValidateReader(ctx, zplReq, r.Body); err != nil {
return HandleErrorsZebPay(err)
}

country, err := s.LinkZebPayWallet(ctx, *id.UUID(), xalr.VerificationToken)
country, err := s.LinkZebPayWallet(ctx, *id.UUID(), zplReq.VerificationToken)
if err != nil {
l.Error().Err(err).Str("paymentID", id.String()).
Msg("failed to link wallet")
l.Error().Err(err).Str("paymentID", id.String()).Msg("failed to link wallet")
switch {
case errors.Is(err, errorutils.ErrInvalidCountry):
return handlers.WrapError(err, "region not supported", http.StatusBadRequest)
Expand Down
51 changes: 41 additions & 10 deletions services/wallet/controllers_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestLinkBitFlyerWalletV3(t *testing.T) {
}`, tokenString)),
)
mockReputation = mockreputation.NewMockClient(mockCtrl)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.LinkBitFlyerDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestLinkGeminiWalletV3RelinkBadRegion(t *testing.T) {
"recipient_id": "%s"
}`, linkingInfo, idTo)),
)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.LinkGeminiDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestLinkGeminiWalletV3RelinkBadRegion(t *testing.T) {
"DELETE",
fmt.Sprintf("/v3/wallet/gemini/%s/claim", idFrom), nil)

s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.DisconnectCustodianLinkV3(s)
rw = httptest.NewRecorder()

Expand Down Expand Up @@ -552,7 +552,7 @@ func TestLinkGeminiWalletV3FirstLinking(t *testing.T) {
"recipient_id": "%s"
}`, linkingInfo, idTo)),
)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.LinkGeminiDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand Down Expand Up @@ -670,7 +670,12 @@ func TestLinkZebPayWalletV3_InvalidKyc(t *testing.T) {
},
})

s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
mtc = &mockMtc{
fnLinkFailureZP: func(cc string) {
assert.Equal(t, "IN", cc)
},
}
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, mtc)
handler = wallet.LinkZebPayDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand All @@ -680,7 +685,11 @@ func TestLinkZebPayWalletV3_InvalidKyc(t *testing.T) {
ctx = context.WithValue(ctx, appctx.ZebPayLinkingKeyCTXKey, base64.StdEncoding.EncodeToString(secret))

linkingInfo, err := jwt.Signed(sig).Claims(map[string]interface{}{
"accountId": accountID, "depositId": idTo, "iat": time.Now().Unix(), "exp": time.Now().Add(5 * time.Second).Unix(),
"accountId": accountID,
"depositId": idTo,
"countryCode": "IN",
"iat": time.Now().Unix(),
"exp": time.Now().Add(5 * time.Second).Unix(),
}).CompactSerialize()
if err != nil {
panic(err)
Expand Down Expand Up @@ -742,7 +751,13 @@ func TestLinkZebPayWalletV3(t *testing.T) {
// setup mock clients
mockReputationClient = mockreputation.NewMockClient(mockCtrl)

s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
mtc = &mockMtc{
fnLinkSuccessZP: func(cc string) {
assert.Equal(t, "IN", cc)
},
}

s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, mtc)
handler = wallet.LinkZebPayDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand Down Expand Up @@ -867,7 +882,7 @@ func TestLinkGeminiWalletV3(t *testing.T) {
"recipient_id": "%s"
}`, linkingInfo, idTo)),
)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.LinkGeminiDepositAccountV3(s)
rw = httptest.NewRecorder()
)
Expand Down Expand Up @@ -971,8 +986,7 @@ func TestDisconnectCustodianLinkV3(t *testing.T) {
r = httptest.NewRequest(
"DELETE",
fmt.Sprintf("/v3/wallet/gemini/%s/claim", idFrom), nil)

s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil)
s, _ = wallet.InitService(datastore, nil, nil, nil, nil, nil, nil)
handler = wallet.DisconnectCustodianLinkV3(s)
w = httptest.NewRecorder()
)
Expand Down Expand Up @@ -1028,3 +1042,20 @@ func mockSQLCustodianLink(mock sqlmock.Sqlmock, custodian string) {
mock.ExpectQuery("^select(.+) from wallet_custodian(.+)").
WillReturnRows(clRow)
}

type mockMtc struct {
fnLinkSuccessZP func(cc string)
fnLinkFailureZP func(cc string)
}

func (m *mockMtc) LinkSuccessZP(cc string) {
if m.fnLinkSuccessZP != nil {
m.fnLinkSuccessZP(cc)
}
}

func (m *mockMtc) LinkFailureZP(cc string) {
if m.fnLinkFailureZP != nil {
m.fnLinkFailureZP(cc)
}
}
26 changes: 11 additions & 15 deletions services/wallet/controllers_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"encoding/json"
"errors"
"fmt"
errorutils "github.com/brave-intl/bat-go/libs/errors"
"net/http"
"net/http/httptest"
"testing"

errorutils "github.com/brave-intl/bat-go/libs/errors"

"github.com/brave-intl/bat-go/libs/clients"

"github.com/brave-intl/bat-go/libs/altcurrency"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (suite *WalletControllersV4TestSuite) TestCreateBraveWalletV4_Success() {
Validate(gomock.Any(), geoCountry).
Return(true, nil)

service, err := wallet.InitService(storage, nil, reputationClient, nil, locationValidator, backoff.Retry)
service, err := wallet.InitService(storage, nil, reputationClient, nil, locationValidator, backoff.Retry, nil)
suite.Require().NoError(err)

router := chi.NewRouter()
Expand Down Expand Up @@ -119,7 +120,7 @@ func (suite *WalletControllersV4TestSuite) TestCreateBraveWalletV4_GeoCountryDis
Validate(gomock.Any(), gomock.Any()).
Return(false, nil)

service, err := wallet.InitService(nil, nil, nil, nil, locationValidator, backoff.Retry)
service, err := wallet.InitService(nil, nil, nil, nil, locationValidator, backoff.Retry, nil)
suite.Require().NoError(err)

router := chi.NewRouter()
Expand Down Expand Up @@ -171,7 +172,7 @@ func (suite *WalletControllersV4TestSuite) TestCreateBraveWalletV4_WalletAlready
Validate(gomock.Any(), geoCountry).
Return(true, nil)

service, err := wallet.InitService(storage, nil, nil, nil, locationValidator, nil)
service, err := wallet.InitService(storage, nil, nil, nil, locationValidator, nil, nil)
suite.Require().NoError(err)

router := chi.NewRouter()
Expand Down Expand Up @@ -242,7 +243,7 @@ func (suite *WalletControllersV4TestSuite) TestCreateBraveWalletV4_ReputationCal
Validate(gomock.Any(), gomock.Any()).
Return(true, nil)

service, err := wallet.InitService(storage, nil, reputationClient, nil, locationValidator, backoff.Retry)
service, err := wallet.InitService(storage, nil, reputationClient, nil, locationValidator, backoff.Retry, nil)
suite.Require().NoError(err)

router := chi.NewRouter()
Expand Down Expand Up @@ -292,8 +293,7 @@ func (suite *WalletControllersV4TestSuite) TestUpdateBraveWalletV4_Success() {
UpsertReputationSummary(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)

service, err := wallet.InitService(storage, nil, reputationClient, nil,
nil, backoff.Retry)
service, err := wallet.InitService(storage, nil, reputationClient, nil, nil, backoff.Retry, nil)
suite.Require().NoError(err)

// create rewards wallet with public key
Expand Down Expand Up @@ -343,8 +343,7 @@ func (suite *WalletControllersV4TestSuite) TestUpdateBraveWalletV4_VerificationM
storage, err := wallet.NewWritablePostgres("", false, "")
suite.NoError(err)

service, err := wallet.InitService(storage, nil, nil, nil,
nil, backoff.Retry)
service, err := wallet.InitService(storage, nil, nil, nil, nil, backoff.Retry, nil)
suite.Require().NoError(err)

publicKey, privateKey, err := httpsignature.GenerateEd25519Key(nil)
Expand Down Expand Up @@ -382,8 +381,7 @@ func (suite *WalletControllersV4TestSuite) TestUpdateBraveWalletV4_PaymentIDMism
storage, err := wallet.NewWritablePostgres("", false, "")
suite.NoError(err)

service, err := wallet.InitService(storage, nil, nil, nil,
nil, backoff.Retry)
service, err := wallet.InitService(storage, nil, nil, nil, nil, backoff.Retry, nil)
suite.Require().NoError(err)

// create rewards wallet with public key
Expand Down Expand Up @@ -450,8 +448,7 @@ func (suite *WalletControllersV4TestSuite) TestUpdateBraveWalletV4_GeoCountryAlr
UpsertReputationSummary(gomock.Any(), gomock.Any(), gomock.Any()).
Return(errorBundle)

service, err := wallet.InitService(storage, nil, reputationClient, nil,
nil, backoff.Retry)
service, err := wallet.InitService(storage, nil, reputationClient, nil, nil, backoff.Retry, nil)
suite.Require().NoError(err)

// create rewards wallet with public key
Expand Down Expand Up @@ -516,8 +513,7 @@ func (suite *WalletControllersV4TestSuite) TestUpdateBraveWalletV4_ReputationCal
UpsertReputationSummary(gomock.Any(), gomock.Any(), gomock.Any()).
Return(errReputation)

service, err := wallet.InitService(storage, nil, reputationClient, nil,
nil, backoff.Retry)
service, err := wallet.InitService(storage, nil, reputationClient, nil, nil, backoff.Retry, nil)
suite.Require().NoError(err)

// create rewards wallet with public key
Expand Down
8 changes: 4 additions & 4 deletions services/wallet/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (suite *WalletControllersTestSuite) TestBalanceV3() {
mockCtrl := gomock.NewController(suite.T())
defer mockCtrl.Finish()

service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil)
service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil, nil)

w1 := suite.NewWallet(service, "uphold")

Expand Down Expand Up @@ -163,7 +163,7 @@ func (suite *WalletControllersTestSuite) TestLinkWalletV3() {
mockCtrl := gomock.NewController(suite.T())
defer mockCtrl.Finish()

service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil)
service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil, nil)

w1 := suite.NewWallet(service, "uphold")
w2 := suite.NewWallet(service, "uphold")
Expand Down Expand Up @@ -319,7 +319,7 @@ func (suite *WalletControllersTestSuite) TestCreateBraveWalletV3() {
pg, _, err := wallet.NewPostgres()
suite.Require().NoError(err, "Failed to get postgres connection")

service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil)
service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil, nil)

publicKey, privKey, err := httpsignature.GenerateEd25519Key(nil)

Expand Down Expand Up @@ -362,7 +362,7 @@ func (suite *WalletControllersTestSuite) TestCreateUpholdWalletV3() {
pg, _, err := wallet.NewPostgres()
suite.Require().NoError(err, "Failed to get postgres connection")

service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil)
service, _ := wallet.InitService(pg, nil, nil, nil, nil, nil, nil)

publicKey, privKey, err := httpsignature.GenerateEd25519Key(nil)

Expand Down
44 changes: 44 additions & 0 deletions services/wallet/metric/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package metric

import (
"github.com/prometheus/client_golang/prometheus"
)

const (
status = "status"
countryCode = "country_code"
)

type Metric struct {
cntLinkZP *prometheus.CounterVec
}

// New returns a new metric.Metric.
// New panics if it cannot register any of the metrics.
func New() *Metric {
clzp := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "count_link_zebpay",
Help: "Counts the number of successful and failed ZebPay linkings partitioned by country code",
},
[]string{status, countryCode},
)
prometheus.MustRegister(clzp)

return &Metric{cntLinkZP: clzp}
}

func (m *Metric) LinkSuccessZP(cc string) {
const success = "success"
m.cntLinkZP.With(prometheus.Labels{
status: success,
countryCode: cc,
}).Inc()
}

func (m *Metric) LinkFailureZP(cc string) {
const failure = "failure"
m.cntLinkZP.With(prometheus.Labels{
status: failure,
countryCode: cc,
}).Inc()
}
6 changes: 6 additions & 0 deletions services/wallet/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ package model
import "errors"

var ErrNoWalletCustodian = errors.New("model: no linked wallet custodian")

type Error string

func (e Error) Error() string {
return string(e)
}
Loading

0 comments on commit a442e48

Please sign in to comment.