Skip to content

Commit 2f64d97

Browse files
jkh52k8s-publishing-bot
authored andcommitted
Fix konnectivity-client metric registration.
Kubernetes-commit: a9edc2c2191ae9b9fb527f9d65fa7d73406261ed
1 parent 05e39c7 commit 2f64d97

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

pkg/server/egressselector/egress_selector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
utilnet "k8s.io/apimachinery/pkg/util/net"
3737
"k8s.io/apiserver/pkg/apis/apiserver"
3838
egressmetrics "k8s.io/apiserver/pkg/server/egressselector/metrics"
39-
compbasemetrics "k8s.io/component-base/metrics"
39+
"k8s.io/component-base/metrics/legacyregistry"
4040
"k8s.io/component-base/tracing"
4141
"k8s.io/klog/v2"
4242
client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client"
@@ -45,7 +45,7 @@ import (
4545
var directDialer utilnet.DialFunc = http.DefaultTransport.(*http.Transport).DialContext
4646

4747
func init() {
48-
client.Metrics.RegisterMetrics(compbasemetrics.NewKubeRegistry().Registerer())
48+
client.Metrics.RegisterMetrics(legacyregistry.Registerer())
4949
}
5050

5151
// EgressSelector is the map of network context type to context dialer, for network egress.

pkg/server/egressselector/egress_selector_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package egressselector
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"net"
2324
"strings"
@@ -31,6 +32,9 @@ import (
3132
"k8s.io/component-base/metrics/legacyregistry"
3233
"k8s.io/component-base/metrics/testutil"
3334
testingclock "k8s.io/utils/clock/testing"
35+
clientmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/metrics"
36+
ccmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics"
37+
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
3438
)
3539

3640
type fakeEgressSelection struct {
@@ -272,5 +276,70 @@ func TestMetrics(t *testing.T) {
272276
}
273277
})
274278
}
279+
}
275280

281+
func TestKonnectivityClientMetrics(t *testing.T) {
282+
testcases := []struct {
283+
name string
284+
metrics []string
285+
trigger func()
286+
want string
287+
}{
288+
{
289+
name: "stream packets",
290+
metrics: []string{"konnectivity_network_proxy_client_stream_packets_total"},
291+
trigger: func() {
292+
clientmetrics.Metrics.ObservePacket(ccmetrics.SegmentFromClient, client.PacketType_DIAL_REQ)
293+
},
294+
want: `
295+
# HELP konnectivity_network_proxy_client_stream_packets_total Count of packets processed, by segment and packet type (example: from_client, DIAL_REQ)
296+
# TYPE konnectivity_network_proxy_client_stream_packets_total counter
297+
konnectivity_network_proxy_client_stream_packets_total{packet_type="DIAL_REQ",segment="from_client"} 1
298+
`,
299+
},
300+
{
301+
name: "stream errors",
302+
metrics: []string{"konnectivity_network_proxy_client_stream_errors_total"},
303+
trigger: func() {
304+
clientmetrics.Metrics.ObserveStreamError(ccmetrics.SegmentToClient, errors.New("example"), client.PacketType_DIAL_RSP)
305+
},
306+
want: `
307+
# HELP konnectivity_network_proxy_client_stream_errors_total Count of gRPC stream errors, by segment, grpc Code, packet type. (example: from_agent, Code.Unavailable, DIAL_RSP)
308+
# TYPE konnectivity_network_proxy_client_stream_errors_total counter
309+
konnectivity_network_proxy_client_stream_errors_total{code="Unknown",packet_type="DIAL_RSP",segment="to_client"} 1
310+
`,
311+
},
312+
{
313+
name: "dial failure",
314+
metrics: []string{"konnectivity_network_proxy_client_dial_failure_total"},
315+
trigger: func() {
316+
clientmetrics.Metrics.ObserveDialFailure(clientmetrics.DialFailureTimeout)
317+
},
318+
want: `
319+
# HELP konnectivity_network_proxy_client_dial_failure_total Number of dial failures observed, by reason (example: remote endpoint error)
320+
# TYPE konnectivity_network_proxy_client_dial_failure_total counter
321+
konnectivity_network_proxy_client_dial_failure_total{reason="timeout"} 1
322+
`,
323+
},
324+
{
325+
name: "client connections",
326+
metrics: []string{"konnectivity_network_proxy_client_client_connections"},
327+
trigger: func() {
328+
clientmetrics.Metrics.GetClientConnectionsMetric().WithLabelValues("dialing").Inc()
329+
},
330+
want: `
331+
# HELP konnectivity_network_proxy_client_client_connections Number of open client connections, by status (Example: dialing)
332+
# TYPE konnectivity_network_proxy_client_client_connections gauge
333+
konnectivity_network_proxy_client_client_connections{status="dialing"} 1
334+
`,
335+
},
336+
}
337+
for _, tc := range testcases {
338+
t.Run(tc.name, func(t *testing.T) {
339+
tc.trigger()
340+
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil {
341+
t.Errorf("GatherAndCompare error: %v", err)
342+
}
343+
})
344+
}
276345
}

0 commit comments

Comments
 (0)