From 58a91fbccad0cab2e34152a987d2396296f6a618 Mon Sep 17 00:00:00 2001 From: nvnamsss Date: Mon, 22 Aug 2022 17:44:40 +0700 Subject: [PATCH 1/2] Fixed an issue where name of metrics were incorrect. Fixed issues in AddGauge and AddHistogram where instances were not recorded for later usage. Pusher.Start now will loop properly --- adapters/prometheus/pusher.go | 48 ++++++++++++++++++++++++++++++----- metrics/metrics.go | 42 +++++++++++++++--------------- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/adapters/prometheus/pusher.go b/adapters/prometheus/pusher.go index 8a48243..4eb1218 100644 --- a/adapters/prometheus/pusher.go +++ b/adapters/prometheus/pusher.go @@ -6,6 +6,7 @@ import ( "github.com/axieinfinity/bridge-core/adapters" + "github.com/ethereum/go-ethereum/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" ) @@ -32,6 +33,21 @@ func (p *Pusher) AddCounter(name string, description string) *Pusher { return p } +func (p *Pusher) AddCounterWithLable(name string, description string, labels map[string]string) *Pusher { + if _, ok := p.counters[name]; ok { + return p + } + + counter := prometheus.NewCounter(prometheus.CounterOpts{ + Name: name, + Help: description, + ConstLabels: labels, + }) + p.counters[name] = counter + p.pusher.Collector(counter) + return p +} + func (p *Pusher) IncrCounter(name string, value int) { if _, ok := p.counters[name]; !ok { return @@ -43,11 +59,24 @@ func (p *Pusher) AddGauge(name string, description string) *Pusher { if _, ok := p.gauges[name]; ok { return p } - gauge := prometheus.NewGauge(prometheus.GaugeOpts{ Name: name, Help: description, }) + p.gauges[name] = gauge + p.pusher.Collector(gauge) + return p +} + +func (p *Pusher) AddGaugeWithLabel(name string, description string, labels map[string]string) *Pusher { + if _, ok := p.gauges[name]; ok { + return p + } + gauge := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: name, + Help: description, + ConstLabels: labels, + }) p.pusher.Collector(gauge) return p } @@ -77,6 +106,7 @@ func (p *Pusher) AddHistogram(name string, description string) *Pusher { Name: name, Help: description, }) + p.histograms[name] = histogram p.pusher.Collector(histogram) return p } @@ -95,12 +125,18 @@ func (p *Pusher) Push() error { func (p *Pusher) Start(ctx context.Context) { ticker := time.NewTicker(time.Second * time.Duration(adapters.AppConfig.Prometheus.PushInterval)) - select { - case <-ticker.C: - p.pusher.Push() - case <-ctx.Done(): - ticker.Stop() + for { + select { + case <-ticker.C: + if err := p.Push(); err != nil { + log.Error("Push metrics got error", err) + } + case <-ctx.Done(): + ticker.Stop() + return + } } + } func NewPusher() *Pusher { diff --git a/metrics/metrics.go b/metrics/metrics.go index aacd605..a90cfac 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -7,27 +7,27 @@ import ( ) const ( - ListenerProcessedBlockMetric string = "%s/processedBlock" - - PreparingFailedJobMetric string = "jobs/prepare/failed" - PreparingSuccessJobMetric string = "jobs/prepare/success" - ProcessingJobMetric string = "jobs/processing" - ProcessedSuccessJobMetric string = "jobs/success" - ProcessedFailedJobMetric string = "jobs/failed" - - PendingTaskMetric string = "Ronin/tasks/pending" - ProcessingTaskMetric string = "Ronin/tasks/processing" - SuccessTaskMetric string = "Ronin/tasks/success" - FailedTaskMetric string = "Ronin/tasks/failed" - WithdrawalTaskMetric string = "Ronin/tasks/withdrawal" - DepositTaskMetric string = "Ronin/tasks/deposit" - AckWithdrawalTaskMetric string = "Ronin/tasks/acknowledgeWithdrawal" - - KmsSuccessSign string = "kms/success" - KmsNetworkFailure string = "kms/failure/network" - KmsInternalFailure string = "kms/failure/internal" - KmsSignLatency string = "kms/latency" - KmsLastSuccess string = "kms/lastSuccess" + ListenerProcessedBlockMetric string = "%s_processedBlock" + + PreparingFailedJobMetric string = "jobs_prepare_failed" + PreparingSuccessJobMetric string = "jobs_prepare_success" + ProcessingJobMetric string = "jobs_processing" + ProcessedSuccessJobMetric string = "jobs_success" + ProcessedFailedJobMetric string = "jobs_failed" + + PendingTaskMetric string = "Ronin_tasks_pending" + ProcessingTaskMetric string = "Ronin_tasks_processing" + SuccessTaskMetric string = "Ronin_tasks_success" + FailedTaskMetric string = "Ronin_tasks_failed" + WithdrawalTaskMetric string = "Ronin_tasks_withdrawal" + DepositTaskMetric string = "Ronin_tasks_deposit" + AckWithdrawalTaskMetric string = "Ronin_tasks_acknowledgeWithdrawal" + + KmsSuccessSign string = "kms_success" + KmsNetworkFailure string = "kms_failure/network" + KmsInternalFailure string = "kms_failure/internal" + KmsSignLatency string = "kms_latency" + KmsLastSuccess string = "kms_lastSuccess" ) var ( From 21d7c22f08cefd7f276aa5cbee663917ff387327 Mon Sep 17 00:00:00 2001 From: nvnamsss Date: Mon, 22 Aug 2022 17:47:57 +0700 Subject: [PATCH 2/2] Added AddHistogramWithLabels --- adapters/prometheus/pusher.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/adapters/prometheus/pusher.go b/adapters/prometheus/pusher.go index 4eb1218..18bdb22 100644 --- a/adapters/prometheus/pusher.go +++ b/adapters/prometheus/pusher.go @@ -77,6 +77,7 @@ func (p *Pusher) AddGaugeWithLabel(name string, description string, labels map[s Help: description, ConstLabels: labels, }) + p.gauges[name] = gauge p.pusher.Collector(gauge) return p } @@ -111,6 +112,21 @@ func (p *Pusher) AddHistogram(name string, description string) *Pusher { return p } +func (p *Pusher) AddHistogramWithLabels(name string, description string, labels map[string]string) *Pusher { + if _, ok := p.histograms[name]; ok { + return p + } + + histogram := prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: name, + Help: description, + ConstLabels: labels, + }) + p.histograms[name] = histogram + p.pusher.Collector(histogram) + return p +} + func (p *Pusher) ObserveHistogram(name string, value int) { if _, ok := p.histograms[name]; !ok { return