cubefs/flashnode/metric.go
chihe 07a308a7de fix(flashnode): change the type of metric for ReadBytes and ReadCount
close:#22200531

Signed-off-by: chihe <chihe@oppo.com>
2025-08-07 16:00:26 +08:00

109 lines
3.1 KiB
Go
Executable File

package flashnode
import (
"sync/atomic"
"time"
"github.com/cubefs/cubefs/util/exporter"
"github.com/cubefs/cubefs/util/log"
)
const (
StatPeriod = time.Minute * time.Duration(1)
MetricFlashNodeReadBytes = "flashNodeReadBytes"
MetricFlashNodeReadCount = "flashNodeReadCount"
MetricFlashNodeHitRate = "flashNodeHitRate"
MetricFlashNodeEvictCount = "flashNodeEvictCount"
)
type MetricStat struct {
ReadBytes uint64
ReadCount uint64
}
type FlashNodeMetrics struct {
flashNode *FlashNode
stopC chan struct{}
MetricReadBytes *exporter.Gauge
MetricReadCount *exporter.Gauge
MetricEvictCount *exporter.Gauge
MetricHitRate *exporter.Gauge
Stat MetricStat
}
func (f *FlashNode) registerMetrics() {
f.metrics = &FlashNodeMetrics{
flashNode: f,
stopC: make(chan struct{}),
}
f.metrics.MetricReadBytes = exporter.NewGauge(MetricFlashNodeReadBytes)
f.metrics.MetricReadCount = exporter.NewGauge(MetricFlashNodeReadCount)
f.metrics.MetricEvictCount = exporter.NewGauge(MetricFlashNodeEvictCount)
f.metrics.MetricHitRate = exporter.NewGauge(MetricFlashNodeHitRate)
log.LogInfof("registerMetrics")
}
func (f *FlashNode) startMetrics() {
go f.metrics.statMetrics()
log.LogInfof("startMetrics")
}
func (fm *FlashNodeMetrics) statMetrics() {
ticker := time.NewTicker(StatPeriod)
for {
select {
case <-fm.stopC:
ticker.Stop()
log.LogInfof("stop metrics ticker")
return
case <-ticker.C:
fm.doStat()
}
}
}
func (fm *FlashNodeMetrics) doStat() {
log.LogInfof("FlashNodeMetrics: doStat")
fm.setReadBytesMetric()
fm.setReadCountMetric()
fm.setEvictCountMetric()
fm.setHitRateMetric()
}
func (fm *FlashNodeMetrics) setReadBytesMetric() {
readBytes := atomic.SwapUint64(&fm.Stat.ReadBytes, 0)
fm.MetricReadBytes.SetWithLabels(float64(readBytes), map[string]string{"cluster": fm.flashNode.clusterID, exporter.FlashNode: fm.flashNode.localAddr})
}
func (fm *FlashNodeMetrics) setReadCountMetric() {
readCount := atomic.SwapUint64(&fm.Stat.ReadCount, 0)
fm.MetricReadCount.SetWithLabels(float64(readCount), map[string]string{"cluster": fm.flashNode.clusterID, exporter.FlashNode: fm.flashNode.localAddr})
}
func (fm *FlashNodeMetrics) setEvictCountMetric() {
evictCountMap := fm.flashNode.cacheEngine.GetEvictCount()
for dataPath, evictCount := range evictCountMap {
fm.MetricEvictCount.SetWithLabels(float64(evictCount), map[string]string{"cluster": fm.flashNode.clusterID, exporter.FlashNode: fm.flashNode.localAddr, exporter.Disk: dataPath})
}
}
func (fm *FlashNodeMetrics) setHitRateMetric() {
hitRateMap := fm.flashNode.cacheEngine.GetHitRate()
for dataPath, hitRate := range hitRateMap {
fm.MetricHitRate.SetWithLabels(hitRate, map[string]string{"cluster": fm.flashNode.clusterID, exporter.FlashNode: fm.flashNode.localAddr, exporter.Disk: dataPath})
}
}
func (f *FlashNode) updateReadBytesMetric(size uint64) {
if f.metrics != nil {
atomic.AddUint64(&f.metrics.Stat.ReadBytes, size)
}
}
func (f *FlashNode) updateReadCountMetric() {
if f.metrics != nil {
atomic.AddUint64(&f.metrics.Stat.ReadCount, 1)
}
}