Skip to content

Commit a514fa0

Browse files
authored
Merge pull request kubernetes#74636 from logicalhan/reflector-metrics
Remove reflector metrics since they are causing a memory leak
2 parents 02bd660 + ca096f8 commit a514fa0

File tree

2 files changed

+1
-28
lines changed

2 files changed

+1
-28
lines changed

pkg/util/reflector/prometheus/prometheus.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ func init() {
8585
prometheus.MustRegister(watchDuration)
8686
prometheus.MustRegister(itemsPerWatch)
8787
prometheus.MustRegister(lastResourceVersion)
88-
89-
cache.SetReflectorMetricsProvider(prometheusMetricsProvider{})
9088
}
9189

9290
type prometheusMetricsProvider struct{}

staging/src/k8s.io/client-go/tools/cache/reflector.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ import (
2424
"net"
2525
"net/url"
2626
"reflect"
27-
"strconv"
2827
"strings"
2928
"sync"
30-
"sync/atomic"
3129
"syscall"
3230
"time"
3331

@@ -96,17 +94,10 @@ func NewReflector(lw ListerWatcher, expectedType interface{}, store Store, resyn
9694
return NewNamedReflector(naming.GetNameFromCallsite(internalPackages...), lw, expectedType, store, resyncPeriod)
9795
}
9896

99-
// reflectorDisambiguator is used to disambiguate started reflectors.
100-
// initialized to an unstable value to ensure meaning isn't attributed to the suffix.
101-
var reflectorDisambiguator = int64(time.Now().UnixNano() % 12345)
102-
10397
// NewNamedReflector same as NewReflector, but with a specified name for logging
10498
func NewNamedReflector(name string, lw ListerWatcher, expectedType interface{}, store Store, resyncPeriod time.Duration) *Reflector {
105-
reflectorSuffix := atomic.AddInt64(&reflectorDisambiguator, 1)
10699
r := &Reflector{
107-
name: name,
108-
// we need this to be unique per process (some names are still the same) but obvious who it belongs to
109-
metrics: newReflectorMetrics(makeValidPrometheusMetricLabel(fmt.Sprintf("reflector_"+name+"_%d", reflectorSuffix))),
100+
name: name,
110101
listerWatcher: lw,
111102
store: store,
112103
expectedType: reflect.TypeOf(expectedType),
@@ -174,8 +165,6 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
174165
// to be served from cache and potentially be delayed relative to
175166
// etcd contents. Reflector framework will catch up via Watch() eventually.
176167
options := metav1.ListOptions{ResourceVersion: "0"}
177-
r.metrics.numberOfLists.Inc()
178-
start := r.clock.Now()
179168

180169
if err := func() error {
181170
initTrace := trace.New("Reflector " + r.name + " ListAndWatch")
@@ -204,7 +193,6 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
204193
return fmt.Errorf("%s: Failed to list %v: %v", r.name, r.expectedType, err)
205194
}
206195
initTrace.Step("Objects listed")
207-
r.metrics.listDuration.Observe(time.Since(start).Seconds())
208196
listMetaInterface, err := meta.ListAccessor(list)
209197
if err != nil {
210198
return fmt.Errorf("%s: Unable to understand list result %#v: %v", r.name, list, err)
@@ -216,7 +204,6 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
216204
return fmt.Errorf("%s: Unable to understand list result %#v (%v)", r.name, list, err)
217205
}
218206
initTrace.Step("Objects extracted")
219-
r.metrics.numberOfItemsInList.Observe(float64(len(items)))
220207
if err := r.syncWith(items, resourceVersion); err != nil {
221208
return fmt.Errorf("%s: Unable to sync list result: %v", r.name, err)
222209
}
@@ -272,7 +259,6 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
272259
TimeoutSeconds: &timeoutSeconds,
273260
}
274261

275-
r.metrics.numberOfWatches.Inc()
276262
w, err := r.listerWatcher.Watch(options)
277263
if err != nil {
278264
switch err {
@@ -324,11 +310,6 @@ func (r *Reflector) watchHandler(w watch.Interface, resourceVersion *string, err
324310
// Stopping the watcher should be idempotent and if we return from this function there's no way
325311
// we're coming back in with the same watch interface.
326312
defer w.Stop()
327-
// update metrics
328-
defer func() {
329-
r.metrics.numberOfItemsInWatch.Observe(float64(eventCount))
330-
r.metrics.watchDuration.Observe(time.Since(start).Seconds())
331-
}()
332313

333314
loop:
334315
for {
@@ -384,7 +365,6 @@ loop:
384365

385366
watchDuration := r.clock.Now().Sub(start)
386367
if watchDuration < 1*time.Second && eventCount == 0 {
387-
r.metrics.numberOfShortWatches.Inc()
388368
return fmt.Errorf("very short watch: %s: Unexpected watch close - watch lasted less than a second and no items received", r.name)
389369
}
390370
klog.V(4).Infof("%s: Watch close - %v total %v items received", r.name, r.expectedType, eventCount)
@@ -403,9 +383,4 @@ func (r *Reflector) setLastSyncResourceVersion(v string) {
403383
r.lastSyncResourceVersionMutex.Lock()
404384
defer r.lastSyncResourceVersionMutex.Unlock()
405385
r.lastSyncResourceVersion = v
406-
407-
rv, err := strconv.Atoi(v)
408-
if err == nil {
409-
r.metrics.lastResourceVersion.Set(float64(rv))
410-
}
411386
}

0 commit comments

Comments
 (0)