diff --git a/main.go b/main.go index 955bca8682..7fabfd219e 100644 --- a/main.go +++ b/main.go @@ -402,10 +402,10 @@ func main() { } if cfg.ProviderCacheTime > 0 { - p = &provider.CachedProvider{ - Provider: p, - RefreshDelay: cfg.ProviderCacheTime, - } + p = provider.NewCachedProvider( + p, + cfg.ProviderCacheTime, + ) } var r registry.Registry diff --git a/provider/cached_provider.go b/provider/cached_provider.go index 42f327e07e..dcea510f0d 100644 --- a/provider/cached_provider.go +++ b/provider/cached_provider.go @@ -17,6 +17,7 @@ package provider import ( "context" + "sync" "time" "github.com/prometheus/client_golang/prometheus" @@ -46,6 +47,8 @@ var ( Help: "Number of calls to the provider cache ApplyChanges.", }, ) + + registerCacheProviderMetrics = sync.Once{} ) type CachedProvider struct { @@ -55,6 +58,16 @@ type CachedProvider struct { cache []*endpoint.Endpoint } +func NewCachedProvider(provider Provider, refreshDelay time.Duration) *CachedProvider { + registerMetrics.Do(func() { + prometheus.MustRegister(cachedRecordsCallsTotal) + }) + return &CachedProvider{ + Provider: provider, + RefreshDelay: refreshDelay, + } +} + func (c *CachedProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) { if c.needRefresh() { log.Info("Records cache provider: refreshing records list cache") @@ -95,7 +108,3 @@ func (c *CachedProvider) needRefresh() bool { log.Debug("Records cache last Read: ", c.lastRead, "expiration: ", c.RefreshDelay, " provider expiration:", c.lastRead.Add(c.RefreshDelay), "expired: ", time.Now().After(c.lastRead.Add(c.RefreshDelay))) return time.Now().After(c.lastRead.Add(c.RefreshDelay)) } - -func init() { - prometheus.MustRegister(cachedRecordsCallsTotal) -}