github.com/matrixorigin/matrixone@v1.2.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 pb "github.com/matrixorigin/matrixone/pkg/pb/metric" 25 ) 26 27 var ( 28 // full buffer approximately cost (56[Sample struct] + 8[pointer]) x 4096 = 256K 29 configRawHistBufLimit int32 = EnvOrDefaultInt[int32]("MO_METRIC_RAWHIST_BUF_LIMIT", 4096) 30 configGatherInterval int64 = EnvOrDefaultInt[int64]("MO_METRIC_GATHER_INTERVAL", 15000) // 15s 31 configStatsGatherInterval int64 = EnvOrDefaultInt[int64]("MO_STATS_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 EnvOrDefaultBool(key string, defaultValue int32) int32 { 37 val, ok := os.LookupEnv(key) 38 if !ok { 39 return defaultValue 40 } 41 switch strings.ToLower(val) { 42 case "0", "false", "f": 43 return 0 44 case "1", "true", "t": 45 return 1 46 default: 47 return defaultValue 48 } 49 } 50 51 func EnvOrDefaultInt[T int32 | int64](key string, defaultValue T) T { 52 val, ok := os.LookupEnv(key) 53 if !ok { 54 return defaultValue 55 } 56 var size int 57 switch any(&defaultValue).(type) { 58 case int32: 59 size = 32 60 case int64: 61 size = 64 62 } 63 i, err := strconv.ParseInt(val, 10, size) 64 if err != nil { 65 return defaultValue 66 } 67 return T(i) 68 } 69 func GetRawHistBufLimit() int32 { return atomic.LoadInt32(&configRawHistBufLimit) } 70 71 func EnableExportToProm() bool { return atomic.LoadInt32(&configExportToProm) != 0 } 72 73 func GetForceInit() bool { return atomic.LoadInt32(&configForceReinit) != 0 } 74 75 func GetGatherInterval() time.Duration { 76 return time.Duration(atomic.LoadInt64(&configGatherInterval)) * time.Millisecond 77 } 78 79 func GetStatsGatherInterval() time.Duration { 80 return time.Duration(atomic.LoadInt64(&configStatsGatherInterval)) * time.Millisecond 81 } 82 83 // for tests 84 85 func SetRawHistBufLimit(new int32) int32 { 86 return atomic.SwapInt32(&configRawHistBufLimit, new) 87 } 88 89 func SetExportToProm(new bool) bool { 90 var val int32 = 0 91 if new { 92 val = 1 93 } 94 return atomic.SwapInt32(&configExportToProm, val) != 0 95 } 96 97 func SetGatherInterval(new time.Duration) time.Duration { 98 return time.Duration(atomic.SwapInt64(&configGatherInterval, int64(new/time.Millisecond))) * time.Millisecond 99 } 100 101 func IsFullBatchRawHist(mf *pb.MetricFamily) bool { 102 return mf.GetType() == pb.MetricType_RAWHIST && len(mf.Metric[0].RawHist.Samples) >= int(GetRawHistBufLimit()) 103 }