diff options
| -rw-r--r-- | main.go | 39 |
1 files changed, 24 insertions, 15 deletions
@@ -23,7 +23,8 @@ var ( path string subsystem string - gauges map[string]*prometheus.GaugeVec + extensionKeys map[string]bool + gauges map[string]*prometheus.GaugeVec ) func main() { @@ -71,6 +72,8 @@ func main() { ) // Start the update process + extensionKeys = make(map[string]bool) + update() go updateLoop() @@ -89,10 +92,8 @@ func main() { } func update() { - keys := make(map[string]bool) - - sizes := make(map[string]int64) - counts := make(map[string]int64) + sizes := make(map[string]float64) + counts := make(map[string]float64) slog.Debug("Scanning filesystem for updates") err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { @@ -111,10 +112,10 @@ func update() { ext = ext[1:] } - keys[ext] = true + extensionKeys[ext] = true counts[ext]++ - sizes[ext] += info.Size() + sizes[ext] += float64(info.Size()) return nil }) @@ -126,14 +127,22 @@ func update() { // Loop over all keys and set the respective gauge values slog.Debug("Writing new values to metrics") - for key, _ := range keys { - gauges["count"].With(prometheus.Labels{ - "extension": key, - }).Set(float64(counts[key])) - - gauges["size"].With(prometheus.Labels{ - "extension": key, - }).Set(float64(sizes[key])) + for key, _ := range extensionKeys { + if _, ok := counts[key]; ok { + gauges["count"].With(prometheus.Labels{ + "extension": key, + }).Set(counts[key]) + gauges["size"].With(prometheus.Labels{ + "extension": key, + }).Set(sizes[key]) + + continue + } + + // Clean up extension which no longer exists in the directory + gauges["count"].Delete(prometheus.Labels{"extension": key}) + gauges["size"].Delete(prometheus.Labels{"extension": key}) + delete(extensionKeys, key) } } |
