11package observability_test
22
33import (
4+ "fmt"
45 "strings"
56 "testing"
67
@@ -16,39 +17,36 @@ import (
1617// observations and that the histogram is correctly exported via text format.
1718func TestReconcileDurationObserve (t * testing.T ) {
1819 reg := prometheus .NewRegistry ()
20+ observability .ReconcileDuration .Reset ()
21+ reg .MustRegister (observability .ReconcileDuration )
1922
20- hist := prometheus .NewHistogramVec (
21- prometheus.HistogramOpts {
22- Name : "agentrax_reconcile_duration_seconds" ,
23- Help : "Duration of AgentDeployment reconcile loops in seconds." ,
24- Buckets : prometheus .DefBuckets ,
25- },
26- []string {"controller" , "tenant" },
27- )
28- reg .MustRegister (hist )
23+ observability .ReconcileDuration .WithLabelValues ("agentdeployment" , "tenant-test" ).Observe (0.5 )
24+ observability .ReconcileDuration .WithLabelValues ("agentdeployment" , "tenant-test" ).Observe (1.5 )
2925
30- hist .WithLabelValues ("agentdeployment" , "tenant-test" ).Observe (0.5 )
31- hist .WithLabelValues ("agentdeployment" , "tenant-test" ).Observe (1.5 )
32-
33- // Verify via text format: sample_count should be 2.
3426 expected := `
3527 # HELP agentrax_reconcile_duration_seconds Duration of AgentDeployment reconcile loops in seconds.
3628 # TYPE agentrax_reconcile_duration_seconds histogram
29+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.005"} 0
30+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.01"} 0
31+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.025"} 0
32+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.05"} 0
33+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.1"} 0
34+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.25"} 0
35+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="0.5"} 1
36+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="1"} 1
37+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="2.5"} 2
38+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="5"} 2
39+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="10"} 2
40+ agentrax_reconcile_duration_seconds_bucket{controller="agentdeployment",tenant="tenant-test",le="+Inf"} 2
41+ agentrax_reconcile_duration_seconds_sum{controller="agentdeployment",tenant="tenant-test"} 2
42+ agentrax_reconcile_duration_seconds_count{controller="agentdeployment",tenant="tenant-test"} 2
3743 `
38- err := testutil .GatherAndCompare (reg , strings .NewReader (expected ),
39- "agentrax_reconcile_duration_seconds" )
40- // We only check prefix presence, not exact bucket values — a full comparison
41- // would require listing all bucket boundaries. Use Count instead.
42- _ = err
44+ err := testutil .GatherAndCompare (reg , strings .NewReader (expected ), "agentrax_reconcile_duration_seconds" )
45+ require .NoError (t , err )
4346
4447 count , err := testutil .GatherAndCount (reg , "agentrax_reconcile_duration_seconds" )
4548 require .NoError (t , err )
46- // GatherAndCount returns the number of MetricFamily + all their metrics
47- // (buckets + sum + count); assert > 0 to prove the histogram emitted output.
4849 assert .Greater (t , count , 0 , "expected histogram to produce at least one metric series" )
49-
50- // Verify the package-level var is registered and usable.
51- _ = observability .ReconcileDuration
5250}
5351
5452// TestQuotaUsageRatioSet verifies that QuotaUsageRatio records the expected
@@ -68,22 +66,20 @@ func TestQuotaUsageRatioSet(t *testing.T) {
6866 for _ , tc := range tests {
6967 t .Run (tc .name , func (t * testing.T ) {
7068 reg := prometheus .NewRegistry ()
71- gauge := prometheus .NewGaugeVec (
72- prometheus.GaugeOpts {
73- Name : "agentrax_tenant_quota_usage_ratio" ,
74- Help : "Current replica usage as a fraction of maxTotalReplicas." ,
75- },
76- []string {"tenant" },
77- )
78- reg .MustRegister (gauge )
69+ observability .QuotaUsageRatio .Reset ()
70+ reg .MustRegister (observability .QuotaUsageRatio )
7971
80- gauge .WithLabelValues (tc .tenant ).Set (tc .value )
72+ observability . QuotaUsageRatio .WithLabelValues (tc .tenant ).Set (tc .value )
8173
82- got := testutil .ToFloat64 (gauge .WithLabelValues (tc .tenant ))
74+ expected := fmt .Sprintf (`
75+ # HELP agentrax_tenant_quota_usage_ratio Current replica usage as a fraction of maxTotalReplicas (0.0–1.0) per tenant.
76+ # TYPE agentrax_tenant_quota_usage_ratio gauge
77+ agentrax_tenant_quota_usage_ratio{tenant="%s"} %g
78+ ` , tc .tenant , tc .value )
79+ require .NoError (t , testutil .GatherAndCompare (reg , strings .NewReader (expected ), "agentrax_tenant_quota_usage_ratio" ))
80+
81+ got := testutil .ToFloat64 (observability .QuotaUsageRatio .WithLabelValues (tc .tenant ))
8382 assert .InDelta (t , tc .expected , got , 1e-9 )
8483 })
8584 }
86-
87- // Verify the package-level var is registered and usable.
88- _ = observability .QuotaUsageRatio
8985}
0 commit comments