github.com/matrixorigin/matrixone@v0.7.0/pkg/util/metric/config.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 15 package metric 16 17 import ( 18 "os" 19 "strconv" 20 "strings" 21 "sync/atomic" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/config" 25 pb "github.com/matrixorigin/matrixone/pkg/pb/metric" 26 ) 27 28 var ( 29 // full buffer approximately cost (56[Sample struct] + 8[pointer]) x 4096 = 256K 30 configRawHistBufLimit int32 = envOrDefaultInt[int32]("MO_METRIC_RAWHIST_BUF_LIMIT", 4096) 31 configGatherInterval int64 = envOrDefaultInt[int64]("MO_METRIC_GATHER_INTERVAL", 15000) // 15s 32 configExportToProm int32 = envOrDefaultBool("MO_METRIC_EXPORT_TO_PROM", 1) 33 configForceReinit int32 = envOrDefaultBool("MO_METRIC_DROP_AND_INIT", 0) // TODO: find a better way to init metrics and remove this one 34 ) 35 36 func initConfigByParamaterUnit(SV *config.ObservabilityParameters) { 37 setExportToProm(SV.EnableMetricToProm) 38 setGatherInterval(time.Second * time.Duration(SV.MetricGatherInterval)) 39 } 40 41 func envOrDefaultBool(key string, defaultValue int32) int32 { 42 val, ok := os.LookupEnv(key) 43 if !ok { 44 return defaultValue 45 } 46 switch strings.ToLower(val) { 47 case "0", "false", "f": 48 return 0 49 case "1", "true", "t": 50 return 1 51 default: 52 return defaultValue 53 } 54 } 55 56 func envOrDefaultInt[T int32 | int64](key string, defaultValue T) T { 57 val, ok := os.LookupEnv(key) 58 if !ok { 59 return defaultValue 60 } 61 var size int 62 switch any(&defaultValue).(type) { 63 case int32: 64 size = 32 65 case int64: 66 size = 64 67 } 68 i, err := strconv.ParseInt(val, 10, size) 69 if err != nil { 70 return defaultValue 71 } 72 return T(i) 73 } 74 75 func getRawHistBufLimit() int32 { return atomic.LoadInt32(&configRawHistBufLimit) } 76 77 func getExportToProm() bool { return atomic.LoadInt32(&configExportToProm) != 0 } 78 79 func getForceInit() bool { return atomic.LoadInt32(&configForceReinit) != 0 } 80 81 func getGatherInterval() time.Duration { 82 return time.Duration(atomic.LoadInt64(&configGatherInterval)) * time.Millisecond 83 } 84 85 // for tests 86 87 func setRawHistBufLimit(new int32) int32 { 88 return atomic.SwapInt32(&configRawHistBufLimit, new) 89 } 90 91 func setExportToProm(new bool) bool { 92 var val int32 = 0 93 if new { 94 val = 1 95 } 96 return atomic.SwapInt32(&configExportToProm, val) != 0 97 } 98 99 func setGatherInterval(new time.Duration) time.Duration { 100 return time.Duration(atomic.SwapInt64(&configGatherInterval, int64(new/time.Millisecond))) * time.Millisecond 101 } 102 103 func isFullBatchRawHist(mf *pb.MetricFamily) bool { 104 return mf.GetType() == pb.MetricType_RAWHIST && len(mf.Metric[0].RawHist.Samples) >= int(getRawHistBufLimit()) 105 }