Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat(BUX-567): collecting stats in a cronJob
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Feb 12, 2024
1 parent 1b286c1 commit 0c9e6b7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
46 changes: 44 additions & 2 deletions cron_job_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func taskSyncTransactions(ctx context.Context, client *Client) error {
}

func taskCalculateMetrics(ctx context.Context, client *Client) error {
metrics, enabled := client.Metrics()
m, enabled := client.Metrics()
if !enabled {
return errors.New("metrics are not enabled")
}
Expand All @@ -97,7 +97,49 @@ func taskCalculateMetrics(ctx context.Context, client *Client) error {
if xpubsCount, err := getXPubsCount(ctx, nil, nil, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting xpubs count")
} else {
metrics.Stats.XPub.Set(float64(xpubsCount))
m.Stats.XPub.Set(float64(xpubsCount))
}

if utxosCount, err := getUtxosCount(ctx, nil, nil, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting utxos count")
} else {
m.Stats.Utxo.Set(float64(utxosCount))
}

if paymailsCount, err := getPaymailAddressesCount(ctx, nil, nil, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting paymails count")
} else {
m.Stats.Paymail.Set(float64(paymailsCount))
}

if destinationsCount, err := getDestinationsCount(ctx, nil, nil, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting destinations count")
} else {
m.Stats.Destination.Set(float64(destinationsCount))
}

if accessKeysCount, err := getAccessKeysCount(ctx, nil, nil, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting access keys count")
} else {
m.Stats.AccessKey.Set(float64(accessKeysCount))
}

inTransactionsFilter := map[string]interface{}{
"direction": TransactionDirectionIn,
}
if transactionsCount, err := getTransactionsCount(ctx, nil, &inTransactionsFilter, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting transactions count")
} else {
m.Stats.TransactionIn.Set(float64(transactionsCount))
}

outTransactionsFilter := map[string]interface{}{
"direction": TransactionDirectionOut,
}
if transactionsCount, err := getTransactionsCount(ctx, nil, &outTransactionsFilter, modelOpts...); err != nil {
client.options.logger.Error().Err(err).Msg("error getting transactions count")
} else {
m.Stats.TransactionOut.Set(float64(transactionsCount))
}

return nil
Expand Down
10 changes: 9 additions & 1 deletion metrics/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ const (
cronLastExecutionGaugeName = domainPrefix + "cron_last_execution_gauge"
)

const xpubGaugeName = domainPrefix + "xpub_gauge"
const (
xpubGaugeName = domainPrefix + "xpub_gauge"
utxoGaugeName = domainPrefix + "utxo_gauge"
transactionInGaugeName = domainPrefix + "transaction_in_gauge"
transactionOutGaugeName = domainPrefix + "transaction_out_gauge"
paymailGaugeName = domainPrefix + "paymail_gauge"
destinationGaugeName = domainPrefix + "destination_gauge"
accessKeyGaugeName = domainPrefix + "access_key_gauge"
)
16 changes: 14 additions & 2 deletions metrics/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ package metrics

// Stats is a struct that contains all the gauges that are used to track the calculated stats of the application
type Stats struct {
XPub GaugeInterface
XPub GaugeInterface
Utxo GaugeInterface
TransactionIn GaugeInterface
TransactionOut GaugeInterface
Paymail GaugeInterface
Destination GaugeInterface
AccessKey GaugeInterface
}

func registerStats(collector Collector) Stats {
return Stats{
XPub: collector.RegisterGauge(xpubGaugeName),
XPub: collector.RegisterGauge(xpubGaugeName),
Utxo: collector.RegisterGauge(utxoGaugeName),
TransactionIn: collector.RegisterGauge(transactionInGaugeName),
TransactionOut: collector.RegisterGauge(transactionOutGaugeName),
Paymail: collector.RegisterGauge(paymailGaugeName),
Destination: collector.RegisterGauge(destinationGaugeName),
AccessKey: collector.RegisterGauge(accessKeyGaugeName),
}
}

0 comments on commit 0c9e6b7

Please sign in to comment.