...

Source file src/k8s.io/component-base/metrics/summary_test.go

Documentation: k8s.io/component-base/metrics

     1  /*
     2  Copyright 2019 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 metrics
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/blang/semver/v4"
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	apimachineryversion "k8s.io/apimachinery/pkg/version"
    26  )
    27  
    28  func TestSummary(t *testing.T) {
    29  	v115 := semver.MustParse("1.15.0")
    30  	var tests = []struct {
    31  		desc string
    32  		*SummaryOpts
    33  		registryVersion     *semver.Version
    34  		expectedMetricCount int
    35  		expectedHelp        string
    36  	}{
    37  		{
    38  			desc: "Test non deprecated",
    39  			SummaryOpts: &SummaryOpts{
    40  				Namespace:      "namespace",
    41  				Name:           "metric_test_name",
    42  				Subsystem:      "subsystem",
    43  				Help:           "summary help message",
    44  				StabilityLevel: ALPHA,
    45  			},
    46  			registryVersion:     &v115,
    47  			expectedMetricCount: 1,
    48  			expectedHelp:        "[ALPHA] summary help message",
    49  		},
    50  		{
    51  			desc: "Test deprecated",
    52  			SummaryOpts: &SummaryOpts{
    53  				Namespace:         "namespace",
    54  				Name:              "metric_test_name",
    55  				Subsystem:         "subsystem",
    56  				Help:              "summary help message",
    57  				DeprecatedVersion: "1.15.0",
    58  				StabilityLevel:    ALPHA,
    59  			},
    60  			registryVersion:     &v115,
    61  			expectedMetricCount: 1,
    62  			expectedHelp:        "[ALPHA] (Deprecated since 1.15.0) summary help message",
    63  		},
    64  		{
    65  			desc: "Test hidden",
    66  			SummaryOpts: &SummaryOpts{
    67  				Namespace:         "namespace",
    68  				Name:              "metric_test_name",
    69  				Subsystem:         "subsystem",
    70  				Help:              "summary help message",
    71  				DeprecatedVersion: "1.14.0",
    72  			},
    73  			registryVersion:     &v115,
    74  			expectedMetricCount: 0,
    75  			expectedHelp:        "summary help message",
    76  		},
    77  	}
    78  
    79  	for _, test := range tests {
    80  		t.Run(test.desc, func(t *testing.T) {
    81  			registry := newKubeRegistry(apimachineryversion.Info{
    82  				Major:      "1",
    83  				Minor:      "15",
    84  				GitVersion: "v1.15.0-alpha-1.12345",
    85  			})
    86  			c := NewSummary(test.SummaryOpts)
    87  			registry.MustRegister(c)
    88  
    89  			ms, err := registry.Gather()
    90  			assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
    91  			assert.Nil(t, err, "Gather failed %v", err)
    92  
    93  			for _, metric := range ms {
    94  				assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
    95  			}
    96  
    97  			// let's increment the counter and verify that the metric still works
    98  			c.Observe(1)
    99  			c.Observe(2)
   100  			c.Observe(3)
   101  			c.Observe(1.5)
   102  			expected := 4
   103  			ms, err = registry.Gather()
   104  			assert.Nil(t, err, "Gather failed %v", err)
   105  
   106  			for _, mf := range ms {
   107  				for _, m := range mf.GetMetric() {
   108  					assert.Equalf(t, expected, int(m.GetSummary().GetSampleCount()), "Got %v, want %v as the sample count", m.GetHistogram().GetSampleCount(), expected)
   109  				}
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestSummaryVec(t *testing.T) {
   116  	v115 := semver.MustParse("1.15.0")
   117  	var tests = []struct {
   118  		desc string
   119  		*SummaryOpts
   120  		labels              []string
   121  		registryVersion     *semver.Version
   122  		expectedMetricCount int
   123  		expectedHelp        string
   124  	}{
   125  		{
   126  			desc: "Test non deprecated",
   127  			SummaryOpts: &SummaryOpts{
   128  				Namespace: "namespace",
   129  				Name:      "metric_test_name",
   130  				Subsystem: "subsystem",
   131  				Help:      "summary help message",
   132  			},
   133  			labels:              []string{"label_a", "label_b"},
   134  			registryVersion:     &v115,
   135  			expectedMetricCount: 1,
   136  			expectedHelp:        "[ALPHA] summary help message",
   137  		},
   138  		{
   139  			desc: "Test deprecated",
   140  			SummaryOpts: &SummaryOpts{
   141  				Namespace:         "namespace",
   142  				Name:              "metric_test_name",
   143  				Subsystem:         "subsystem",
   144  				Help:              "summary help message",
   145  				DeprecatedVersion: "1.15.0",
   146  			},
   147  			labels:              []string{"label_a", "label_b"},
   148  			registryVersion:     &v115,
   149  			expectedMetricCount: 1,
   150  			expectedHelp:        "[ALPHA] (Deprecated since 1.15.0) summary help message",
   151  		},
   152  		{
   153  			desc: "Test hidden",
   154  			SummaryOpts: &SummaryOpts{
   155  				Namespace:         "namespace",
   156  				Name:              "metric_test_name",
   157  				Subsystem:         "subsystem",
   158  				Help:              "summary help message",
   159  				DeprecatedVersion: "1.14.0",
   160  			},
   161  			labels:              []string{"label_a", "label_b"},
   162  			registryVersion:     &v115,
   163  			expectedMetricCount: 0,
   164  			expectedHelp:        "summary help message",
   165  		},
   166  	}
   167  
   168  	for _, test := range tests {
   169  		t.Run(test.desc, func(t *testing.T) {
   170  			registry := newKubeRegistry(apimachineryversion.Info{
   171  				Major:      "1",
   172  				Minor:      "15",
   173  				GitVersion: "v1.15.0-alpha-1.12345",
   174  			})
   175  			c := NewSummaryVec(test.SummaryOpts, test.labels)
   176  			registry.MustRegister(c)
   177  			c.WithLabelValues("1", "2").Observe(1.0)
   178  			ms, err := registry.Gather()
   179  			assert.Equalf(t, test.expectedMetricCount, len(ms), "Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount)
   180  			assert.Nil(t, err, "Gather failed %v", err)
   181  
   182  			for _, metric := range ms {
   183  				assert.Equalf(t, test.expectedHelp, metric.GetHelp(), "Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp)
   184  			}
   185  
   186  			// let's increment the counter and verify that the metric still works
   187  			c.WithLabelValues("1", "3").Observe(1.0)
   188  			c.WithLabelValues("2", "3").Observe(1.0)
   189  			ms, err = registry.Gather()
   190  			assert.Nil(t, err, "Gather failed %v", err)
   191  
   192  			for _, mf := range ms {
   193  				assert.Equalf(t, 3, len(mf.GetMetric()), "Got %v metrics, wanted 2 as the count", len(mf.GetMetric()))
   194  				for _, m := range mf.GetMetric() {
   195  					assert.Equalf(t, uint64(1), m.GetSummary().GetSampleCount(), "Got %v metrics, wanted 1 as the summary sample count", m.GetSummary().GetSampleCount())
   196  				}
   197  			}
   198  		})
   199  	}
   200  }
   201  
   202  func TestSummaryWithLabelValueAllowList(t *testing.T) {
   203  	labelAllowValues := map[string]string{
   204  		"namespace_subsystem_metric_allowlist_test,label_a": "allowed",
   205  	}
   206  	labels := []string{"label_a", "label_b"}
   207  	opts := &SummaryOpts{
   208  		Namespace: "namespace",
   209  		Name:      "metric_allowlist_test",
   210  		Subsystem: "subsystem",
   211  	}
   212  	var tests = []struct {
   213  		desc               string
   214  		labelValues        [][]string
   215  		expectMetricValues map[string]int
   216  	}{
   217  		{
   218  			desc:        "Test no unexpected input",
   219  			labelValues: [][]string{{"allowed", "b1"}, {"allowed", "b2"}},
   220  			expectMetricValues: map[string]int{
   221  				"allowed b1": 1,
   222  				"allowed b2": 1,
   223  			},
   224  		},
   225  		{
   226  			desc:        "Test unexpected input",
   227  			labelValues: [][]string{{"allowed", "b1"}, {"not_allowed", "b1"}},
   228  			expectMetricValues: map[string]int{
   229  				"allowed b1":    1,
   230  				"unexpected b1": 1,
   231  			},
   232  		},
   233  	}
   234  
   235  	for _, test := range tests {
   236  		t.Run(test.desc, func(t *testing.T) {
   237  			SetLabelAllowListFromCLI(labelAllowValues)
   238  			registry := newKubeRegistry(apimachineryversion.Info{
   239  				Major:      "1",
   240  				Minor:      "15",
   241  				GitVersion: "v1.15.0-alpha-1.12345",
   242  			})
   243  			c := NewSummaryVec(opts, labels)
   244  			registry.MustRegister(c)
   245  
   246  			for _, lv := range test.labelValues {
   247  				c.WithLabelValues(lv...).Observe(1.0)
   248  			}
   249  			mfs, err := registry.Gather()
   250  			assert.Nil(t, err, "Gather failed %v", err)
   251  
   252  			for _, mf := range mfs {
   253  				if *mf.Name != BuildFQName(opts.Namespace, opts.Subsystem, opts.Name) {
   254  					continue
   255  				}
   256  				mfMetric := mf.GetMetric()
   257  
   258  				for _, m := range mfMetric {
   259  					var aValue, bValue string
   260  					for _, l := range m.Label {
   261  						if *l.Name == "label_a" {
   262  							aValue = *l.Value
   263  						}
   264  						if *l.Name == "label_b" {
   265  							bValue = *l.Value
   266  						}
   267  					}
   268  					labelValuePair := aValue + " " + bValue
   269  					expectedValue, ok := test.expectMetricValues[labelValuePair]
   270  					assert.True(t, ok, "Got unexpected label values, lable_a is %v, label_b is %v", aValue, bValue)
   271  					actualValue := int(m.GetSummary().GetSampleCount())
   272  					assert.Equalf(t, expectedValue, actualValue, "Got %v, wanted %v as the count while setting label_a to %v and label b to %v", actualValue, expectedValue, aValue, bValue)
   273  				}
   274  			}
   275  		})
   276  	}
   277  }
   278  

View as plain text