Files
seaweedfs/telemetry/server/storage/metrics_test.go
T
Chris Lu c8cafd8a1a telemetry: fix total disk usage over time (#10476)
* telemetry: total disk usage over time counted each cluster on one day

GetMetrics aggregated s.instances, which holds only each cluster's most
recent report. Every cluster therefore landed in a single date bucket --
the day it last reported on -- so the chart plotted the disk usage of
clusters that went silent that day, and piled the whole live fleet onto
today. Aggregate the daily histories instead, reusing the day alignment
that the per-cluster size series already does.

* telemetry: don't pad the charts with days the server has no history for

The dashboard asks for 30 days, but daily history only starts when a
server first collects it, so the charts opened on a run of zeros and then
jumped -- reading as a fleet that appeared overnight. Start the window at
the oldest sample on hand when it is younger than the requested range.
2026-07-28 16:22:29 -07:00

118 lines
3.9 KiB
Go

package storage
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
)
// Every cluster counts towards every day it reported on, not just towards the
// one day it last reported on.
func TestGetMetricsSumsEachDay(t *testing.T) {
s := newPrometheusStorage(prometheus.NewRegistry())
// Reported every day of the window.
seedSamples(s, "daily", HistorySample{TotalDiskBytes: 300, VolumeServerCount: 3},
-9, -8, -7, -6, -5, -4, -3, -2, -1, 0)
// Stopped reporting past the active window: counts on its own days only.
seedSamples(s, "gone", HistorySample{TotalDiskBytes: 900, VolumeServerCount: 9}, -9, -8)
metrics, err := s.GetMetrics(10)
if err != nil {
t.Fatal(err)
}
if got := metrics["dates"].([]string); len(got) != 10 {
t.Fatalf("dates = %v, want 10 days", got)
}
if got := metrics["disk_usage"].([]uint64); !equal(got,
[]uint64{1200, 1200, 300, 300, 300, 300, 300, 300, 300, 300}) {
t.Errorf("disk_usage = %v, want the fleet total per day", got)
}
if got := metrics["server_counts"].([]int64); !equalInt64(got,
[]int64{12, 12, 3, 3, 3, 3, 3, 3, 3, 3}) {
t.Errorf("server_counts = %v, want the fleet total per day", got)
}
}
// A cluster that skipped a day keeps its size on that day rather than dipping
// the fleet total to zero and back.
func TestGetMetricsCarriesSkippedDaysForward(t *testing.T) {
s := newPrometheusStorage(prometheus.NewRegistry())
seedSamples(s, "gappy", HistorySample{TotalDiskBytes: 500, VolumeServerCount: 5}, -3, -1)
metrics, err := s.GetMetrics(4)
if err != nil {
t.Fatal(err)
}
if got := metrics["disk_usage"].([]uint64); !equal(got, []uint64{500, 500, 500, 500}) {
t.Errorf("disk_usage = %v, want the skipped days carried forward", got)
}
}
// The window starts at the oldest sample the server actually has, so a server
// with less history than the caller asked for does not report a fleet that grew
// out of nothing on its first day of data.
func TestGetMetricsWindowStartsAtOldestSample(t *testing.T) {
s := newPrometheusStorage(prometheus.NewRegistry())
seedSamples(s, "recent", HistorySample{TotalDiskBytes: 100, VolumeServerCount: 1}, -2, -1, 0)
metrics, err := s.GetMetrics(30)
if err != nil {
t.Fatal(err)
}
if got := metrics["dates"].([]string); len(got) != 3 {
t.Errorf("dates = %v, want the 3 days with history, not 30", got)
}
if got := metrics["disk_usage"].([]uint64); !equal(got, []uint64{100, 100, 100}) {
t.Errorf("disk_usage = %v, want no leading zero days", got)
}
// History reaching past the requested window still clips to the window.
seedSamples(s, "old", HistorySample{TotalDiskBytes: 50, VolumeServerCount: 1}, -40)
metrics, err = s.GetMetrics(10)
if err != nil {
t.Fatal(err)
}
if got := metrics["dates"].([]string); len(got) != 10 {
t.Errorf("dates = %v, want 10 days", got)
}
}
// "Total Disk Usage Over Time" and the stacked "Cluster Sizes Over Time" sit on
// the same dashboard, so their last day has to add up to the same number.
func TestGetMetricsAgreesWithClusterSizes(t *testing.T) {
s := newPrometheusStorage(prometheus.NewRegistry())
seedSamples(s, "daily", HistorySample{TotalDiskBytes: 300, VolumeServerCount: 3},
-9, -8, -7, -6, -5, -4, -3, -2, -1, 0)
seedSamples(s, "lagging", HistorySample{TotalDiskBytes: 200, VolumeServerCount: 2}, -2)
seedSamples(s, "gone", HistorySample{TotalDiskBytes: 900, VolumeServerCount: 9}, -9, -8)
metrics, err := s.GetMetrics(10)
if err != nil {
t.Fatal(err)
}
disk := metrics["disk_usage"].([]uint64)
sizes := s.GetClusterSizeSeries(10, 1) // limit forces the Other fold-in too
if got, want := disk[len(disk)-1], sizes.TotalDisk; got != want {
t.Errorf("metrics last day = %d, cluster sizes total = %d", got, want)
}
if len(disk) != len(sizes.Dates) {
t.Errorf("metrics has %d days, cluster sizes has %d", len(disk), len(sizes.Dates))
}
}
func equalInt64(a, b []int64) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}