github.com/GuanceCloud/cliutils@v1.1.21/point/p8s_point_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package point 7 8 import ( 9 "path/filepath" 10 T "testing" 11 12 "github.com/prometheus/client_golang/prometheus" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestMergePoint(t *T.T) { 18 t.Run(`basic`, func(t *T.T) { 19 pts := []*Point{ 20 NewPointV2("m1", 21 append(NewTags(map[string]string{"t1": "v1", "t2": "v2"}), 22 NewKVs(map[string]any{"f1": 123})...), 23 DefaultLoggingOptions()..., 24 ), 25 26 NewPointV2("m1", 27 append(NewTags(map[string]string{"t1": "v1", "t2": "v2"}), 28 NewKVs(map[string]any{"f2": "hello"})...), 29 DefaultLoggingOptions()..., 30 ), 31 } 32 33 mPts := mergePts(pts) 34 assert.Len(t, mPts, 1) 35 mfs := mPts[0].Fields() 36 assert.Equal(t, `hello`, mfs.Get(`f2`).GetS()) 37 38 t.Logf("point: %s", mPts[0].LineProto()) 39 }) 40 41 t.Run(`merge-multiple-time-series`, func(t *T.T) { 42 pts := []*Point{ 43 NewPointV2("m1", 44 append(NewTags(map[string]string{"t1": "v1", "t2": "v2"}), 45 NewKVs(map[string]any{"f1": 123})...), 46 DefaultLoggingOptions()..., 47 ), 48 49 NewPointV2("m1", 50 append(NewTags(map[string]string{"t1": "v1", "t2": "v2"}), 51 NewKVs(map[string]any{"f2": "hello"})...), 52 DefaultLoggingOptions()..., 53 ), 54 55 NewPointV2("m1", 56 append(NewTags(map[string]string{"tag1": "v1", "tag2": "v2"}), 57 NewKVs(map[string]any{"f1": 123})...), 58 DefaultLoggingOptions()..., 59 ), 60 61 NewPointV2("m1", 62 append(NewTags(map[string]string{"tag1": "v1", "tag2": "v2"}), 63 NewKVs(map[string]any{"f2": "hello"})...), 64 DefaultLoggingOptions()..., 65 ), 66 } 67 68 mPts := mergePts(pts) 69 require.Len(t, mPts, 2) 70 for _, pt := range mPts { 71 t.Logf("point: %s", pt.LineProto()) 72 } 73 74 mfs := mPts[0].Fields() 75 assert.Equal(t, `hello`, mfs.Get(`f2`).GetS()) 76 77 mfs = mPts[1].Fields() 78 assert.Equal(t, `hello`, mfs.Get(`f2`).GetS()) 79 }) 80 } 81 82 func TestGatherPoint(t *T.T) { 83 ns := "testing" 84 85 t.Run(`basic`, func(t *T.T) { 86 cnt := prometheus.NewCounterVec( 87 prometheus.CounterOpts{ 88 Namespace: ns, 89 Name: filepath.Base(t.Name()), 90 }, []string{"tag1", "tag2"}, 91 ) 92 reg := prometheus.NewRegistry() 93 reg.MustRegister(cnt) 94 95 cnt.WithLabelValues("v1", "v2").Add(1.0) 96 cnt.WithLabelValues("v1", "v2").Add(1.0) 97 cnt.WithLabelValues("v1", "v2").Add(1.0) 98 99 cnt.WithLabelValues("v3", "v4").Add(1.0) 100 cnt.WithLabelValues("v3", "v4").Add(1.0) 101 cnt.WithLabelValues("v3", "v4").Add(1.0) 102 103 pts, err := doGatherPoints(reg) 104 assert.NoError(t, err) 105 106 require.Len(t, pts, 2) 107 108 tags := pts[0].Tags() 109 fs := pts[0].Fields() 110 assert.Equal(t, ns, pts[0].Name()) 111 assert.Equal(t, "v1", tags.Get("tag1").GetS()) 112 assert.Equal(t, "v2", tags.Get("tag2").GetS()) 113 assert.Equal(t, 3.0, fs.Get(filepath.Base(t.Name())).GetF()) 114 115 tags = pts[1].Tags() 116 fs = pts[1].Fields() 117 assert.Equal(t, ns, pts[0].Name()) 118 assert.Equal(t, "v3", tags.Get("tag1").GetS()) 119 assert.Equal(t, "v4", tags.Get("tag2").GetS()) 120 assert.Equal(t, 3.0, fs.Get(filepath.Base(t.Name())).GetF()) 121 122 for _, pt := range pts { 123 t.Logf("point: %s", pt.LineProto()) 124 } 125 }) 126 }