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

@@ -4,69 +4,80 @@ import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"lostak.dev/pve-exporter/proxmox"
)
// PVE subscription state collector.
type PveSubscriptionCollector struct {
apiClient *proxmox.PveApiClient // PVE API client instance.
registry *TTLRegistry // TTL metrics registry.
info *prometheus.GaugeVec // Node subscription info prometheus gauge.
status *prometheus.GaugeVec // Node subscription status prometheus gauge.
nextDueDate *prometheus.GaugeVec // Node subscription next due date prometheus gauge.
regDate *prometheus.GaugeVec // Node subscription registration date prometheus gauge.
sockets *prometheus.GaugeVec // Node subscription sockets count prometheus gauge.
info *TTLGaugeVec // Node subscription info prometheus gauge.
status *TTLGaugeVec // Node subscription status prometheus gauge.
nextDueDate *TTLGaugeVec // Node subscription next due date prometheus gauge.
regDate *TTLGaugeVec // Node subscription registration date prometheus gauge.
sockets *TTLGaugeVec // Node subscription sockets count prometheus gauge.
}
// Create new instance of PVE cluster state collector.
func NewPveSubscriptionCollector(apiClient *proxmox.PveApiClient) *PveSubscriptionCollector {
func NewPveSubscriptionCollector(apiClient *proxmox.PveApiClient, registry *TTLRegistry) *PveSubscriptionCollector {
c := PveSubscriptionCollector{apiClient: apiClient}
c.registry = registry
// Node subscription info.
c.info = promauto.NewGaugeVec(
c.info = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_subscription_info",
Help: "Node subscription info.",
},
[]string{"cluster", "node", "productname", "serverid"},
1*time.Minute,
)
c.registry.Register(c.info)
// Node subscription status.
c.status = promauto.NewGaugeVec(
c.status = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_subscription_status",
Help: "Node subscription status.",
},
[]string{"cluster", "node"},
1*time.Minute,
)
c.registry.Register(c.status)
// Node subscription registration date.
c.regDate = promauto.NewGaugeVec(
c.regDate = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_subscription_regdate",
Help: "Node subscription registration date.",
},
[]string{"cluster", "node"},
1*time.Minute,
)
c.registry.Register(c.regDate)
// Node subscription next due date.
c.nextDueDate = promauto.NewGaugeVec(
c.nextDueDate = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_subscription_nextduedate",
Help: "Node subscription next due date.",
},
[]string{"cluster", "node"},
1*time.Minute,
)
c.registry.Register(c.nextDueDate)
// Node subscription count of sockets.
c.sockets = promauto.NewGaugeVec(
c.sockets = NewTTLGaugeVec(
prometheus.GaugeOpts{
Name: "pve_node_subscription_sockets",
Help: "Node subscription count of sockets.",
},
[]string{"cluster", "node"},
1*time.Minute,
)
c.registry.Register(c.sockets)
return &c
}
@@ -78,12 +89,6 @@ func (c *PveSubscriptionCollector) CollectMetrics() error {
return err
}
c.info.Reset()
c.status.Reset()
c.nextDueDate.Reset()
c.regDate.Reset()
c.sockets.Reset()
for _, node := range cluster.NodeStatuses {
labels := prometheus.Labels{
"cluster": cluster.GetClusterName(),