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

@@ -1,40 +1,48 @@
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"lostak.dev/pve-exporter/proxmox"
)
// PVE cluster state collector.
type PveClusterStateCollector struct {
apiClient *proxmox.PveApiClient // PVE API client instance.
registry *TTLRegistry // TTL metrics registry.
nodes *prometheus.GaugeVec // Count of nodes prometheus gauge.
quorate *prometheus.GaugeVec // Cluster quorum state prometheus gauge.
nodes *TTLGaugeVec // Count of nodes prometheus gauge.
quorate *TTLGaugeVec // Cluster quorum state prometheus gauge.
}
// Create new instance of PVE cluster state collector.
func NewPveClusterStateCollector(apiClient *proxmox.PveApiClient) *PveClusterStateCollector {
func NewPveClusterStateCollector(apiClient *proxmox.PveApiClient, registry *TTLRegistry) *PveClusterStateCollector {
c := PveClusterStateCollector{apiClient: apiClient}
c.registry = registry
// Cluster meta gauge.
c.nodes = promauto.NewGaugeVec(
c.nodes = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_cluster_nodes",
Help: "Cluster nodes count.",
},
[]string{"cluster"},
1*time.Minute,
)
c.registry.Register(c.nodes)
// Cluster quorate gauge.
c.quorate = promauto.NewGaugeVec(
c.quorate = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_cluster_quorate",
Help: "Cluster quorum state.",
},
[]string{"cluster"},
1*time.Minute,
)
c.registry.Register(c.quorate)
c.registry.StartCleanupLoop(5 * time.Second)
return &c
}
@@ -46,9 +54,6 @@ func (c *PveClusterStateCollector) CollectMetrics() error {
return err
}
c.nodes.Reset()
c.quorate.Reset()
l := prometheus.Labels{"cluster": cluster.Name}
c.nodes.With(l).Set(float64(cluster.Nodes))
c.quorate.With(l).Set(float64(cluster.Quorate))