github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/mometric/metric_test.go (about) 1 // Copyright 2022 Matrix Origin 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 package mometric 15 16 import ( 17 "context" 18 "io" 19 "net/http" 20 "strings" 21 "testing" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/common/runtime" 25 "github.com/matrixorigin/matrixone/pkg/logutil" 26 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 27 "github.com/matrixorigin/matrixone/pkg/util/metric" 28 29 "github.com/matrixorigin/matrixone/pkg/config" 30 prom "github.com/prometheus/client_golang/prometheus" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestMetric(t *testing.T) { 36 sqlch := make(chan string, 100) 37 factory := newExecutorFactory(sqlch) 38 39 runtime.SetupProcessLevelRuntime(runtime.NewRuntime(metadata.ServiceType_CN, "test", logutil.GetGlobalLogger())) 40 41 withModifiedConfig(func() { 42 SV := config.NewObservabilityParameters() 43 SV.SetDefaultValues("test") 44 SV.Host = "0.0.0.0" 45 SV.StatusPort = 7001 46 SV.EnableMetricToProm = true 47 SV.MetricExportInterval = 1 48 defer metric.SetGatherInterval(metric.SetGatherInterval(30 * time.Millisecond)) 49 defer metric.SetRawHistBufLimit(metric.SetRawHistBufLimit(5)) 50 InitMetric(context.TODO(), factory, SV, "node_uuid", "test", WithInitAction(true)) 51 defer StopMetricSync() 52 53 const ( 54 none = "--None" 55 createDB = "create database" 56 createTbl = "CREATE TABLE" 57 createView = "CREATE VIEW" 58 insertRow = "insert into" 59 ) 60 prevSqlKind := none 61 for sql := range sqlch { 62 t.Logf("sql: %s", sql) 63 if strings.HasPrefix(sql, prevSqlKind) { 64 continue 65 } 66 switch prevSqlKind { 67 case none: 68 require.True(t, strings.HasPrefix(sql, createDB), "income sql: %s", sql) 69 prevSqlKind = createDB 70 case createDB: 71 require.True(t, strings.HasPrefix(sql, createTbl), "income sql: %s", sql) 72 prevSqlKind = createTbl 73 case createTbl: 74 require.True(t, strings.HasPrefix(sql, createView), "income sql: %s", sql) 75 prevSqlKind = createView 76 case createView: 77 require.True(t, strings.HasPrefix(sql, insertRow), "income sql: %s", sql) 78 goto GOON 79 default: 80 require.True(t, false, "unknow sql kind %s", sql) 81 } 82 } 83 GOON: 84 client := http.Client{ 85 Timeout: 120 * time.Second, 86 } 87 r, err := client.Get("http://127.0.0.1:7001/metrics") 88 require.Nil(t, err) 89 require.Equal(t, r.StatusCode, 200) 90 91 content, _ := io.ReadAll(r.Body) 92 require.Contains(t, string(content), "# HELP") // check we have metrics output 93 }) 94 } 95 96 func TestMetricNoProm(t *testing.T) { 97 sqlch := make(chan string, 100) 98 factory := newExecutorFactory(sqlch) 99 100 withModifiedConfig(func() { 101 SV := config.NewObservabilityParameters() 102 SV.SetDefaultValues("test") 103 SV.Host = "0.0.0.0" 104 SV.StatusPort = 7001 105 SV.EnableMetricToProm = false 106 107 defer metric.SetGatherInterval(metric.SetGatherInterval(30 * time.Millisecond)) 108 defer metric.SetRawHistBufLimit(metric.SetRawHistBufLimit(5)) 109 InitMetric(context.TODO(), factory, SV, "node_uuid", "test", WithInitAction(true)) 110 defer StopMetricSync() 111 112 client := http.Client{ 113 Timeout: 120 * time.Second, 114 } 115 _, err := client.Get("http://127.0.0.1:7001/metrics") 116 require.NotNil(t, err) 117 require.Contains(t, err.Error(), "connection refused") 118 119 // make static-check(errcheck) happay 120 SV.EnableMetricToProm = true 121 }) 122 } 123 124 func TestDescExtra(t *testing.T) { 125 desc := prom.NewDesc("sys_xxx_yyy_FEFA", "help info", []string{"is_internal", "xy"}, map[string]string{"node": "1"}) 126 extra := newDescExtra(desc) 127 assert.Equal(t, extra.fqName, "sys_xxx_yyy_FEFA") 128 assert.Equal(t, extra.labels[0].GetName(), "is_internal") 129 assert.Equal(t, extra.labels[1].GetName(), "node") 130 assert.Equal(t, extra.labels[2].GetName(), "xy") 131 } 132 133 func TestGetSchemaForAccount(t *testing.T) { 134 type args struct { 135 account string 136 } 137 tests := []struct { 138 name string 139 args args 140 wantPath string 141 wantSche int 142 }{ 143 { 144 name: "test_account_user1", 145 args: args{ 146 account: "user1", 147 }, 148 wantPath: "CREATE TABLE IF NOT EXISTS", 149 wantSche: 7, 150 }, 151 } 152 ctx := context.Background() 153 for _, tt := range tests { 154 t.Run(tt.name, func(t *testing.T) { 155 schemas := GetSchemaForAccount(ctx, tt.args.account) 156 found := false 157 158 if strings.Contains(schemas[0], tt.wantPath) { 159 found = true 160 } 161 require.Equal(t, true, found) 162 }) 163 } 164 }