Files
seaweedfs/weed/stats/metrics_bucket_quota_test.go
T
Chris Lu 582b7268f5 s3: export per-bucket quota and read-only state metrics (#9923)
The quota enforcement loop already computes each bucket's configured
quota and effective read-only flag every minute, but neither was
visible to monitoring, so operators could not alert before a bucket
flips read-only.

Add two gauges next to the existing bucket size metrics:

  SeaweedFS_s3_bucket_quota_bytes  configured quota; the series is only
                                   present while the quota is enabled,
                                   so size/quota utilization queries
                                   never divide by zero
  SeaweedFS_s3_bucket_read_only    1 when the bucket's location rule is
                                   read-only (over quota or manually
                                   locked), 0 otherwise

Both are cleaned up with the other per-bucket gauges on bucket
deletion and inactivity TTL.
2026-06-11 09:03:00 -07:00

43 lines
1.5 KiB
Go

package stats
import (
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestUpdateBucketQuotaMetrics(t *testing.T) {
bucket := "quota-test-bucket"
UpdateBucketQuotaMetrics(bucket, 1024, true)
if got := testutil.ToFloat64(S3BucketQuotaBytesGauge.WithLabelValues(bucket)); got != 1024 {
t.Errorf("quota gauge: got %.0f, want 1024", got)
}
if got := testutil.ToFloat64(S3BucketReadOnlyGauge.WithLabelValues(bucket)); got != 1 {
t.Errorf("read-only gauge: got %.0f, want 1", got)
}
UpdateBucketQuotaMetrics(bucket, 1024, false)
if got := testutil.ToFloat64(S3BucketReadOnlyGauge.WithLabelValues(bucket)); got != 0 {
t.Errorf("read-only gauge after clearing: got %.0f, want 0", got)
}
// disabled quota (negative) removes the quota series but keeps reporting read-only state
UpdateBucketQuotaMetrics(bucket, -1024, true)
if n := testutil.CollectAndCount(S3BucketQuotaBytesGauge); n != 0 {
t.Errorf("quota series after disabling: got %d, want 0", n)
}
if got := testutil.ToFloat64(S3BucketReadOnlyGauge.WithLabelValues(bucket)); got != 1 {
t.Errorf("read-only gauge with disabled quota: got %.0f, want 1", got)
}
UpdateBucketQuotaMetrics(bucket, 2048, true)
DeleteBucketMetrics(bucket)
if n := testutil.CollectAndCount(S3BucketQuotaBytesGauge); n != 0 {
t.Errorf("quota series after bucket deletion: got %d, want 0", n)
}
if n := testutil.CollectAndCount(S3BucketReadOnlyGauge); n != 0 {
t.Errorf("read-only series after bucket deletion: got %d, want 0", n)
}
}