From c17449558b1aa99fd238092397c2e945ca64c8b0 Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 28 Aug 2023 19:14:23 +0800 Subject: [PATCH] fix: metric module init exactly once when there is multiple service or reference --- metrics/api.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/metrics/api.go b/metrics/api.go index 1b6eb8e912..b2e8b75570 100644 --- a/metrics/api.go +++ b/metrics/api.go @@ -42,6 +42,7 @@ var ( registries = make(map[string]func(*common.URL) MetricRegistry) collectors = make([]CollectorFunc, 0) registry MetricRegistry + once sync.Once ) // CollectorFunc used to extend more indicators @@ -49,16 +50,18 @@ 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