Skip to content

Commit f2a8238

Browse files
committed
Cleanup metrics and add Airdroper balance metric poller
1 parent 6f6beec commit f2a8238

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

pkg/code/async/account/config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package async_account
22

3-
// todo: setup configs
3+
import (
4+
"github.com/code-payments/code-server/pkg/config"
5+
"github.com/code-payments/code-server/pkg/config/env"
6+
)
47

58
const (
69
envConfigPrefix = "ACCOUNT_SERVICE_"
10+
11+
AirdropperOwnerPublicKeyEnvName = envConfigPrefix + "AIRDROPPER_OWNER_PUBLIC_KEY"
12+
defaultAirdropperOwnerPublicKey = "invalid" // Ensure something valid is set
713
)
814

915
type conf struct {
16+
airdropperOwnerPublicKey config.String
1017
}
1118

1219
// ConfigProvider defines how config values are pulled
@@ -15,6 +22,8 @@ type ConfigProvider func() *conf
1522
// WithEnvConfigs returns configuration pulled from environment variables
1623
func WithEnvConfigs() ConfigProvider {
1724
return func() *conf {
18-
return &conf{}
25+
return &conf{
26+
airdropperOwnerPublicKey: env.NewStringConfig(AirdropperOwnerPublicKeyEnvName, defaultAirdropperOwnerPublicKey),
27+
}
1928
}
2029
}

pkg/code/async/account/metrics.go

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

7+
"github.com/code-payments/code-server/pkg/code/balance"
78
"github.com/code-payments/code-server/pkg/metrics"
89
)
910

1011
const (
1112
giftCardWorkerEventName = "GiftCardWorkerPollingCheck"
1213
swapRetryWorkerEventName = "SwapRetryWorkerPollingCheck"
14+
15+
airdropperBalanceEventName = "AirdropperBalance"
1316
)
1417

1518
func (p *service) metricsGaugeWorker(ctx context.Context) error {
@@ -23,6 +26,7 @@ func (p *service) metricsGaugeWorker(ctx context.Context) error {
2326
start := time.Now()
2427

2528
p.recordBackupQueueStatusPollingEvent(ctx)
29+
p.recordAidropAccountBalance(ctx)
2630

2731
delay = time.Second - time.Since(start)
2832
}
@@ -36,11 +40,18 @@ func (p *service) recordBackupQueueStatusPollingEvent(ctx context.Context) {
3640
"queue_size": count,
3741
})
3842
}
43+
}
3944

40-
count, err = p.data.GetAccountInfoCountRequiringSwapRetry(ctx)
45+
func (p *service) recordAidropAccountBalance(ctx context.Context) {
46+
if p.airdropper == nil {
47+
return
48+
}
49+
50+
quarks, err := balance.CalculateFromCache(ctx, p.data, p.airdropper.Vault)
4151
if err == nil {
42-
metrics.RecordEvent(ctx, swapRetryWorkerEventName, map[string]interface{}{
43-
"queue_size": count,
52+
metrics.RecordEvent(ctx, airdropperBalanceEventName, map[string]interface{}{
53+
"owner": p.airdropper.VaultOwner.PublicKey().ToBase58(),
54+
"balance": quarks,
4455
})
4556
}
4657
}

pkg/code/async/account/service.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,33 @@ import (
77
"github.com/sirupsen/logrus"
88

99
"github.com/code-payments/code-server/pkg/code/async"
10+
"github.com/code-payments/code-server/pkg/code/common"
1011
code_data "github.com/code-payments/code-server/pkg/code/data"
1112
)
1213

1314
type service struct {
1415
log *logrus.Entry
1516
conf *conf
1617
data code_data.Provider
18+
19+
airdropper *common.TimelockAccounts
1720
}
1821

1922
func New(data code_data.Provider, configProvider ConfigProvider) async.Service {
20-
return &service{
23+
ctx := context.Background()
24+
25+
p := &service{
2126
log: logrus.StandardLogger().WithField("service", "account"),
2227
conf: configProvider(),
2328
data: data,
2429
}
30+
31+
airdropper := p.conf.airdropperOwnerPublicKey.Get(ctx)
32+
if len(airdropper) > 0 && airdropper != defaultAirdropperOwnerPublicKey {
33+
p.mustLoadAirdropper(ctx)
34+
}
35+
36+
return p
2537
}
2638

2739
func (p *service) Start(ctx context.Context, interval time.Duration) error {
@@ -33,16 +45,6 @@ func (p *service) Start(ctx context.Context, interval time.Duration) error {
3345
}
3446
}()
3547

36-
// todo: the open code protocol needs to get the push token from the implementing app
37-
/*
38-
go func() {
39-
err := p.swapRetryWorker(ctx, interval)
40-
if err != nil && err != context.Canceled {
41-
p.log.WithError(err).Warn("swap retry processing loop terminated unexpectedly")
42-
}
43-
}()
44-
*/
45-
4648
go func() {
4749
err := p.metricsGaugeWorker(ctx)
4850
if err != nil && err != context.Canceled {
@@ -55,3 +57,33 @@ func (p *service) Start(ctx context.Context, interval time.Duration) error {
5557
return ctx.Err()
5658
}
5759
}
60+
61+
func (p *service) mustLoadAirdropper(ctx context.Context) {
62+
log := p.log.WithFields(logrus.Fields{
63+
"method": "mustLoadAirdropper",
64+
"key": p.conf.airdropperOwnerPublicKey.Get(ctx),
65+
})
66+
67+
err := func() error {
68+
vaultRecord, err := p.data.GetKey(ctx, p.conf.airdropperOwnerPublicKey.Get(ctx))
69+
if err != nil {
70+
return err
71+
}
72+
73+
ownerAccount, err := common.NewAccountFromPrivateKeyString(vaultRecord.PrivateKey)
74+
if err != nil {
75+
return err
76+
}
77+
78+
timelockAccounts, err := ownerAccount.GetTimelockAccounts(common.CodeVmAccount, common.CoreMintAccount)
79+
if err != nil {
80+
return err
81+
}
82+
83+
p.airdropper = timelockAccounts
84+
return nil
85+
}()
86+
if err != nil {
87+
log.WithError(err).Fatal("failure loading account")
88+
}
89+
}

pkg/code/server/transaction/metrics.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313

1414
const (
1515
userIntentCreatedEventName = "UserIntentCreated"
16-
privateUpgradeEventName = "PrivateTransferUpgraded"
1716
submitIntentLatencyBreakdownEventName = "SubmitIntentLatencyBreakdown"
18-
airdropEventName = "Airdrop"
19-
buyModulePurchaseInitiatedEventName = "BuyModulePurchaseInitiated"
17+
18+
airdropEventName = "Airdrop"
19+
20+
buyModulePurchaseInitiatedEventName = "BuyModulePurchaseInitiated"
2021
)
2122

2223
func recordUserIntentCreatedEvent(ctx context.Context, intentRecord *intent.Record) {
@@ -26,15 +27,6 @@ func recordUserIntentCreatedEvent(ctx context.Context, intentRecord *intent.Reco
2627
})
2728
}
2829

29-
func recordPrivacyUpgradedEvent(ctx context.Context, intentRecord *intent.Record, numUpgraded int) {
30-
upgradeTimeInMs := time.Since(intentRecord.CreatedAt) / time.Millisecond
31-
metrics.RecordEvent(ctx, privateUpgradeEventName, map[string]interface{}{
32-
"intent": intentRecord.IntentId,
33-
"num_upgraded": numUpgraded,
34-
"time_to_upgrade_ms": int(upgradeTimeInMs),
35-
})
36-
}
37-
3830
func recordSubmitIntentLatencyBreakdownEvent(ctx context.Context, section string, latency time.Duration, actionCount int, intentType string) {
3931
latencyInMs := latency / time.Millisecond
4032
metrics.RecordEvent(ctx, submitIntentLatencyBreakdownEventName, map[string]interface{}{

0 commit comments

Comments
 (0)