Skip to content

Commit

Permalink
Fixed service-mirror metrics warning (#11246)
Browse files Browse the repository at this point in the history
Whenever the service mirror's main loop was triggered again, the following warnings were generated:

```
time="2023-08-14T20:16:29Z" level=warning msg="failed to register Prometheus gauge Desc{fqName: \"service_cache_size\", help: \"Number of items in the client-go service cache\", constLabels: {cluster=\"remote\"}, variableLabels: []}: duplicate metrics collector registration attempted"
time="2023-08-14T20:16:29Z" level=warning msg="failed to register Prometheus gauge Desc{fqName: \"endpoints_cache_size\", help: \"Number of items in the client-go endpoints cache\", constLabels: {cluster=\"remote\"}, variableLabels: []}: duplicate metrics collector registration attempted"
```

To fix, this adds into the cluster watcher's `Stop()` method a directive to unregister the prometheus cache metrics associated to the cluster's client API.
  • Loading branch information
alpeb authored Aug 16, 2023
1 parent 117048c commit d823ad7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controller/k8s/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ func (api *API) Sync(stopCh <-chan struct{}) {
waitForCacheSync(api.syncChecks)
}

// UnregisterGauges unregisters all the prometheus cache gauges associated to this API
func (api *API) UnregisterGauges() {
api.promGauges.unregister()
}

// NS provides access to a shared informer and lister for Namespaces.
func (api *API) NS() coreinformers.NamespaceInformer {
if api.ns == nil {
Expand Down
6 changes: 6 additions & 0 deletions controller/k8s/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ func (p *promGauges) addInformerSize(kind string, labels prometheus.Labels, inf
return float64(len(inf.GetStore().ListKeys()))
}))
}

func (p *promGauges) unregister() {
for _, gauge := range p.gauges {
prometheus.Unregister(gauge)
}
}
4 changes: 4 additions & 0 deletions multicluster/service-mirror/cluster_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ func (rcsw *RemoteClusterServiceWatcher) Stop(cleanupState bool) {
rcsw.log.Warnf("error removing service informer handler: %s", err)
}
}

if rcsw.remoteAPIClient != nil {
rcsw.remoteAPIClient.UnregisterGauges()
}
}

func (rcsw *RemoteClusterServiceWatcher) resolveGatewayAddress() ([]corev1.EndpointAddress, error) {
Expand Down

0 comments on commit d823ad7

Please sign in to comment.