Skip to content

Commit 0a4a49c

Browse files
committed
Add Server count metric
this patch adds server count metric fixes: #521 Signed-off-by: yolossn <[email protected]>
1 parent 70cbe12 commit 0a4a49c

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

e2e/metrics_assertions_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,29 @@ func assertAgentKnownServerCount(expectedServerCount int) func(context.Context,
138138
return ctx
139139
}
140140
}
141+
142+
// assertServerCountMetric asserts that the server count metric is set to the expected value.
143+
func assertServerCountMetric(expectedServerCount int) func(context.Context, *testing.T, *envconf.Config) context.Context {
144+
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
145+
client := cfg.Client()
146+
147+
serverPods := &corev1.PodList{}
148+
err := client.Resources().List(ctx, serverPods, resources.WithLabelSelector("k8s-app=konnectivity-server"))
149+
if err != nil {
150+
t.Fatalf("couldn't get server pods (label selector 'k8s-app=konnectivity-server'): %v", err)
151+
}
152+
153+
for _, serverPod := range serverPods.Items {
154+
serverCount, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), serverPod.Namespace, serverPod.Name, 8095, "konnectivity_network_proxy_server_server_count")
155+
if err != nil {
156+
t.Fatalf("couldn't get server metric 'konnectivity_network_proxy_server_server_count' for pod %v: %v", serverPod.Name, err)
157+
}
158+
159+
if serverCount != expectedServerCount {
160+
t.Errorf("incorrect server count metric (want: %v, got: %v)", expectedServerCount, serverCount)
161+
}
162+
}
163+
164+
return ctx
165+
}
166+
}

pkg/server/metrics/metrics.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type ServerMetrics struct {
6464
leaseDeletes *prometheus.CounterVec
6565
leaseListLatencies *prometheus.HistogramVec
6666
leaseLists *prometheus.CounterVec
67+
serverCount prometheus.Gauge
6768
}
6869

6970
// newServerMetrics create a new ServerMetrics, configured with default metric names.
@@ -209,6 +210,14 @@ func newServerMetrics() *ServerMetrics {
209210
},
210211
[]string{"http_status_code", "reason"},
211212
)
213+
serverCount := prometheus.NewGauge(
214+
prometheus.GaugeOpts{
215+
Namespace: Namespace,
216+
Subsystem: Subsystem,
217+
Name: "server_count",
218+
Help: "Number of proxy server instances in the cluster",
219+
},
220+
)
212221
streamPackets := commonmetrics.MakeStreamPacketsTotalMetric(Namespace, Subsystem)
213222
streamErrors := commonmetrics.MakeStreamErrorsTotalMetric(Namespace, Subsystem)
214223
prometheus.MustRegister(endpointLatencies)
@@ -228,6 +237,7 @@ func newServerMetrics() *ServerMetrics {
228237
prometheus.MustRegister(leaseDeletes)
229238
prometheus.MustRegister(leaseListLatencies)
230239
prometheus.MustRegister(leaseLists)
240+
prometheus.MustRegister(serverCount)
231241
return &ServerMetrics{
232242
endpointLatencies: endpointLatencies,
233243
frontendLatencies: frontendLatencies,
@@ -246,6 +256,7 @@ func newServerMetrics() *ServerMetrics {
246256
leaseDeletes: leaseDeletes,
247257
leaseListLatencies: leaseListLatencies,
248258
leaseLists: leaseLists,
259+
serverCount: serverCount,
249260
}
250261
}
251262

@@ -315,6 +326,11 @@ func (s *ServerMetrics) SetEstablishedConnCount(count int) {
315326
s.establishedConns.WithLabelValues().Set(float64(count))
316327
}
317328

329+
// SetServerCount sets the number of proxy server instances in the cluster.
330+
func (s *ServerMetrics) SetServerCount(count int) {
331+
s.serverCount.Set(float64(count))
332+
}
333+
318334
// FullRecvChannel retrieves the metric for counting full receive channels.
319335
func (s *ServerMetrics) FullRecvChannel(serviceMethod string) prometheus.Gauge {
320336
return s.fullRecvChannels.With(prometheus.Labels{"service_method": serviceMethod})

pkg/server/metrics/metrics_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"testing"
21+
22+
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/testutil"
24+
)
25+
26+
// TestServerCountMetric tests the server count metric.
27+
func TestServerCountMetric(t *testing.T) {
28+
// Reset metrics to ensure clean state
29+
Metrics.Reset()
30+
31+
// Test setting server count
32+
expectedCount := 3
33+
Metrics.SetServerCount(expectedCount)
34+
35+
// Verify the metric value
36+
actualCount := testutil.ToFloat64(Metrics.serverCount)
37+
if actualCount != float64(expectedCount) {
38+
t.Errorf("Expected server count %d, got %f", expectedCount, actualCount)
39+
}
40+
41+
// Test updating server count
42+
newCount := 5
43+
Metrics.SetServerCount(newCount)
44+
actualCount = testutil.ToFloat64(Metrics.serverCount)
45+
if actualCount != float64(newCount) {
46+
t.Errorf("Expected updated server count %d, got %f", newCount, actualCount)
47+
}
48+
}
49+
50+
// TestServerCountMetricRegistration tests the registration of the server count metric.
51+
func TestServerCountMetricRegistration(t *testing.T) {
52+
// Verify the metric is properly registered
53+
registry := prometheus.NewRegistry()
54+
registry.MustRegister(Metrics.serverCount)
55+
56+
// Set a value and check if it's accessible
57+
Metrics.SetServerCount(2)
58+
actualCount := testutil.ToFloat64(Metrics.serverCount)
59+
if actualCount != 2.0 {
60+
t.Errorf("Expected server count 2, got %f", actualCount)
61+
}
62+
}

pkg/server/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ func NewProxyServer(serverID string, proxyStrategies []proxystrategies.ProxyStra
389389
}
390390
}
391391

392+
metrics.Metrics.SetServerCount(serverCount)
393+
392394
return &ProxyServer{
393395
established: make(map[string](map[int64]*ProxyClientConnection)),
394396
PendingDial: NewPendingDialManager(),

0 commit comments

Comments
 (0)