github.com/matrixorigin/matrixone@v1.2.0/pkg/perfcounter/counter_logexporter.go (about) 1 // Copyright 2023 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 perfcounter 16 17 import ( 18 "strings" 19 20 "github.com/matrixorigin/matrixone/pkg/util/metric/stats" 21 "go.uber.org/zap" 22 ) 23 24 type CounterLogExporter struct { 25 counter *CounterSet 26 } 27 28 func NewCounterLogExporter(counter *CounterSet) stats.LogExporter { 29 return &CounterLogExporter{ 30 counter: counter, 31 } 32 } 33 34 // Export returns the fields and its values in loggable format. 35 func (c *CounterLogExporter) Export() []zap.Field { 36 var fields []zap.Field 37 38 cacheHit := c.counter.FileService.Cache.Hit.LoadW() 39 cacheRead := c.counter.FileService.Cache.Read.LoadW() 40 if cacheHit != 0 && cacheRead != 0 { 41 fields = append(fields, zap.Any("FileService Cache Hit Rate", float64(cacheHit)/float64(cacheRead))) 42 } 43 44 cacheMemHit := c.counter.FileService.Cache.Memory.Hit.LoadW() 45 cacheMemRead := c.counter.FileService.Cache.Memory.Read.LoadW() 46 if cacheMemHit != 0 && cacheMemRead != 0 { 47 fields = append(fields, zap.Any("FileService Cache Memory Hit Rate", float64(cacheMemHit)/float64(cacheMemRead))) 48 } 49 50 cacheDiskHit := c.counter.FileService.Cache.Disk.Hit.LoadW() 51 cacheDiskRead := c.counter.FileService.Cache.Disk.Read.LoadW() 52 if cacheDiskHit != 0 && cacheDiskRead != 0 { 53 fields = append(fields, zap.Any("FileService Cache Disk Hit Rate", float64(cacheDiskHit)/float64(cacheDiskRead))) 54 } 55 56 cacheRemoteHit := c.counter.FileService.Cache.Remote.Hit.LoadW() 57 cacheRemoteRead := c.counter.FileService.Cache.Remote.Read.LoadW() 58 if cacheRemoteHit != 0 && cacheRemoteRead != 0 { 59 fields = append(fields, zap.Any("FileService Cache Remote Hit Rate", float64(cacheRemoteHit)/float64(cacheRemoteRead))) 60 } 61 62 // all fields in CounterSet 63 _ = c.counter.IterFields(func(path []string, counter *stats.Counter) error { 64 counterValue := counter.SwapW(0) 65 if counterValue != 0 { 66 fields = append(fields, zap.Any(strings.Join(path, "."), counterValue)) 67 } 68 return nil 69 }) 70 71 return fields 72 }