github.com/matrixorigin/matrixone@v1.2.0/pkg/common/log/filter.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 log 16 17 import ( 18 "sync/atomic" 19 ) 20 21 var ( 22 // all log filters register here. 23 filters = []logFilter{sampleFilter} 24 25 // all SampleType register here. 26 samples = map[SampleType]*sampleValue{ 27 ExampleSample: {frequency: 3}, 28 SystemInitSample: {frequency: 5}, 29 } 30 ) 31 32 func sampleFilter(opts LogOptions) bool { 33 if opts.sampleType == noneSample { 34 return true 35 } 36 if v, ok := samples[opts.sampleType]; ok { 37 return v.allow() 38 } 39 return false 40 } 41 42 type sampleValue struct { 43 v uint64 44 frequency uint64 45 } 46 47 func (s *sampleValue) allow() bool { 48 n := s.incr() 49 return n == 1 || n%s.frequency == 0 50 } 51 52 func (s *sampleValue) incr() uint64 { 53 return atomic.AddUint64(&s.v, 1) 54 }