@@ -18,6 +18,7 @@ package egressselector
1818
1919import (
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
3640type 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