...

Source file src/k8s.io/component-base/metrics/prometheus/restclient/metrics_test.go

Documentation: k8s.io/component-base/metrics/prometheus/restclient

     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 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  	// no need to register the metrics here, since the init function of
    66  	// the package registers all the client-go metrics.
    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  			// Since prometheus' gatherer is global, other tests may have updated
    77  			// metrics already, so we need to reset them prior to running this test.
    78  			// This also implies that we can't run this test in parallel with other tests.
    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