Skip to content

Commit

Permalink
fix: metric module init exactly once when there is multiple service o…
Browse files Browse the repository at this point in the history
…r reference
  • Loading branch information
FoghostCn committed Aug 30, 2023
1 parent 501cefe commit 298d4ea
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package metrics

import (
"sync"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
Expand All @@ -26,23 +30,26 @@ var (
registries = make(map[string]func(*common.URL) MetricRegistry)
collectors = make([]CollectorFunc, 0)
registry MetricRegistry
once sync.Once
)

// CollectorFunc used to extend more indicators
type CollectorFunc func(MetricRegistry, *common.URL)

// Init Metrics module
func Init(url *common.URL) {
InitAppInfo(url.GetParam(constant.ApplicationKey, ""), url.GetParam(constant.AppVersionKey, ""))
// defalut protocol is already set in metricConfig
regFunc, ok := registries[url.Protocol]
if ok {
registry = regFunc(url)
for _, co := range collectors {
co(registry, url)
once.Do(func() {
InitAppInfo(url.GetParam(constant.ApplicationKey, ""), url.GetParam(constant.AppVersionKey, ""))
// defalut protocol is already set in metricConfig
regFunc, ok := registries[url.Protocol]
if ok {
registry = regFunc(url)
for _, co := range collectors {
co(registry, url)
}
registry.Export()
}
registry.Export()
}
})
}

// SetRegistry extend more MetricRegistry, default PrometheusRegistry
Expand Down

0 comments on commit 298d4ea

Please sign in to comment.