diff --git a/cron_job_definitions.go b/cron_job_definitions.go index 84a52572..50543044 100644 --- a/cron_job_definitions.go +++ b/cron_job_definitions.go @@ -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 } diff --git a/go.mod b/go.mod index a3be03a9..ce65aed8 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,14 @@ require ( gorm.io/gorm v1.25.5 ) +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect +) + require ( github.com/99designs/gqlgen v0.17.42 // indirect github.com/acobaugh/osrelease v0.1.0 // indirect @@ -96,6 +104,7 @@ require ( github.com/newrelic/go-agent/v3/integrations/nrmongo v1.1.2 // indirect github.com/onsi/gomega v1.27.5 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_golang v1.18.0 github.com/segmentio/asm v1.2.0 // indirect github.com/segmentio/encoding v0.4.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect diff --git a/go.sum b/go.sum index af50269c..9221c018 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/aws/aws-sdk-go v1.43.45 h1:2708Bj4uV+ym62MOtBnErm/CDX61C4mFe9V2gXy1caE= github.com/aws/aws-sdk-go v1.43.45/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bitcoin-sv/go-broadcast-client v0.16.0 h1:KadOLv+i9Y6xAOkHsSl2PIECQ59SpUyYurY6Ysvpz5A= github.com/bitcoin-sv/go-broadcast-client v0.16.0/go.mod h1:GRAliwumNBjEbLRIEkXqIKJpsgmMfjvlIDqgyw/NoJE= github.com/bitcoin-sv/go-broadcast-client v0.16.1 h1:VG4QZwJEVQY/QQupTDeLMw+PEeqh9mn4id+XzYpAmHs= @@ -92,6 +94,7 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gocraft/dbr/v2 v2.7.6 h1:ASHKFgCbTLODbb9f756Cl8VAlnvQLKqIzx9E1Cfb7eo= github.com/gocraft/dbr/v2 v2.7.6/go.mod h1:8IH98S8M8J0JSEiYk0MPH26ZDUKemiQ/GvmXL5jo+Uw= @@ -210,12 +213,17 @@ github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/goveralls v0.0.6/go.mod h1:h8b4ow6FxSPMQHF6o2ve3qsclnffZjYTNEKmLesRwqw= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= +github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM= github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk= github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0= github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= @@ -227,6 +235,7 @@ github.com/mrz1836/go-datastore v0.5.9 h1:wNNUNCBCSddOieE5aM06GI5dN8JElbIQrzqEAx github.com/mrz1836/go-datastore v0.5.9/go.mod h1:tkc466oJtAPNxENZpfjlcerTrCy7kyCzJiVVgbIIguE= github.com/mrz1836/go-logger v0.3.2 h1:bjd23NwVaLWncXgXAyxAwWLQ02of0Ci3iJIZZEakkFU= github.com/mrz1836/go-logger v0.3.2/go.mod h1:8gWPdqxOAFNJOHDXS2ducgsokUOf0wWtAAM3LXPrhYo= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/newrelic/go-agent/v3 v3.29.0 h1:Bc1D3DoOkpJs6aIzhOjUp+yIKJ2RfZ+LMQemZOs9t9k= github.com/newrelic/go-agent/v3 v3.29.0/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg= github.com/newrelic/go-agent/v3/integrations/nrhttprouter v1.0.2 h1:Y+bKuryqCg+TAvBkBaEKwak+Boy5OdosQNdDGFwyDLo= @@ -251,6 +260,14 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= +github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= +github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rafaeljusto/redigomock v2.4.0+incompatible h1:d7uo5MVINMxnRr20MxbgDkmZ8QRfevjOVgEa4n0OZyY= github.com/rafaeljusto/redigomock v2.4.0+incompatible/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY= github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= diff --git a/metrics/interface.go b/metrics/interface.go index 4983a039..c5e3aa9b 100644 --- a/metrics/interface.go +++ b/metrics/interface.go @@ -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 } diff --git a/metrics/metrics.go b/metrics/metrics.go index e87b71b0..827a745a 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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 diff --git a/metrics/stats.go b/metrics/stats.go index 7d09432f..3068ffb8 100644 --- a/metrics/stats.go +++ b/metrics/stats.go @@ -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), } }