From 36a09b80059fa0955910896ffd13276e234f4fb5 Mon Sep 17 00:00:00 2001 From: Krzysztof Tomecki <152964795+chris-4chain@users.noreply.github.com> Date: Mon, 12 Feb 2024 07:03:32 +0100 Subject: [PATCH] feat(BUX-467): adjustments after with-grafana tests --- cron_job_definitions.go | 10 +++--- metrics/metrics.go | 17 +++++++--- metrics/naming.go | 8 +---- metrics/stats.go | 36 ++++++++++++---------- record_tx.go | 4 +-- record_tx_strategy_external_incoming_tx.go | 4 +++ record_tx_strategy_internal_incoming_tx.go | 4 +++ record_tx_strategy_outgoing_tx.go | 6 ++-- 8 files changed, 52 insertions(+), 37 deletions(-) diff --git a/cron_job_definitions.go b/cron_job_definitions.go index 50543044..6674adbd 100644 --- a/cron_job_definitions.go +++ b/cron_job_definitions.go @@ -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 diff --git a/metrics/metrics.go b/metrics/metrics.go index 827a745a..8f8a4831 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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 } @@ -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"), @@ -48,7 +55,7 @@ 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()) } } @@ -56,7 +63,7 @@ func (m *Metrics) TrackRecordTransaction(strategyName string) EndWithClassificat 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()) } } diff --git a/metrics/naming.go b/metrics/naming.go index 32b60fbd..aa40f37d 100644 --- a/metrics/naming.go +++ b/metrics/naming.go @@ -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" ) diff --git a/metrics/stats.go b/metrics/stats.go index 3068ffb8..7ed7b89e 100644 --- a/metrics/stats.go +++ b/metrics/stats.go @@ -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)) } diff --git a/record_tx.go b/record_tx.go index c6d629ec..9712b4ff 100644 --- a/record_tx.go +++ b/record_tx.go @@ -7,6 +7,7 @@ import ( ) type recordTxStrategy interface { + Name() string TxID() string LockKey() string Validate() error @@ -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) diff --git a/record_tx_strategy_external_incoming_tx.go b/record_tx_strategy_external_incoming_tx.go index 758f69bf..36fa1be1 100644 --- a/record_tx_strategy_external_incoming_tx.go +++ b/record_tx_strategy_external_incoming_tx.go @@ -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) diff --git a/record_tx_strategy_internal_incoming_tx.go b/record_tx_strategy_internal_incoming_tx.go index 20ecf12d..7e8d69fe 100644 --- a/record_tx_strategy_internal_incoming_tx.go +++ b/record_tx_strategy_internal_incoming_tx.go @@ -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(). diff --git a/record_tx_strategy_outgoing_tx.go b/record_tx_strategy_outgoing_tx.go index edc7465a..67049c8d 100644 --- a/record_tx_strategy_outgoing_tx.go +++ b/record_tx_strategy_outgoing_tx.go @@ -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(). @@ -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 } @@ -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 }