Skip to content

Commit 4f4ec55

Browse files
authored
Add metric reporting around the buy module (#125)
1 parent f700432 commit 4f4ec55

File tree

8 files changed

+65
-5
lines changed

8 files changed

+65
-5
lines changed

pkg/code/async/geyser/external_deposit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/code-payments/code-server/pkg/code/thirdparty"
3131
currency_lib "github.com/code-payments/code-server/pkg/currency"
3232
"github.com/code-payments/code-server/pkg/database/query"
33+
"github.com/code-payments/code-server/pkg/grpc/client"
3334
"github.com/code-payments/code-server/pkg/kin"
3435
push_lib "github.com/code-payments/code-server/pkg/push"
3536
"github.com/code-payments/code-server/pkg/retry"
@@ -635,6 +636,13 @@ func getPurchasesFromSwap(
635636
}
636637
}
637638

639+
recordBuyModulePurchaseCompletedEvent(
640+
ctx,
641+
client.DeviceType(onrampRecord.Platform),
642+
onrampRecord.CreatedAt,
643+
historyItem.BlockTime,
644+
)
645+
638646
res = append([]*transactionpb.ExchangeDataWithoutRate{
639647
{
640648
Currency: onrampRecord.Currency,

pkg/code/async/geyser/metrics.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"github.com/code-payments/code-server/pkg/grpc/client"
78
"github.com/code-payments/code-server/pkg/metrics"
89
"github.com/code-payments/code-server/pkg/solana"
910
)
@@ -20,6 +21,8 @@ const (
2021
externalDepositWorkerName = "ExternalDeposit"
2122
timelockStateWorkerName = "TimelockState"
2223
messagingWorkerName = "Messaging"
24+
25+
buyModulePurchaseCompletedEventName = "BuyModulePurchaseCompleted"
2326
)
2427

2528
func (p *service) metricsGaugeWorker(ctx context.Context) error {
@@ -135,3 +138,14 @@ func (p *service) recordBackupQueueStatusPollingEvent(ctx context.Context) {
135138
"current_size": count,
136139
})
137140
}
141+
142+
func recordBuyModulePurchaseCompletedEvent(ctx context.Context, deviceType client.DeviceType, purchaseInitiationTime time.Time, usdcDepositTime *time.Time) {
143+
kvs := map[string]interface{}{
144+
"total_latency": int(time.Since(purchaseInitiationTime) / time.Millisecond),
145+
"platform": deviceType.String(),
146+
}
147+
if usdcDepositTime != nil {
148+
kvs["swap_latency"] = int(time.Since(*usdcDepositTime) / time.Millisecond)
149+
}
150+
metrics.RecordEvent(ctx, buyModulePurchaseCompletedEventName, kvs)
151+
}

pkg/code/data/onramp/postgres/model.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type model struct {
2020
Id sql.NullInt64 `db:"id"`
2121

2222
Owner string `db:"owner"`
23+
Platform int `db:"platform"`
2324
Currency string `db:"currency"`
2425
Amount float64 `db:"amount"`
2526
Nonce uuid.UUID `db:"nonce"`
@@ -34,6 +35,7 @@ func toModel(obj *onramp.Record) (*model, error) {
3435

3536
return &model{
3637
Owner: obj.Owner,
38+
Platform: obj.Platform,
3739
Currency: obj.Currency,
3840
Amount: obj.Amount,
3941
Nonce: obj.Nonce,
@@ -45,6 +47,7 @@ func fromModel(obj *model) *onramp.Record {
4547
return &onramp.Record{
4648
Id: uint64(obj.Id.Int64),
4749
Owner: obj.Owner,
50+
Platform: obj.Platform,
4851
Currency: obj.Currency,
4952
Amount: obj.Amount,
5053
Nonce: obj.Nonce,
@@ -55,9 +58,9 @@ func fromModel(obj *model) *onramp.Record {
5558
func (m *model) dbPut(ctx context.Context, db *sqlx.DB) error {
5659
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
5760
query := `INSERT INTO ` + tableName + `
58-
(owner, currency, amount, nonce, created_at)
59-
VALUES ($1, $2, $3, $4, $5)
60-
RETURNING id, owner, currency, amount, nonce, created_at`
61+
(owner, platform, currency, amount, nonce, created_at)
62+
VALUES ($1, $2, $3, $4, $5, $6)
63+
RETURNING id, owner, platform, currency, amount, nonce, created_at`
6164

6265
if m.CreatedAt.IsZero() {
6366
m.CreatedAt = time.Now()
@@ -67,6 +70,7 @@ func (m *model) dbPut(ctx context.Context, db *sqlx.DB) error {
6770
ctx,
6871
query,
6972
m.Owner,
73+
m.Platform,
7074
m.Currency,
7175
m.Amount,
7276
m.Nonce,
@@ -81,7 +85,7 @@ func dbGet(ctx context.Context, db *sqlx.DB, nonce uuid.UUID) (*model, error) {
8185
res := &model{}
8286

8387
query := `SELECT
84-
id, owner, currency, amount, nonce, created_at
88+
id, owner, platform, currency, amount, nonce, created_at
8589
FROM ` + tableName + `
8690
WHERE nonce = $1
8791
LIMIT 1`

pkg/code/data/onramp/postgres/store_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
id SERIAL NOT NULL PRIMARY KEY,
2929
3030
owner TEXT NOT NULL,
31+
platform INTEGER NOT NULL,
3132
currency TEXT NOT NULL,
3233
amount numeric(18, 9) NOT NULL,
3334
nonce UUID NOT NULL,

pkg/code/data/onramp/purchase.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Record struct {
1313
Id uint64
1414

1515
Owner string
16+
Platform int // todo: can't use client.DeviceType due to import cycle
1617
Currency string
1718
Amount float64
1819
Nonce uuid.UUID
@@ -45,6 +46,7 @@ func (r *Record) Clone() Record {
4546
Id: r.Id,
4647

4748
Owner: r.Owner,
49+
Platform: r.Platform,
4850
Currency: r.Currency,
4951
Amount: r.Amount,
5052
Nonce: r.Nonce,
@@ -57,6 +59,7 @@ func (r *Record) CopyTo(dst *Record) {
5759
dst.Id = r.Id
5860

5961
dst.Owner = r.Owner
62+
dst.Platform = r.Platform
6063
dst.Currency = r.Currency
6164
dst.Amount = r.Amount
6265
dst.Nonce = r.Nonce

pkg/code/data/onramp/tests/tests.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111

1212
"github.com/code-payments/code-server/pkg/code/data/onramp"
13+
"github.com/code-payments/code-server/pkg/grpc/client"
1314
)
1415

1516
func RunTests(t *testing.T, s onramp.Store, teardown func()) {
@@ -34,6 +35,7 @@ func testHappyPath(t *testing.T, s onramp.Store) {
3435

3536
expected := &onramp.Record{
3637
Owner: "owner",
38+
Platform: int(client.DeviceTypeIOS),
3739
Currency: "cad",
3840
Amount: 50.01,
3941
Nonce: nonce,
@@ -58,6 +60,7 @@ func testHappyPath(t *testing.T, s onramp.Store) {
5860

5961
func assertEquivalentRecords(t *testing.T, obj1, obj2 *onramp.Record) {
6062
assert.Equal(t, obj1.Owner, obj2.Owner)
63+
assert.Equal(t, obj1.Platform, obj2.Platform)
6164
assert.Equal(t, obj1.Currency, obj2.Currency)
6265
assert.Equal(t, obj1.Amount, obj2.Amount)
6366
assert.Equal(t, obj1.Nonce, obj2.Nonce)

pkg/code/server/grpc/transaction/v2/metrics.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import (
44
"context"
55
"time"
66

7-
"github.com/code-payments/code-server/pkg/metrics"
87
"github.com/code-payments/code-server/pkg/code/common"
98
"github.com/code-payments/code-server/pkg/code/data/intent"
9+
currency_lib "github.com/code-payments/code-server/pkg/currency"
10+
"github.com/code-payments/code-server/pkg/grpc/client"
11+
"github.com/code-payments/code-server/pkg/metrics"
1012
)
1113

1214
const (
1315
userIntentCreatedEventName = "UserIntentCreated"
1416
privateUpgradeEventName = "PrivateTransferUpgraded"
1517
submitIntentLatencyBreakdownEventName = "SubmitIntentLatencyBreakdown"
1618
airdropEventName = "Airdrop"
19+
buyModulePurchaseInitiatedEventName = "BuyModulePurchaseInitiated"
1720
)
1821

1922
func recordUserIntentCreatedEvent(ctx context.Context, intentRecord *intent.Record) {
@@ -49,3 +52,11 @@ func recordAirdropEvent(ctx context.Context, owner *common.Account, airdropType
4952
"usd_value": usdValue,
5053
})
5154
}
55+
56+
func recordBuyModulePurchaseInitiatedEvent(ctx context.Context, currency currency_lib.Code, amount float64, deviceType client.DeviceType) {
57+
metrics.RecordEvent(ctx, buyModulePurchaseInitiatedEventName, map[string]interface{}{
58+
"currency": string(currency),
59+
"amount": amount,
60+
"platform": deviceType.String(),
61+
})
62+
}

pkg/code/server/grpc/transaction/v2/onramp.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func (s *transactionServer) DeclareFiatOnrampPurchaseAttempt(ctx context.Context
2323
log := s.log.WithField("method", "DeclareFiatOnrampPurchaseAttempt")
2424
log = client.InjectLoggingMetadata(ctx, log)
2525

26+
var deviceType client.DeviceType
27+
userAgent, err := client.GetUserAgent(ctx)
28+
if err == nil {
29+
deviceType = userAgent.DeviceType
30+
} else {
31+
deviceType = client.DeviceTypeUnknown
32+
}
33+
2634
owner, err := common.NewAccountFromProto(req.Owner)
2735
if err != nil {
2836
log.WithError(err).Warn("invalid owner account")
@@ -73,6 +81,7 @@ func (s *transactionServer) DeclareFiatOnrampPurchaseAttempt(ctx context.Context
7381

7482
record := &onramp.Record{
7583
Owner: owner.PublicKey().ToBase58(),
84+
Platform: int(deviceType),
7685
Currency: string(currency),
7786
Amount: amount,
7887
Nonce: nonce,
@@ -84,6 +93,13 @@ func (s *transactionServer) DeclareFiatOnrampPurchaseAttempt(ctx context.Context
8493
return nil, status.Error(codes.Internal, "")
8594
}
8695

96+
recordBuyModulePurchaseInitiatedEvent(
97+
ctx,
98+
currency,
99+
amount,
100+
deviceType,
101+
)
102+
87103
return &transactionpb.DeclareFiatOnrampPurchaseAttemptResponse{
88104
Result: transactionpb.DeclareFiatOnrampPurchaseAttemptResponse_OK,
89105
}, nil

0 commit comments

Comments
 (0)