Copilot commented on code in PR #962:
URL: https://github.com/apache/dubbo-go-pixiu/pull/962#discussion_r3371173860


##########
pkg/server/cluster_manager_test.go:
##########
@@ -1111,3 +1118,118 @@ func healthCheckerAddresses(runtime *cluster.Cluster) 
[]string {
        }
        return addrs
 }
+
+// TestStaticClusterSnapshotMetricsRecordedAtStartup verifies that snapshot
+// publication metrics emitted during static cluster initialization land on the
+// real meter provider. This test models production startup ordering: clusters
+// are created during CreateDefaultClusterManager (called from initialize), and
+// the OTel provider must be installed BEFORE that point so the initial 
snapshot
+// publish (the only guaranteed emission for steady-state clusters) is 
recorded.
+//
+// Regression guard for: if registerOtelMetricMeter is moved back after cluster
+// construction, static clusters' initial publish lands on the no-op delegating
+// provider, and the counter/gauges remain empty in steady state.
+func TestStaticClusterSnapshotMetricsRecordedAtStartup(t *testing.T) {
+       // Step 1: Install a ManualReader meter provider BEFORE cluster 
construction.
+       // This models the corrected startup order: 
registerOtelMetricMeter(bs.Metric)
+       // is called in Start(bs) before server.initialize(bs).
+       reader := installClusterSnapshotMetricsReader(t)
+
+       // Step 2: Construct a cluster manager with static clusters, simulating 
what
+       // happens during initialize → CreateDefaultClusterManager.
+       staticCluster := testCluster("static-metrics-test", 
model.LoadBalancerRoundRobin, []*model.Endpoint{
+               testEndpoint("ep-1", "127.0.0.1", 19001),
+               testEndpoint("ep-2", "127.0.0.1", 19002),
+       })
+       _ = CreateDefaultClusterManager(&model.Bootstrap{
+               StaticResources: model.StaticResources{
+                       Clusters: []*model.ClusterConfig{staticCluster},
+               },
+       })
+
+       // Step 3: Verify the initial snapshot publish was recorded. NewCluster 
calls
+       // RefreshEndpointsFrom, which publishes once. That single emission 
must land
+       // on the real provider for steady-state clusters (those with no health 
flips
+       // or registry churn) to have any recorded metrics at all.
+       metrics := collectClusterSnapshotMetrics(t, reader)
+
+       publishTotal, ok := metrics["pixiu_cluster_snapshot_publish_total"]
+       assert.True(t, ok, "publish total metric missing")
+       assert.Equal(t, int64(1), sumForClusterMetric(t, publishTotal, 
"static-metrics-test"),
+               "static cluster initial publish must increment the counter")
+
+       endpointCount, ok := metrics["pixiu_cluster_snapshot_endpoint_count"]
+       assert.True(t, ok, "endpoint count gauge missing")
+       assert.Equal(t, int64(2), gaugeForClusterMetric(t, endpointCount, 
"static-metrics-test"),
+               "endpoint count gauge must reflect the initial snapshot size")
+
+       healthyCount, ok := 
metrics["pixiu_cluster_snapshot_healthy_endpoint_count"]
+       assert.True(t, ok, "healthy endpoint count gauge missing")
+       assert.Equal(t, int64(2), gaugeForClusterMetric(t, healthyCount, 
"static-metrics-test"),
+               "healthy endpoint count gauge must reflect the initial snapshot 
size")
+}
+
+// installClusterSnapshotMetricsReader installs a ManualReader meter provider
+// for snapshot metrics testing and resets the global instrument state on 
cleanup.
+// This helper mutates process-global state (otel.SetMeterProvider and the
+// pkg/cluster instrument variables), so tests using it must not call 
t.Parallel().

Review Comment:
   The helper comment says it "resets the global instrument state" / 
"pkg/cluster instrument variables", but the implementation only swaps the 
global MeterProvider and restores it in Cleanup. Either update the comment to 
match what the helper actually does, or add an explicit test hook in 
pkg/cluster to reset snapshot instrument globals if that’s required here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to