github.com/kubewharf/katalyst-core@v0.5.3/pkg/custom-metric/collector/prometheus/scrape_test.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 prometheus 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 29 "github.com/kubewharf/katalyst-core/pkg/custom-metric/store/data" 30 "github.com/kubewharf/katalyst-core/pkg/metrics" 31 ) 32 33 func Test_scrape(t *testing.T) { 34 t.Parallel() 35 36 ctx := context.Background() 37 38 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 39 _, _ = w.Write([]byte(` # HELP none_namespace_metric 40 # TYPE none_namespace_metric gauge 41 none_namespace_metric{test="none_namespace_metric_l"} 0 3 42 # HELP none_object_metric 43 # TYPE none_object_metric gauge 44 none_object_metric{label_1="none_object_metric",namespace="n1"} 16 4 45 # HELP full_metric 46 # TYPE full_metric gauge 47 full_metric{label_test="full",namespace="n1",object="pod",object_name="pod_1",timestamp="12"} 176 55 48 # HELP with_labeled_timestamp 49 # TYPE with_labeled_timestamp gauge 50 with_labeled_timestamp{label_test="labeled",namespace="n1",object="pod",object_name="pod_2",timestamp="123"} 179 51 # HELP without_timestamp 52 # TYPE without_timestamp gauge 53 without_timestamp{label_test="without_timestamp",namespace="n1",object="pod",object_name="pod_3"} 173 54 `)) 55 })) 56 defer server.Close() 57 58 client, _ := newPrometheusClient() 59 s, _ := NewScrapeManager(ctx, time.Hour, client, "fake-node", server.URL, metrics.DummyMetrics{}, "", "") 60 // to make sure the metric will only be collected once 61 s.scrape() 62 time.Sleep(time.Millisecond * 300) 63 64 handler := func(d []*data.MetricSeries, tags ...metrics.MetricTag) error { 65 assert.NotNil(t, d) 66 return fmt.Errorf("test error") 67 } 68 s.HandleMetric(handler) 69 70 var dataList []*data.MetricSeries 71 handler = func(d []*data.MetricSeries, tags ...metrics.MetricTag) error { 72 assert.NotNil(t, d) 73 dataList = append(dataList, d...) 74 return nil 75 } 76 s.HandleMetric(handler) 77 78 assert.Equal(t, 4, len(dataList)) 79 assert.ElementsMatch(t, []*data.MetricSeries{ 80 { 81 Name: "none_namespace_metric", 82 Labels: map[string]string{ 83 "test": "none_namespace_metric_l", 84 }, 85 Series: []*data.MetricData{ 86 { 87 Data: 0, 88 Timestamp: 3, 89 }, 90 }, 91 }, 92 { 93 Name: "none_object_metric", 94 Labels: map[string]string{ 95 "label_1": "none_object_metric", 96 "namespace": "n1", 97 }, 98 Series: []*data.MetricData{ 99 { 100 Data: 16, 101 Timestamp: 4, 102 }, 103 }, 104 }, 105 { 106 Name: "full_metric", 107 Labels: map[string]string{ 108 "label_test": "full", 109 "namespace": "n1", 110 "object": "pod", 111 "object_name": "pod_1", 112 }, 113 Series: []*data.MetricData{ 114 { 115 Data: 176, 116 Timestamp: 55, 117 }, 118 }, 119 }, 120 { 121 Name: "with_labeled_timestamp", 122 Labels: map[string]string{ 123 "label_test": "labeled", 124 "namespace": "n1", 125 "object": "pod", 126 "object_name": "pod_2", 127 }, 128 Series: []*data.MetricData{ 129 { 130 Data: 179, 131 Timestamp: 123, 132 }, 133 }, 134 }, 135 }, dataList) 136 assert.Equal(t, 0, len(s.storedSeriesMap)) 137 138 s.Stop() 139 }