Debian Codegraph @

pkg/testing/metrics/metrics.go

raw symbols SCIP
1 /*
2 Copyright 2022 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 testing
18
19 import (
20 "fmt"
21 "strings"
22
23 "github.com/prometheus/client_golang/prometheus"
24 promtest "github.com/prometheus/client_golang/prometheus/testutil"
25 agent "sigs.k8s.io/apiserver-network-proxy/pkg/agent/metrics"
26 server "sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics"
27 )
28
29 const (
30 serverDialFailureHeader = `
31 # HELP konnectivity_network_proxy_server_dial_failure_count Number of dial failures observed. Multiple failures can occur for a single dial request.
32 # TYPE konnectivity_network_proxy_server_dial_failure_count counter`
33 serverDialFailureSample = `konnectivity_network_proxy_server_dial_failure_count{reason="%s"} %d`
34
35 serverPendingDialsHeader = `
36 # HELP konnectivity_network_proxy_server_pending_backend_dials Current number of pending backend dial requests
37 # TYPE konnectivity_network_proxy_server_pending_backend_dials gauge`
38 serverPendingDialsSample = `konnectivity_network_proxy_server_pending_backend_dials{} %d`
39
40 serverReadyBackendsHeader = `
41 # HELP konnectivity_network_proxy_server_ready_backend_connections Number of konnectivity agent connected to the proxy server
42 # TYPE konnectivity_network_proxy_server_ready_backend_connections gauge`
43 serverReadyBackendsSample = `konnectivity_network_proxy_server_ready_backend_connections{} %d`
44
45 serverEstablishedConnsHeader = `
46 # HELP konnectivity_network_proxy_server_established_connections Current number of established end-to-end connections (post-dial).
47 # TYPE konnectivity_network_proxy_server_established_connections gauge`
48 serverEstablishedConnsSample = `konnectivity_network_proxy_server_established_connections{} %d`
49
50 agentDialFailureHeader = `
51 # HELP konnectivity_network_proxy_agent_endpoint_dial_failure_total Number of failures dialing the remote endpoint, by reason (example: timeout).
52 # TYPE konnectivity_network_proxy_agent_endpoint_dial_failure_total counter`
53 agentDialFailureSample = `konnectivity_network_proxy_agent_endpoint_dial_failure_total{reason="%s"} %d`
54
55 agentEndpointConnections = `
56 # HELP konnectivity_network_proxy_agent_open_endpoint_connections Current number of open endpoint connections.
57 # TYPE konnectivity_network_proxy_agent_open_endpoint_connections gauge
58 konnectivity_network_proxy_agent_open_endpoint_connections %d`
59 )
60
61 var DefaultTester = &Tester{}
62 var _ ServerTester = DefaultTester
63 var _ AgentTester = DefaultTester
64
65 type Tester struct {
66 // Endpoint is the metrics endpoint to scrape metrics from. If it is empty, the in-process
67 // DefaultGatherer is used.
68 Endpoint string
69 }
70
71 type ServerTester interface {
72 ExpectServerDialFailures(map[server.DialFailureReason]int) error
73 ExpectServerDialFailure(server.DialFailureReason, int) error
74 ExpectServerPendingDials(int) error
75 ExpectServerReadyBackends(int) error
76 ExpectServerEstablishedConns(int) error
77 }
78
79 type AgentTester interface {
80 ExpectAgentDialFailures(map[agent.DialFailureReason]int) error
81 ExpectAgentDialFailure(agent.DialFailureReason, int) error
82 ExpectAgentEndpointConnections(int) error
83 }
84
85 func (t *Tester) ExpectServerDialFailures(expected map[server.DialFailureReason]int) error {
86 expect := serverDialFailureHeader + "\n"
87 for r, v := range expected {
88 expect += fmt.Sprintf(serverDialFailureSample+"\n", r, v)
89 }
90 return t.ExpectMetric(server.Namespace, server.Subsystem, "dial_failure_count", expect)
91 }
92
93 func (t *Tester) ExpectServerDialFailure(reason server.DialFailureReason, count int) error {
94 return t.ExpectServerDialFailures(map[server.DialFailureReason]int{reason: count})
95 }
96
97 func (t *Tester) ExpectServerPendingDials(v int) error {
98 expect := serverPendingDialsHeader + "\n"
99 expect += fmt.Sprintf(serverPendingDialsSample+"\n", v)
100 return t.ExpectMetric(server.Namespace, server.Subsystem, "pending_backend_dials", expect)
101 }
102
103 func (t *Tester) ExpectServerReadyBackends(v int) error {
104 expect := serverReadyBackendsHeader + "\n"
105 expect += fmt.Sprintf(serverReadyBackendsSample+"\n", v)
106 return t.ExpectMetric(server.Namespace, server.Subsystem, "ready_backend_connections", expect)
107 }
108
109 func (t *Tester) ExpectServerEstablishedConns(v int) error {
110 expect := serverEstablishedConnsHeader + "\n"
111 expect += fmt.Sprintf(serverEstablishedConnsSample+"\n", v)
112 return t.ExpectMetric(server.Namespace, server.Subsystem, "established_connections", expect)
113 }
114
115 func (t *Tester) ExpectAgentDialFailures(expected map[agent.DialFailureReason]int) error {
116 expect := agentDialFailureHeader + "\n"
117 for r, v := range expected {
118 expect += fmt.Sprintf(agentDialFailureSample+"\n", r, v)
119 }
120 return t.ExpectMetric(agent.Namespace, agent.Subsystem, "endpoint_dial_failure_total", expect)
121 }
122
123 func (t *Tester) ExpectAgentDialFailure(reason agent.DialFailureReason, count int) error {
124 return t.ExpectAgentDialFailures(map[agent.DialFailureReason]int{reason: count})
125 }
126
127 func (t *Tester) ExpectAgentEndpointConnections(count int) error {
128 expect := fmt.Sprintf(agentEndpointConnections+"\n", count)
129 return t.ExpectMetric(agent.Namespace, agent.Subsystem, "open_endpoint_connections", expect)
130 }
131
132 func (t *Tester) ExpectMetric(namespace, subsystem, name, expected string) error {
133 fqName := prometheus.BuildFQName(namespace, subsystem, name)
134 if t.Endpoint == "" {
135 return promtest.GatherAndCompare(prometheus.DefaultGatherer, strings.NewReader(expected), fqName)
136 }
137 return promtest.ScrapeAndCompare(t.Endpoint, strings.NewReader(expected), fqName)
138 }