aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2024-11-27 18:30:34 +0100
committerPatrick Spek <p.spek@tyil.nl>2024-11-27 18:30:34 +0100
commit88ecd851d227b2be763f3665f4c10e1ec9a6a3fc (patch)
tree942bc322e8431ebd27c7272a021e8975e7710c02
parent3ced80875717eb77777946ac0a48f45c224eb59f (diff)
downloadfsdir-88ecd851d227b2be763f3665f4c10e1ec9a6a3fc.tar.gz
fsdir-88ecd851d227b2be763f3665f4c10e1ec9a6a3fc.tar.bz2
Change extension key tablev1.0.1
Remove gauges for extensions which no longer appear after a filesystem scan. This removes the Prometheus metric labels for extensions that have been removed from the filesystem, instead of leaving them at their last known value indefinitely.
-rw-r--r--main.go39
1 files changed, 24 insertions, 15 deletions
diff --git a/main.go b/main.go
index 2e3a136..6408877 100644
--- a/main.go
+++ b/main.go
@@ -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)
}
}