Files
seaweedfs/weed/stats/metrics_ec_rebuild_test.go
d0db94c34a feat(metrics): Add EC rebuild/reconstruct Prometheus metrics (#10124)
* Review comment removed unnecessary success and failure count

* fix: use Gather.Gather() with seeded counter for EC rebuild registration test

- Restore Gather.Gather() to verify MustRegister calls as requested in review
- Seed VolumeServerECRebuildCounter before gathering because CounterVec
  only appears after at least one label value is observed
- Use correct fully-qualified metric names (SeaweedFS_volumeServer_*)

* fix: remove preflight checkEcVolumeStatus failure from ec_rebuild_total counter

ec_rebuild_total should only reflect actual rebuild execution failures
(from RebuildEcFiles / RebuildEcxFile), not scan/precheck failures in
the volume status loop. The error is still returned to the caller;
only the misleading counter increment was removed.

* Review comment removed unnecessary observe

* label EC rebuild duration histogram by result

Without a result label, fast failures pull down the success-latency
quantiles shown on the EC Rebuild Duration panel. Make the histogram a
HistogramVec keyed by result, record success/failure through one
recordEcRebuild helper, and split the Grafana quantiles by (le, result).

* reset EC rebuild metric vecs in registration test

The HistogramVec needs a child before Gather emits it, so the test must
observe once; reset both vecs in cleanup so that sample doesn't leak into
other tests.

---------

Co-authored-by: Ubuntu User <ubuntu@example.com>
Co-authored-by: Chris Lu <chris.lu@gmail.com>
2026-06-27 22:01:36 -07:00

59 lines
1.6 KiB
Go

package stats
import (
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestECRebuildMetricsRegistered(t *testing.T) {
t.Cleanup(VolumeServerECRebuildCounter.Reset)
t.Cleanup(VolumeServerECRebuildHistogram.Reset)
// A HistogramVec emits no metric family until it has a child, so observe once.
VolumeServerECRebuildCounter.WithLabelValues("success").Inc()
VolumeServerECRebuildHistogram.WithLabelValues("success").Observe(0.1)
metrics, err := Gather.Gather()
if err != nil {
t.Fatalf("failed to gather metrics: %v", err)
}
counterFound := false
histogramFound := false
for _, mf := range metrics {
if mf.GetName() == "SeaweedFS_volumeServer_ec_rebuild_total" {
counterFound = true
}
if mf.GetName() == "SeaweedFS_volumeServer_ec_rebuild_seconds" {
histogramFound = true
}
}
if !counterFound {
t.Errorf("VolumeServerECRebuildCounter: metric not registered with Gather")
}
if !histogramFound {
t.Errorf("VolumeServerECRebuildHistogram: metric not registered with Gather")
}
}
func TestECRebuildCounterIncrement(t *testing.T) {
VolumeServerECRebuildCounter.Reset()
t.Cleanup(func() {
VolumeServerECRebuildCounter.Reset()
})
VolumeServerECRebuildCounter.WithLabelValues("success").Inc()
VolumeServerECRebuildCounter.WithLabelValues("failure").Inc()
got := testutil.ToFloat64(VolumeServerECRebuildCounter.WithLabelValues("success"))
if got != 1 {
t.Errorf("expected 1.0, got %f", got)
}
got = testutil.ToFloat64(VolumeServerECRebuildCounter.WithLabelValues("failure"))
if got != 1 {
t.Errorf("expected 1.0, got %f", got)
}
}