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): prometheus instead of plain interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Feb 12, 2024
1 parent 0c9e6b7 commit 263fc95
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 60 deletions.
18 changes: 0 additions & 18 deletions cron_job_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,5 @@ func taskCalculateMetrics(ctx context.Context, client *Client) error {
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
}
9 changes: 9 additions & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 5 additions & 22 deletions metrics/interface.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

// Collector is an interface that is used to register metrics
type Collector interface {
RegisterGauge(name string) GaugeInterface
RegisterGaugeVec(name string, labels ...string) GaugeVecInterface
RegisterHistogramVec(name string, labels ...string) HistogramVecInterface
}

type GaugeVecInterface interface {
WithLabelValues(lvs ...string) GaugeInterface
}

// GaugeInterface is an interface that is used to track gauges of values
type GaugeInterface interface {
Set(value float64)
}

// HistogramVecInterface is an interface that is used to register histograms with labels
type HistogramVecInterface interface {
WithLabelValues(lvs ...string) HistogramInterface
}

// HistogramInterface is an interface that is used to track histograms of values
type HistogramInterface interface {
Observe(value float64)
RegisterGauge(name string) prometheus.Gauge
RegisterGaugeVec(name string, labels ...string) *prometheus.GaugeVec
RegisterHistogramVec(name string, labels ...string) *prometheus.HistogramVec
}
16 changes: 10 additions & 6 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ Package metrics provides a way to track metrics in the application. Functionalit
*/
package metrics

import "time"
import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

// Metrics is a struct that contains all the metrics that are used to track in the package
type Metrics struct {
collector Collector
Stats Stats
verifyMerkleRoots HistogramVecInterface
recordTransaction HistogramVecInterface
queryTransaction HistogramVecInterface
cronHistogram HistogramVecInterface
cronLastExecution GaugeVecInterface
verifyMerkleRoots *prometheus.HistogramVec
recordTransaction *prometheus.HistogramVec
queryTransaction *prometheus.HistogramVec
cronHistogram *prometheus.HistogramVec
cronLastExecution *prometheus.GaugeVec
}

// NewMetrics is a constructor for the Metrics struct
Expand Down
26 changes: 12 additions & 14 deletions metrics/stats.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

// 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
Utxo GaugeInterface
TransactionIn GaugeInterface
TransactionOut GaugeInterface
Paymail GaugeInterface
Destination GaugeInterface
AccessKey GaugeInterface
XPub prometheus.Gauge
Utxo prometheus.Gauge
Paymail prometheus.Gauge
Destination prometheus.Gauge
AccessKey prometheus.Gauge
}

func registerStats(collector Collector) Stats {
return Stats{
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),
XPub: collector.RegisterGauge(xpubGaugeName),
Utxo: collector.RegisterGauge(utxoGaugeName),
Paymail: collector.RegisterGauge(paymailGaugeName),
Destination: collector.RegisterGauge(destinationGaugeName),
AccessKey: collector.RegisterGauge(accessKeyGaugeName),
}
}

0 comments on commit 263fc95

Please sign in to comment.