k8s.io/apiserver@v0.31.1/pkg/endpoints/discovery/aggregated/metrics_test.go (about)

     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 aggregated_test
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strings"
    23  	"testing"
    24  
    25  	"k8s.io/component-base/metrics/legacyregistry"
    26  	"k8s.io/component-base/metrics/testutil"
    27  
    28  	discoveryendpoint "k8s.io/apiserver/pkg/endpoints/discovery/aggregated"
    29  )
    30  
    31  func formatExpectedMetrics(aggregationCount int) io.Reader {
    32  	expected := ``
    33  	if aggregationCount > 0 {
    34  		expected = expected + `# HELP aggregator_discovery_aggregation_count_total [ALPHA] Counter of number of times discovery was aggregated
    35  # TYPE aggregator_discovery_aggregation_count_total counter
    36  aggregator_discovery_aggregation_count_total %d
    37  `
    38  	}
    39  	args := []any{}
    40  	if aggregationCount > 0 {
    41  		args = append(args, aggregationCount)
    42  	}
    43  	return strings.NewReader(fmt.Sprintf(expected, args...))
    44  }
    45  
    46  func TestBasicMetrics(t *testing.T) {
    47  	legacyregistry.Reset()
    48  	manager := discoveryendpoint.NewResourceManager("apis")
    49  
    50  	apis := fuzzAPIGroups(1, 3, 10)
    51  	manager.SetGroups(apis.Items)
    52  
    53  	interests := []string{"aggregator_discovery_aggregation_count_total"}
    54  
    55  	_, _, _ = fetchPath(manager, "application/json", discoveryPath, "")
    56  	// A single fetch should aggregate and increment regeneration counter.
    57  	if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, formatExpectedMetrics(1), interests...); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	_, _, _ = fetchPath(manager, "application/json", discoveryPath, "")
    61  	// Subsequent fetches should not reaggregate discovery.
    62  	if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, formatExpectedMetrics(1), interests...); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  
    67  func TestMetricsModified(t *testing.T) {
    68  	legacyregistry.Reset()
    69  	manager := discoveryendpoint.NewResourceManager("apis")
    70  
    71  	apis := fuzzAPIGroups(1, 3, 10)
    72  	manager.SetGroups(apis.Items)
    73  
    74  	interests := []string{"aggregator_discovery_aggregation_count_total"}
    75  
    76  	_, _, _ = fetchPath(manager, "application/json", discoveryPath, "")
    77  	// A single fetch should aggregate and increment regeneration counter.
    78  	if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, formatExpectedMetrics(1), interests...); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	// Update discovery document.
    83  	manager.SetGroups(fuzzAPIGroups(1, 3, 10).Items)
    84  	_, _, _ = fetchPath(manager, "application/json", discoveryPath, "")
    85  	// If the discovery content has changed, reaggregation should be performed.
    86  	if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, formatExpectedMetrics(2), interests...); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  }