learning_go/rbd_exporter/metrics/metrics.go
2025-12-23 19:20:42 +03:00

79 lines
1.8 KiB
Go

package metrics
import (
"rbd_exporter/rbdfactory"
"rbd_exporter/mytypes"
"rbd_exporter/logger"
"time"
"github.com/prometheus/client_golang/prometheus"
)
func GetMetrics(cephConn mytypes.CephConnector) {
metrics := InitMetrics()
prometheus.MustRegister(
metrics.TotalRbdRequestedSizePerPool,
)
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for range ticker.C {
var result = []mytypes.Pool{}
poolList, err := cephConn.ListPools()
if err != nil {
logger.Logger.Error("Cannot get list of pools")
// do not exit but continue checking
continue
}
for _, v := range poolList {
x, _ := rbdfactory.PoolFactory(cephConn, v)
result = append(result, x)
}
metrics.TotalRbdRequestedSizePerPool.Reset()
for _,v := range result {
if !v.HasRBD {continue}
FillMetrics(v,metrics)
}
logger.Logger.Debug("====================")
// fmt.Println(result)
}
}
func InitMetrics() *mytypes.Metrics {
m := &mytypes.Metrics{
TotalRbdRequestedSizePerPool : prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "total_rbd_requested_size_per_pool",
Help: "total size of all requested RBDs in a specific pool",
},
[]string{"poolname"},
),
// total_rbd_requested_size: prometheus.NewGauge(
// prometheus.GaugeOpts{
// Name: "total_rbd_requested_size",
// Help: "total size of all RBDs in the cluster",
// },
// ),
}
return m
}
func FillMetrics(pool mytypes.Pool,metrics *mytypes.Metrics) {
var totalSizePerPool uint64 = 0
logger.Logger.Debugf("Processing pool %s",pool.Name)
for _,v := range pool.RBDlist {
totalSizePerPool += v.GetSize()
}
logger.Logger.Debugf("Total size of RBDs in pool %s is %d",pool.Name,totalSizePerPool)
metrics.TotalRbdRequestedSizePerPool.WithLabelValues(pool.Name,
).Set(
float64(totalSizePerPool))
}