github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/metric/metric_sink_test.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metric
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"k8s.io/heapster/metrics/core"
    24  )
    25  
    26  func makeBatches(now time.Time, key, otherKey string) (core.DataBatch, core.DataBatch, core.DataBatch) {
    27  	batch1 := core.DataBatch{
    28  		Timestamp: now.Add(-180 * time.Second),
    29  		MetricSets: map[string]*core.MetricSet{
    30  			key: {
    31  				Labels: map[string]string{
    32  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    33  				},
    34  				MetricValues: map[string]core.MetricValue{
    35  					"m1": {
    36  						ValueType:  core.ValueInt64,
    37  						MetricType: core.MetricGauge,
    38  						IntValue:   60,
    39  					},
    40  					"m2": {
    41  						ValueType:  core.ValueInt64,
    42  						MetricType: core.MetricGauge,
    43  						IntValue:   666,
    44  					},
    45  				},
    46  			},
    47  		},
    48  	}
    49  
    50  	batch2 := core.DataBatch{
    51  		Timestamp: now.Add(-60 * time.Second),
    52  		MetricSets: map[string]*core.MetricSet{
    53  			key: {
    54  				Labels: map[string]string{
    55  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    56  				},
    57  				MetricValues: map[string]core.MetricValue{
    58  					"m1": {
    59  						ValueType:  core.ValueInt64,
    60  						MetricType: core.MetricGauge,
    61  						IntValue:   40,
    62  					},
    63  					"m2": {
    64  						ValueType:  core.ValueInt64,
    65  						MetricType: core.MetricGauge,
    66  						IntValue:   444,
    67  					},
    68  				},
    69  				LabeledMetrics: []core.LabeledMetric{
    70  					{
    71  						Name:   "somelblmetric",
    72  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
    73  						MetricValue: core.MetricValue{
    74  							ValueType:  core.ValueInt64,
    75  							MetricType: core.MetricGauge,
    76  							IntValue:   8675,
    77  						},
    78  					},
    79  					{
    80  						Name:   "otherlblmetric",
    81  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
    82  						MetricValue: core.MetricValue{
    83  							ValueType:  core.ValueInt64,
    84  							MetricType: core.MetricGauge,
    85  							IntValue:   1234,
    86  						},
    87  					},
    88  				},
    89  			},
    90  		},
    91  	}
    92  
    93  	batch3 := core.DataBatch{
    94  		Timestamp: now.Add(-20 * time.Second),
    95  		MetricSets: map[string]*core.MetricSet{
    96  			key: {
    97  				Labels: map[string]string{
    98  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    99  				},
   100  				MetricValues: map[string]core.MetricValue{
   101  					"m1": {
   102  						ValueType:  core.ValueInt64,
   103  						MetricType: core.MetricGauge,
   104  						IntValue:   20,
   105  					},
   106  					"m2": {
   107  						ValueType:  core.ValueInt64,
   108  						MetricType: core.MetricGauge,
   109  						IntValue:   222,
   110  					},
   111  				},
   112  				LabeledMetrics: []core.LabeledMetric{
   113  					{
   114  						Name:   "somelblmetric",
   115  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
   116  						MetricValue: core.MetricValue{
   117  							ValueType:  core.ValueInt64,
   118  							MetricType: core.MetricGauge,
   119  							IntValue:   309,
   120  						},
   121  					},
   122  					{
   123  						Name:   "somelblmetric",
   124  						Labels: map[string]string{"lbl1": "val1.2", "lbl2": "val2.1"},
   125  						MetricValue: core.MetricValue{
   126  							ValueType:  core.ValueInt64,
   127  							MetricType: core.MetricGauge,
   128  							IntValue:   5678,
   129  						},
   130  					},
   131  				},
   132  			},
   133  			otherKey: {
   134  				Labels: map[string]string{
   135  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   136  				},
   137  				MetricValues: map[string]core.MetricValue{
   138  					"m1": {
   139  						ValueType:  core.ValueInt64,
   140  						MetricType: core.MetricGauge,
   141  						IntValue:   123,
   142  					},
   143  				},
   144  			},
   145  		},
   146  	}
   147  
   148  	return batch1, batch2, batch3
   149  }
   150  
   151  func TestGetMetrics(t *testing.T) {
   152  	now := time.Now()
   153  	key := core.PodKey("ns1", "pod1")
   154  	otherKey := core.PodKey("ns1", "other")
   155  
   156  	batch1, batch2, batch3 := makeBatches(now, key, otherKey)
   157  
   158  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   159  	metrics.ExportData(&batch1)
   160  	metrics.ExportData(&batch2)
   161  	metrics.ExportData(&batch3)
   162  
   163  	//batch1 is discarded by long store
   164  	result1 := metrics.GetMetric("m1", []string{key}, now.Add(-120*time.Second), now)
   165  	assert.Equal(t, 2, len(result1[key]))
   166  	assert.Equal(t, int64(40), result1[key][0].MetricValue.IntValue)
   167  	assert.Equal(t, int64(20), result1[key][1].MetricValue.IntValue)
   168  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{otherKey}, now.Add(-120*time.Second), now)[otherKey]))
   169  
   170  	//batch1 is discarded by long store and batch2 doesn't belong to time window
   171  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{key}, now.Add(-30*time.Second), now)[key]))
   172  
   173  	//batch1 and batch1 are discarded by short store
   174  	assert.Equal(t, 1, len(metrics.GetMetric("m2", []string{key}, now.Add(-120*time.Second), now)[key]))
   175  
   176  	//nothing is in time window
   177  	assert.Equal(t, 0, len(metrics.GetMetric("m2", []string{key}, now.Add(-10*time.Second), now)[key]))
   178  
   179  	metricNames := metrics.GetMetricNames(key)
   180  	assert.Equal(t, 2, len(metricNames))
   181  	assert.Contains(t, metricNames, "m1")
   182  	assert.Contains(t, metricNames, "m2")
   183  }
   184  
   185  func TestGetLabeledMetrics(t *testing.T) {
   186  	now := time.Now().UTC()
   187  	key := core.PodKey("ns1", "pod1")
   188  	otherKey := core.PodKey("ns1", "other")
   189  
   190  	batch1, batch2, batch3 := makeBatches(now, key, otherKey)
   191  
   192  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   193  	metrics.ExportData(&batch1)
   194  	metrics.ExportData(&batch2)
   195  	metrics.ExportData(&batch3)
   196  
   197  	result := metrics.GetLabeledMetric("somelblmetric", map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"}, []string{key}, now.Add(-120*time.Second), now)
   198  
   199  	assert.Equal(t, []core.TimestampedMetricValue{
   200  		{
   201  			Timestamp: now.Add(-20 * time.Second),
   202  			MetricValue: core.MetricValue{
   203  				ValueType:  core.ValueInt64,
   204  				MetricType: core.MetricGauge,
   205  				IntValue:   309,
   206  			},
   207  		},
   208  	}, result[key])
   209  }
   210  
   211  func TestGetNames(t *testing.T) {
   212  	now := time.Now()
   213  	key := core.PodKey("ns1", "pod1")
   214  	otherKey := core.PodKey("ns1", "other")
   215  
   216  	batch := core.DataBatch{
   217  		Timestamp: now.Add(-20 * time.Second),
   218  		MetricSets: map[string]*core.MetricSet{
   219  			key: {
   220  				Labels: map[string]string{
   221  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   222  					core.LabelNamespaceName.Key: "ns1",
   223  					core.LabelPodName.Key:       "pod1",
   224  				},
   225  				MetricValues: map[string]core.MetricValue{
   226  					"m1": {
   227  						ValueType:  core.ValueInt64,
   228  						MetricType: core.MetricGauge,
   229  						IntValue:   20,
   230  					},
   231  					"m2": {
   232  						ValueType:  core.ValueInt64,
   233  						MetricType: core.MetricGauge,
   234  						IntValue:   222,
   235  					},
   236  				},
   237  			},
   238  			otherKey: {
   239  				Labels: map[string]string{
   240  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   241  					core.LabelNamespaceName.Key: "ns2",
   242  					core.LabelPodName.Key:       "pod2",
   243  				},
   244  				MetricValues: map[string]core.MetricValue{
   245  					"m1": {
   246  						ValueType:  core.ValueInt64,
   247  						MetricType: core.MetricGauge,
   248  						IntValue:   123,
   249  					},
   250  				},
   251  			},
   252  		},
   253  	}
   254  
   255  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   256  	metrics.ExportData(&batch)
   257  
   258  	assert.Contains(t, metrics.GetPods(), "ns1/pod1")
   259  	assert.Contains(t, metrics.GetPods(), "ns2/pod2")
   260  	assert.Contains(t, metrics.GetPodsFromNamespace("ns1"), "pod1")
   261  	assert.NotContains(t, metrics.GetPodsFromNamespace("ns1"), "pod2")
   262  	assert.Contains(t, metrics.GetMetricSetKeys(), key)
   263  	assert.Contains(t, metrics.GetMetricSetKeys(), otherKey)
   264  }