ops: Disk tier occupancy on /metrics
Operators could see attach-ready and capacity-pressure events but not how full the configured disk (or memory) tier was without reading filesystems. Expose size/capacity gauges and disk_cache_full_ratio next to disk_tier_ready. Capacity is a config read, so it stays available while attach is pending.
This commit was merged in pull request #53.
This commit is contained in:
@@ -100,6 +100,9 @@ func TestWriteTextPrometheusExposition(t *testing.T) {
|
||||
TotalBytesSaved: 50,
|
||||
MemoryCacheSize: 8,
|
||||
DiskCacheSize: 16,
|
||||
MemoryCacheCapacity: 8,
|
||||
DiskCacheCapacity: 32,
|
||||
DiskCacheFullRatio: 0.5,
|
||||
DiskTierReady: 1,
|
||||
Uptime: 3 * time.Second,
|
||||
}
|
||||
@@ -120,6 +123,7 @@ func TestWriteTextPrometheusExposition(t *testing.T) {
|
||||
}
|
||||
gauges := []string{
|
||||
"hit_rate", "avg_response_time_ms", "memory_cache_size", "disk_cache_size",
|
||||
"memory_cache_capacity", "disk_cache_capacity", "disk_cache_full_ratio",
|
||||
"disk_tier_ready", "uptime_seconds",
|
||||
}
|
||||
for _, name := range counters {
|
||||
@@ -138,6 +142,15 @@ func TestWriteTextPrometheusExposition(t *testing.T) {
|
||||
if !strings.Contains(body, "disk_tier_ready 1\n") {
|
||||
t.Errorf("missing disk_tier_ready 1 sample: %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "memory_cache_capacity 8\n") {
|
||||
t.Errorf("missing memory_cache_capacity 8 sample: %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "disk_cache_capacity 32\n") {
|
||||
t.Errorf("missing disk_cache_capacity 32 sample: %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "disk_cache_full_ratio 0.5000\n") {
|
||||
t.Errorf("missing disk_cache_full_ratio 0.5000 sample: %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "range_cache 3\n") {
|
||||
t.Errorf("missing range_cache 3 sample: %q", body)
|
||||
}
|
||||
@@ -146,6 +159,54 @@ func TestWriteTextPrometheusExposition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskCacheFullRatioInGetStats(t *testing.T) {
|
||||
t.Parallel()
|
||||
cases := []struct {
|
||||
name string
|
||||
size int64
|
||||
capacity int64
|
||||
wantRatio float64
|
||||
wantCapacity int64
|
||||
}{
|
||||
{"no disk (capacity 0)", 0, 0, 0, 0},
|
||||
{"zero size with capacity", 0, 1024, 0, 1024},
|
||||
{"half full", 512, 1024, 0.5, 1024},
|
||||
{"exact full", 1024, 1024, 1, 1024},
|
||||
{"size above capacity clamps to 1", 2048, 1024, 1, 1024},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := NewMetrics()
|
||||
m.SetDiskCacheSize(tc.size)
|
||||
m.SetDiskCacheCapacity(tc.capacity)
|
||||
st := m.GetStats()
|
||||
if st.DiskCacheCapacity != tc.wantCapacity {
|
||||
t.Fatalf("DiskCacheCapacity=%d, want %d", st.DiskCacheCapacity, tc.wantCapacity)
|
||||
}
|
||||
if st.DiskCacheFullRatio != tc.wantRatio {
|
||||
t.Fatalf("DiskCacheFullRatio=%v, want %v", st.DiskCacheFullRatio, tc.wantRatio)
|
||||
}
|
||||
if st.DiskCacheFullRatio < 0 || st.DiskCacheFullRatio > 1 {
|
||||
t.Fatalf("DiskCacheFullRatio=%v outside [0,1]", st.DiskCacheFullRatio)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Memory capacity is a plain passthrough, and capacity survives Reset
|
||||
// (re-derived by GetMetrics, like MemoryCacheSize/DiskTierReady).
|
||||
m := NewMetrics()
|
||||
m.SetMemoryCacheCapacity(4096)
|
||||
if got := m.GetStats().MemoryCacheCapacity; got != 4096 {
|
||||
t.Fatalf("MemoryCacheCapacity=%d, want 4096", got)
|
||||
}
|
||||
m.Reset()
|
||||
if got := m.GetStats().MemoryCacheCapacity; got != 4096 {
|
||||
t.Fatalf("MemoryCacheCapacity=%d after Reset, want 4096 (config snapshot, like size gauges)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func assertHelpType(t *testing.T, body, name, typ string) {
|
||||
t.Helper()
|
||||
help := "# HELP " + name + " "
|
||||
|
||||
Reference in New Issue
Block a user