This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 2f8dc5af4 Fix: fix bug when creating metrics objects concurrently 
(#1961)
2f8dc5af4 is described below

commit 2f8dc5af47ecbc3088fa3842909f162c784ace93
Author: Yepeng Zhang <[email protected]>
AuthorDate: Sun Jul 17 23:58:22 2022 +0800

    Fix: fix bug when creating metrics objects concurrently (#1961)
    
    * fix: fix bug when creating metrics objects concurrently
    
    * rename variable names of metrics
---
 metrics/prometheus/reporter.go | 84 ++++++++++++++++++++++++++++++------------
 1 file changed, 60 insertions(+), 24 deletions(-)

diff --git a/metrics/prometheus/reporter.go b/metrics/prometheus/reporter.go
index 231a2c883..9777c740e 100644
--- a/metrics/prometheus/reporter.go
+++ b/metrics/prometheus/reporter.go
@@ -252,11 +252,17 @@ func (reporter *PrometheusReporter) setGauge(gaugeName 
string, toSetValue float6
        if len(labelMap) == 0 {
                // gauge
                if val, exist := reporter.userGauge.Load(gaugeName); !exist {
-                       newGauge := newGauge(gaugeName, reporter.namespace)
-                       _ = prom.DefaultRegisterer.Register(newGauge)
+                       gauge := newGauge(gaugeName, reporter.namespace)
+                       err := prom.DefaultRegisterer.Register(gauge)
+                       if err == nil {
+                               reporter.userGauge.Store(gaugeName, gauge)
+                               gauge.Set(toSetValue)
+                       } else if are, ok := 
err.(prometheus.AlreadyRegisteredError); ok {
+                               // A gauge for that metric has been registered 
before.
+                               // Use the old gauge from now on.
+                               
are.ExistingCollector.(prometheus.Gauge).Set(toSetValue)
+                       }
 
-                       reporter.userGauge.Store(gaugeName, newGauge)
-                       newGauge.Set(toSetValue)
                } else {
                        val.(prometheus.Gauge).Set(toSetValue)
                }
@@ -269,10 +275,16 @@ func (reporter *PrometheusReporter) setGauge(gaugeName 
string, toSetValue float6
                for k, _ := range labelMap {
                        keyList = append(keyList, k)
                }
-               newGaugeVec := newGaugeVec(gaugeName, reporter.namespace, 
keyList)
-               _ = prom.DefaultRegisterer.Register(newGaugeVec)
-               reporter.userGaugeVec.Store(gaugeName, newGaugeVec)
-               newGaugeVec.With(labelMap).Set(toSetValue)
+               gaugeVec := newGaugeVec(gaugeName, reporter.namespace, keyList)
+               err := prom.DefaultRegisterer.Register(gaugeVec)
+               if err == nil {
+                       reporter.userGaugeVec.Store(gaugeName, gaugeVec)
+                       gaugeVec.With(labelMap).Set(toSetValue)
+               } else if are, ok := err.(prometheus.AlreadyRegisteredError); 
ok {
+                       // A gauge for that metric has been registered before.
+                       // Use the old gauge from now on.
+                       
are.ExistingCollector.(*prometheus.GaugeVec).With(labelMap).Set(toSetValue)
+               }
        } else {
                val.(*prometheus.GaugeVec).With(labelMap).Set(toSetValue)
        }
@@ -284,10 +296,16 @@ func (reporter *PrometheusReporter) 
incCounter(counterName string, labelMap prom
        if len(labelMap) == 0 {
                // counter
                if val, exist := reporter.userCounter.Load(counterName); !exist 
{
-                       newCounter := newCounter(counterName, 
reporter.namespace)
-                       _ = prom.DefaultRegisterer.Register(newCounter)
-                       reporter.userCounter.Store(counterName, newCounter)
-                       newCounter.Inc()
+                       counter := newCounter(counterName, reporter.namespace)
+                       err := prom.DefaultRegisterer.Register(counter)
+                       if err == nil {
+                               reporter.userCounter.Store(counterName, counter)
+                               counter.Inc()
+                       } else if are, ok := 
err.(prometheus.AlreadyRegisteredError); ok {
+                               // A counter for that metric has been 
registered before.
+                               // Use the old counter from now on.
+                               are.ExistingCollector.(prometheus.Counter).Inc()
+                       }
                } else {
                        val.(prometheus.Counter).Inc()
                }
@@ -300,10 +318,16 @@ func (reporter *PrometheusReporter) 
incCounter(counterName string, labelMap prom
                for k, _ := range labelMap {
                        keyList = append(keyList, k)
                }
-               newCounterVec := newCounterVec(counterName, reporter.namespace, 
keyList)
-               _ = prom.DefaultRegisterer.Register(newCounterVec)
-               reporter.userCounterVec.Store(counterName, newCounterVec)
-               newCounterVec.With(labelMap).Inc()
+               counterVec := newCounterVec(counterName, reporter.namespace, 
keyList)
+               err := prom.DefaultRegisterer.Register(counterVec)
+               if err == nil {
+                       reporter.userCounterVec.Store(counterName, counterVec)
+                       counterVec.With(labelMap).Inc()
+               } else if are, ok := err.(prometheus.AlreadyRegisteredError); 
ok {
+                       // A counter for that metric has been registered before.
+                       // Use the old counter from now on.
+                       
are.ExistingCollector.(*prometheus.CounterVec).With(labelMap).Inc()
+               }
        } else {
                val.(*prometheus.CounterVec).With(labelMap).Inc()
        }
@@ -315,10 +339,16 @@ func (reporter *PrometheusReporter) 
incSummary(summaryName string, toSetValue fl
        if len(labelMap) == 0 {
                // summary
                if val, exist := reporter.userSummary.Load(summaryName); !exist 
{
-                       newSummary := newSummary(summaryName, 
reporter.namespace)
-                       _ = prom.DefaultRegisterer.Register(newSummary)
-                       reporter.userSummary.Store(summaryName, newSummary)
-                       newSummary.Observe(toSetValue)
+                       summary := newSummary(summaryName, reporter.namespace)
+                       err := prom.DefaultRegisterer.Register(summary)
+                       if err == nil {
+                               reporter.userSummary.Store(summaryName, summary)
+                               summary.Observe(toSetValue)
+                       } else if are, ok := 
err.(prometheus.AlreadyRegisteredError); ok {
+                               // A summary for that metric has been 
registered before.
+                               // Use the old summary from now on.
+                               
are.ExistingCollector.(prometheus.Summary).Observe(toSetValue)
+                       }
                } else {
                        val.(prometheus.Summary).Observe(toSetValue)
                }
@@ -331,10 +361,16 @@ func (reporter *PrometheusReporter) 
incSummary(summaryName string, toSetValue fl
                for k, _ := range labelMap {
                        keyList = append(keyList, k)
                }
-               newSummaryVec := newSummaryVec(summaryName, reporter.namespace, 
keyList, reporter.reporterConfig.SummaryMaxAge)
-               _ = prom.DefaultRegisterer.Register(newSummaryVec)
-               reporter.userSummaryVec.Store(summaryName, newSummaryVec)
-               newSummaryVec.With(labelMap).Observe(toSetValue)
+               summaryVec := newSummaryVec(summaryName, reporter.namespace, 
keyList, reporter.reporterConfig.SummaryMaxAge)
+               err := prom.DefaultRegisterer.Register(summaryVec)
+               if err == nil {
+                       reporter.userSummaryVec.Store(summaryName, summaryVec)
+                       summaryVec.With(labelMap).Observe(toSetValue)
+               } else if are, ok := err.(prometheus.AlreadyRegisteredError); 
ok {
+                       // A summary for that metric has been registered before.
+                       // Use the old summary from now on.
+                       
are.ExistingCollector.(*prometheus.SummaryVec).With(labelMap).Observe(toSetValue)
+               }
        } else {
                val.(*prometheus.SummaryVec).With(labelMap).Observe(toSetValue)
        }

Reply via email to