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

Commit

Permalink
feat(BUX-467): adjustments after with-grafana tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Feb 12, 2024
1 parent 263fc95 commit 36a09b8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 37 deletions.
10 changes: 5 additions & 5 deletions cron_job_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,31 @@ 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 {
m.Stats.XPub.Set(float64(xpubsCount))
m.SetXPubCount(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))
m.SetUtxoCount(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))
m.SetPaymailCount(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))
m.SetDestinationCount(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))
m.SetAccessKeyCount(accessKeysCount)
}

return nil
Expand Down
17 changes: 12 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import (

// Metrics is a struct that contains all the metrics that are used to track in the package
type Metrics struct {
collector Collector
Stats Stats
// collector is used to register the metrics
collector Collector

// Stats contains all the gauges that track the db-calculated stats
stats *prometheus.GaugeVec

// the histograms that track the time it takes to perform certain operations
verifyMerkleRoots *prometheus.HistogramVec
recordTransaction *prometheus.HistogramVec
queryTransaction *prometheus.HistogramVec

// each cronJob is observed by the duration it takes to execute and the last time it was executed
cronHistogram *prometheus.HistogramVec
cronLastExecution *prometheus.GaugeVec
}
Expand All @@ -24,7 +31,7 @@ type Metrics struct {
func NewMetrics(collector Collector) *Metrics {
return &Metrics{
collector: collector,
Stats: registerStats(collector),
stats: collector.RegisterGaugeVec(statsGaugeName, "name"),
verifyMerkleRoots: collector.RegisterHistogramVec(verifyMerkleRootsHistogramName, "classification"),
recordTransaction: collector.RegisterHistogramVec(recordTransactionHistogramName, "classification", "strategy"),
queryTransaction: collector.RegisterHistogramVec(queryTransactionHistogramName, "classification"),
Expand All @@ -48,15 +55,15 @@ func (m *Metrics) TrackVerifyMerkleRoots() EndWithClassification {
func (m *Metrics) TrackRecordTransaction(strategyName string) EndWithClassification {
start := time.Now()
return func(success bool) {
m.verifyMerkleRoots.WithLabelValues(classify(success), strategyName).Observe(time.Since(start).Seconds())
m.recordTransaction.WithLabelValues(classify(success), strategyName).Observe(time.Since(start).Seconds())
}
}

// TrackQueryTransaction is used to track the time it takes to query a transaction
func (m *Metrics) TrackQueryTransaction() EndWithClassification {
start := time.Now()
return func(success bool) {
m.verifyMerkleRoots.WithLabelValues(classify(success)).Observe(time.Since(start).Seconds())
m.queryTransaction.WithLabelValues(classify(success)).Observe(time.Since(start).Seconds())
}
}

Expand Down
8 changes: 1 addition & 7 deletions metrics/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,5 @@ const (
)

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"
statsGaugeName = domainPrefix + "stats_total"
)
36 changes: 20 additions & 16 deletions metrics/stats.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"
// SetXPubCount adds a value to the stats gauge with the label "xpub"
func (m *Metrics) SetXPubCount(value int64) {
m.stats.WithLabelValues("xpub").Set(float64(value))
}

// SetUtxoCount adds a value to the stats gauge with the label "utxo"
func (m *Metrics) SetUtxoCount(value int64) {
m.stats.WithLabelValues("utxo").Set(float64(value))
}

// SetPaymailCount adds a value to the stats gauge with the label "paymail"
func (m *Metrics) SetPaymailCount(value int64) {
m.stats.WithLabelValues("paymail").Set(float64(value))
}

// Stats is a struct that contains all the gauges that are used to track the calculated stats of the application
type Stats struct {
XPub prometheus.Gauge
Utxo prometheus.Gauge
Paymail prometheus.Gauge
Destination prometheus.Gauge
AccessKey prometheus.Gauge
// SetDestinationCount adds a value to the stats gauge with the label "destination"
func (m *Metrics) SetDestinationCount(value int64) {
m.stats.WithLabelValues("destination").Set(float64(value))
}

func registerStats(collector Collector) Stats {
return Stats{
XPub: collector.RegisterGauge(xpubGaugeName),
Utxo: collector.RegisterGauge(utxoGaugeName),
Paymail: collector.RegisterGauge(paymailGaugeName),
Destination: collector.RegisterGauge(destinationGaugeName),
AccessKey: collector.RegisterGauge(accessKeyGaugeName),
}
// SetAccessKeyCount adds a value to the stats gauge with the label "access_key
func (m *Metrics) SetAccessKeyCount(value int64) {
m.stats.WithLabelValues("access_key").Set(float64(value))
}
4 changes: 2 additions & 2 deletions record_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type recordTxStrategy interface {
Name() string
TxID() string
LockKey() string
Validate() error
Expand All @@ -21,8 +22,7 @@ type recordIncomingTxStrategy interface {

func recordTransaction(ctx context.Context, c ClientInterface, strategy recordTxStrategy, opts ...ModelOps) (transaction *Transaction, err error) {
if metrics, enabled := c.Metrics(); enabled {
strategyType := fmt.Sprintf("%T", strategy)
end := metrics.TrackRecordTransaction(strategyType)
end := metrics.TrackRecordTransaction(strategy.Name())
defer func() {
success := err == nil
end(success)
Expand Down
4 changes: 4 additions & 0 deletions record_tx_strategy_external_incoming_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type externalIncomingTx struct {
allowBroadcastErrors bool // only BEEF cannot allow for broadcast errors
}

func (strategy *externalIncomingTx) Name() string {
return "external_incoming_tx"
}

func (strategy *externalIncomingTx) Execute(ctx context.Context, c ClientInterface, opts []ModelOps) (*Transaction, error) {
logger := c.Logger()
transaction, err := _createExternalTxToRecord(ctx, strategy, c, opts)
Expand Down
4 changes: 4 additions & 0 deletions record_tx_strategy_internal_incoming_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type internalIncomingTx struct {
allowBroadcastErrors bool // only BEEF cannot allow for broadcast errors
}

func (strategy *internalIncomingTx) Name() string {
return "internal_incoming_tx"
}

func (strategy *internalIncomingTx) Execute(ctx context.Context, c ClientInterface, _ []ModelOps) (*Transaction, error) {
logger := c.Logger()
logger.Info().
Expand Down
6 changes: 4 additions & 2 deletions record_tx_strategy_outgoing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type outgoingTx struct {
XPubKey string
}

func (strategy *outgoingTx) Name() string {
return "outgoing_tx"
}

func (strategy *outgoingTx) Execute(ctx context.Context, c ClientInterface, opts []ModelOps) (*Transaction, error) {
logger := c.Logger()
logger.Info().
Expand Down Expand Up @@ -96,7 +100,6 @@ func _createOutgoingTxToRecord(ctx context.Context, oTx *outgoingTx, c ClientInt
tx, err := newTransactionWithDraftID(
oTx.Hex, oTx.RelatedDraftID, newOpts...,
)

if err != nil {
return nil, err
}
Expand All @@ -117,7 +120,6 @@ func _createOutgoingTxToRecord(ctx context.Context, oTx *outgoingTx, c ClientInt

func _hydrateOutgoingWithDraft(ctx context.Context, tx *Transaction) error {
draft, err := getDraftTransactionID(ctx, tx.XPubID, tx.DraftID, tx.GetOptions(false)...)

if err != nil {
return err
}
Expand Down

0 comments on commit 36a09b8

Please sign in to comment.