watch: record num watches in expvars (#1795)

This commit is contained in:
Dan Miller 2019-07-01 11:44:30 -04:00 committed by Nicolas De loof
parent 47551895f3
commit b5ccea7b0e

View File

@ -3,6 +3,7 @@
package watch package watch
import ( import (
"expvar"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -34,6 +35,10 @@ type naiveNotify struct {
notifyList map[string]bool notifyList map[string]bool
} }
var (
numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
)
func (d *naiveNotify) Add(name string) error { func (d *naiveNotify) Add(name string) error {
fi, err := os.Stat(name) fi, err := os.Stat(name)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
@ -52,7 +57,7 @@ func (d *naiveNotify) Add(name string) error {
return errors.Wrapf(err, "notify.Add(%q)", name) return errors.Wrapf(err, "notify.Add(%q)", name)
} }
} else { } else {
err = d.watcher.Add(filepath.Dir(name)) err = d.add(filepath.Dir(name))
if err != nil { if err != nil {
return errors.Wrapf(err, "notify.Add(%q)", filepath.Dir(name)) return errors.Wrapf(err, "notify.Add(%q)", filepath.Dir(name))
} }
@ -74,7 +79,7 @@ func (d *naiveNotify) watchRecursively(dir string) error {
if !mode.IsDir() { if !mode.IsDir() {
return nil return nil
} }
err = d.watcher.Add(path) err = d.add(path)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
@ -100,7 +105,7 @@ func (d *naiveNotify) watchAncestorOfMissingPath(path string) error {
return d.watchAncestorOfMissingPath(parent) return d.watchAncestorOfMissingPath(parent)
} }
return d.watcher.Add(path) return d.add(path)
} }
func (d *naiveNotify) Close() error { func (d *naiveNotify) Close() error {
@ -151,7 +156,7 @@ func (d *naiveNotify) loop() {
} }
} }
if shouldWatch { if shouldWatch {
err := d.watcher.Add(path) err := d.add(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
d.log.Infof("Error watching path %s: %s", e.Name, err) d.log.Infof("Error watching path %s: %s", e.Name, err)
} }
@ -179,6 +184,15 @@ func (d *naiveNotify) shouldNotify(path string) bool {
return false return false
} }
func (d *naiveNotify) add(path string) error {
err := d.watcher.Add(path)
if err != nil {
return err
}
numberOfWatches.Add(1)
return nil
}
func NewWatcher(l logger.Logger) (*naiveNotify, error) { func NewWatcher(l logger.Logger) (*naiveNotify, error) {
fsw, err := fsnotify.NewWatcher() fsw, err := fsnotify.NewWatcher()
if err != nil { if err != nil {