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 31, 2023
1 parent 8908f58 commit c174495
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,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, ""))
// default 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, ""))
// default 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 c174495

Please sign in to comment.