Metrics now have expiration if not updated

This commit is contained in:
Jan Lošťák
2025-02-22 21:24:03 +01:00
parent 2ed310eef7
commit f78df9d3e3
10 changed files with 455 additions and 232 deletions

View File

@@ -3,51 +3,59 @@ package metrics
import (
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"lostak.dev/pve-exporter/proxmox"
)
// PVE cluster state collector.
type PveNodeDiskCollector struct {
apiClient *proxmox.PveApiClient // PVE API client instance.
registry *TTLRegistry // TTL metrics registry.
healthy *prometheus.GaugeVec // Node disk SMART passed state prometheus gauge.
wearout *prometheus.GaugeVec // Node disk wearout % prometheus gauge.
sizeBytes *prometheus.GaugeVec // Node disk size in bytes prometheus gauge.
healthy *TTLGaugeVec // Node disk SMART passed state prometheus gauge.
wearout *TTLGaugeVec // Node disk wearout % prometheus gauge.
sizeBytes *TTLGaugeVec // Node disk size in bytes prometheus gauge.
}
// Create new instance of PVE cluster state collector.
func NewPveNodeDiskCollector(apiClient *proxmox.PveApiClient) *PveNodeDiskCollector {
func NewPveNodeDiskCollector(apiClient *proxmox.PveApiClient, registry *TTLRegistry) *PveNodeDiskCollector {
c := PveNodeDiskCollector{apiClient: apiClient}
c.registry = registry
// Node disk healthy state.
c.healthy = promauto.NewGaugeVec(
c.healthy = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_disk_healthy",
Help: "Node disk healthy state.",
},
[]string{"cluster", "node", "wwn", "type", "model", "serial", "vendor", "used", "osd_id"},
1*time.Minute,
)
c.registry.Register(c.healthy)
// Node disk wearout.
c.wearout = promauto.NewGaugeVec(
c.wearout = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_disk_wearout",
Help: "Node disk wearout percent.",
},
[]string{"cluster", "node", "wwn", "type", "model", "serial", "vendor", "used", "osd_id"},
1*time.Minute,
)
c.registry.Register(c.healthy)
// Node disk size in bytes.
c.sizeBytes = promauto.NewGaugeVec(
c.sizeBytes = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_disk_size_bytes",
Help: "Node disk size in bytes.",
},
[]string{"cluster", "node", "wwn", "type", "model", "serial", "vendor", "used", "osd_id"},
1*time.Minute,
)
c.registry.Register(c.sizeBytes)
return &c
}
@@ -59,10 +67,6 @@ func (c *PveNodeDiskCollector) CollectMetrics() error {
return err
}
c.healthy.Reset()
c.wearout.Reset()
c.sizeBytes.Reset()
for _, node := range cluster.NodeStatuses {
disks, err := c.apiClient.GetNodeDisksList(node.Name)
if err != nil {