cumulative counters implemented.

This commit is contained in:
Erik Brakkee 2024-08-07 22:22:54 +02:00
parent 5aba014deb
commit 7af8c2a09b
2 changed files with 34 additions and 0 deletions

View File

@ -12,6 +12,22 @@ import (
const NAMESPACE = "converge" const NAMESPACE = "converge"
var ( var (
// remember previous values of agent guids and clients so that we can increment
// the cumulative counters.
lastAgentGuids map[string]bool = make(map[string]bool)
lastClientGuids map[string]bool = make(map[string]bool)
cumulativeAgentCount = promauto.NewCounter(prometheus.CounterOpts{
Namespace: NAMESPACE,
Name: "agent_count_total",
Help: "Total number of agents connected over time",
})
cumulativeClientCount = promauto.NewCounter(prometheus.CounterOpts{
Namespace: NAMESPACE,
Name: "client_count_total",
Help: "Total number of clients connected over time",
})
agentCount = promauto.NewGauge(prometheus.GaugeOpts{ agentCount = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: NAMESPACE, Namespace: NAMESPACE,
Name: "agent_count", Name: "agent_count",
@ -88,14 +104,29 @@ func updateMetrics(state *models.State) {
// become 0. // become 0.
log.Printf("Got notification %v", *state) log.Printf("Got notification %v", *state)
agentGuids := make(map[string]bool)
clientGuids := make(map[string]bool)
agentCount.Set(float64(len(state.Agents))) agentCount.Set(float64(len(state.Agents)))
agentInfo.Reset() agentInfo.Reset()
for _, agent := range state.Agents { for _, agent := range state.Agents {
if !lastAgentGuids[agent.Guid] {
cumulativeAgentCount.Inc()
}
agentGuids[agent.Guid] = true
agentActive(agent) agentActive(agent)
} }
lastAgentGuids = agentGuids
clientCount.Set(float64(len(state.Clients))) clientCount.Set(float64(len(state.Clients)))
clientInfo.Reset() clientInfo.Reset()
for _, client := range state.Clients { for _, client := range state.Clients {
if !lastClientGuids[client.Guid] {
cumulativeClientCount.Inc()
}
clientGuids[client.Guid] = true
clientActive(client) clientActive(client)
} }
lastClientGuids = clientGuids
} }

View File

@ -1,6 +1,9 @@
package models package models
type State struct { type State struct {
CumulativeAgentCount int
CumulativeClientCount int
Agents []Agent Agents []Agent
Clients []Client Clients []Client
Ascii string Ascii string