...
1
16
17 package restclient
18
19 import (
20 "context"
21 "strings"
22 "testing"
23
24 "k8s.io/client-go/tools/metrics"
25 "k8s.io/component-base/metrics/legacyregistry"
26 "k8s.io/component-base/metrics/testutil"
27 )
28
29 func TestClientGOMetrics(t *testing.T) {
30 tests := []struct {
31 description string
32 name string
33 metric interface{}
34 update func()
35 want string
36 }{
37 {
38 description: "Number of HTTP requests, partitioned by status code, verb, and host.",
39 name: "rest_client_requests_total",
40 metric: requestResult,
41 update: func() {
42 metrics.RequestResult.Increment(context.TODO(), "200", "POST", "www.foo.com")
43 },
44 want: `
45 # HELP rest_client_requests_total [ALPHA] Number of HTTP requests, partitioned by status code, method, and host.
46 # TYPE rest_client_requests_total counter
47 rest_client_requests_total{code="200",host="www.foo.com",method="POST"} 1
48 `,
49 },
50 {
51 description: "Number of request retries, partitioned by status code, verb, and host.",
52 name: "rest_client_request_retries_total",
53 metric: requestRetry,
54 update: func() {
55 metrics.RequestRetry.IncrementRetry(context.TODO(), "500", "GET", "www.bar.com")
56 },
57 want: `
58 # HELP rest_client_request_retries_total [ALPHA] Number of request retries, partitioned by status code, verb, and host.
59 # TYPE rest_client_request_retries_total counter
60 rest_client_request_retries_total{code="500",host="www.bar.com",verb="GET"} 1
61 `,
62 },
63 }
64
65
66
67 for _, test := range tests {
68 t.Run(test.description, func(t *testing.T) {
69 resetter, resettable := test.metric.(interface {
70 Reset()
71 })
72 if !resettable {
73 t.Fatalf("the metric must be resettaable: %s", test.name)
74 }
75
76
77
78
79 resetter.Reset()
80 test.update()
81
82 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), test.name); err != nil {
83 t.Fatal(err)
84 }
85 })
86 }
87 }
88
View as plain text