eliminated unused fields in State
Now preserving the lastState in prometheus.go in a State object instead of in multiple values.
This commit is contained in:
parent
6d48146e6b
commit
6bb75728e7
@ -8,13 +8,13 @@ At least go version 1.21 so that it automatically downloads the correct version
|
|||||||
```
|
```
|
||||||
go install github.com/a-h/templ/cmd/templ@latest
|
go install github.com/a-h/templ/cmd/templ@latest
|
||||||
go install golang.org/x/pkgsite/cmd/pkgsite@latest
|
go install golang.org/x/pkgsite/cmd/pkgsite@latest
|
||||||
|
go install honnef.co/go/tools/cmd/staticcheck@v0.5.0
|
||||||
```
|
```
|
||||||
|
|
||||||
# Profiling
|
# Profiling
|
||||||
|
|
||||||
```
|
```
|
||||||
go tool pprof -http:8081 http://localhost:8000/debug/pprof/profile?seconds=30
|
go tool pprof -http 8081 http://localhost:8000/debug/pprof/profile?seconds=30
|
||||||
go tool pprof -http:8081 http://localhost:8000/debug/pprof/heap
|
go tool pprof -http 8081 http://localhost:8000/debug/pprof/heap
|
||||||
go tool pprof -http:8081 http://localhost:8000/debug/pprof/goroutine
|
go tool pprof -http 8081 http://localhost:8000/debug/pprof/goroutine
|
||||||
```
|
```
|
||||||
|
@ -15,8 +15,7 @@ const NAMESPACE = "converge"
|
|||||||
var (
|
var (
|
||||||
// remember previous values of agent guids and clients so that we can increment
|
// remember previous values of agent guids and clients so that we can increment
|
||||||
// the cumulative counters.
|
// the cumulative counters.
|
||||||
lastAgents map[string]models.Agent = make(map[string]models.Agent)
|
lastState *models.State = &models.State{}
|
||||||
lastClients map[string]models.Client = make(map[string]models.Client)
|
|
||||||
|
|
||||||
cumulativeAgentCount = promauto.NewCounter(prometheus.CounterOpts{
|
cumulativeAgentCount = promauto.NewCounter(prometheus.CounterOpts{
|
||||||
Namespace: NAMESPACE,
|
Namespace: NAMESPACE,
|
||||||
@ -100,7 +99,7 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func agentLabels(agent models.Agent) prometheus.Labels {
|
func agentLabels(agent *models.Agent) prometheus.Labels {
|
||||||
return prometheus.Labels{
|
return prometheus.Labels{
|
||||||
"agent_guid": agent.Guid,
|
"agent_guid": agent.Guid,
|
||||||
"agent_address": agent.RemoteAddr,
|
"agent_address": agent.RemoteAddr,
|
||||||
@ -113,7 +112,7 @@ func agentLabels(agent models.Agent) prometheus.Labels {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientLabels(client models.Client) prometheus.Labels {
|
func clientLabels(client *models.Client) prometheus.Labels {
|
||||||
return prometheus.Labels{
|
return prometheus.Labels{
|
||||||
"client_guid": client.Guid,
|
"client_guid": client.Guid,
|
||||||
"client_address": client.RemoteAddr,
|
"client_address": client.RemoteAddr,
|
||||||
@ -129,9 +128,9 @@ func clientLabels(client models.Client) prometheus.Labels {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func agentActive(agent models.Agent) {
|
func agentActive(agent *models.Agent) {
|
||||||
prevAgent, ok := lastAgents[agent.Guid]
|
prevAgent, ok := findAgent(lastState, agent.Guid)
|
||||||
if ok && prevAgent != agent {
|
if ok && *prevAgent != *agent {
|
||||||
removeAgentInfoMetrics(prevAgent)
|
removeAgentInfoMetrics(prevAgent)
|
||||||
}
|
}
|
||||||
agentInfo.With(agentLabels(agent)).Set(1)
|
agentInfo.With(agentLabels(agent)).Set(1)
|
||||||
@ -143,9 +142,27 @@ func agentActive(agent models.Agent) {
|
|||||||
Set(float64(time.Now().Sub(agent.StartTime).Seconds()))
|
Set(float64(time.Now().Sub(agent.StartTime).Seconds()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientActive(client models.Client) {
|
func findAgent(state *models.State, guid string) (*models.Agent, bool) {
|
||||||
prevClient, ok := lastClients[client.Guid]
|
for _, agent := range state.Agents {
|
||||||
if ok && prevClient != client {
|
if agent.Guid == guid {
|
||||||
|
return &agent, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func findClient(state *models.State, guid string) (*models.Client, bool) {
|
||||||
|
for _, client := range state.Clients {
|
||||||
|
if client.Guid == guid {
|
||||||
|
return &client, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientActive(client *models.Client) {
|
||||||
|
prevClient, ok := findClient(lastState, client.Guid)
|
||||||
|
if ok && *prevClient != *client {
|
||||||
removeClientInfoMetrics(prevClient)
|
removeClientInfoMetrics(prevClient)
|
||||||
}
|
}
|
||||||
clientInfo.With(clientLabels(client)).Set(1)
|
clientInfo.With(clientLabels(client)).Set(1)
|
||||||
@ -202,12 +219,12 @@ func updateMetrics(state *models.State) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateDurations() {
|
func updateDurations() {
|
||||||
for _, agent := range lastAgents {
|
for _, agent := range lastState.Agents {
|
||||||
agentDuration.
|
agentDuration.
|
||||||
With(prometheus.Labels{"agent_guid": agent.Guid}).
|
With(prometheus.Labels{"agent_guid": agent.Guid}).
|
||||||
Set(float64(time.Now().Sub(agent.StartTime).Seconds()))
|
Set(float64(time.Now().Sub(agent.StartTime).Seconds()))
|
||||||
}
|
}
|
||||||
for _, client := range lastClients {
|
for _, client := range lastState.Clients {
|
||||||
clientDuration.
|
clientDuration.
|
||||||
With(prometheus.Labels{"client_guid": client.Guid}).
|
With(prometheus.Labels{"client_guid": client.Guid}).
|
||||||
Set(float64(time.Now().Sub(client.StartTime).Seconds()))
|
Set(float64(time.Now().Sub(client.StartTime).Seconds()))
|
||||||
@ -220,48 +237,48 @@ func updateMetricsImpl(state *models.State) {
|
|||||||
|
|
||||||
agentCount.Set(float64(len(state.Agents)))
|
agentCount.Set(float64(len(state.Agents)))
|
||||||
disconnectedAgents := make(map[string]models.Agent)
|
disconnectedAgents := make(map[string]models.Agent)
|
||||||
for k, v := range lastAgents {
|
for _, agent := range lastState.Agents {
|
||||||
disconnectedAgents[k] = v
|
disconnectedAgents[agent.Guid] = agent
|
||||||
}
|
}
|
||||||
for _, agent := range state.Agents {
|
for _, agent := range state.Agents {
|
||||||
if _, ok := lastAgents[agent.Guid]; !ok {
|
if _, ok := findAgent(lastState, agent.Guid); !ok {
|
||||||
cumulativeAgentCount.Inc()
|
cumulativeAgentCount.Inc()
|
||||||
}
|
}
|
||||||
delete(disconnectedAgents, agent.Guid)
|
delete(disconnectedAgents, agent.Guid)
|
||||||
agentGuids[agent.Guid] = agent
|
agentGuids[agent.Guid] = agent
|
||||||
agentActive(agent)
|
agentActive(&agent)
|
||||||
}
|
}
|
||||||
for _, agent := range disconnectedAgents {
|
for _, agent := range disconnectedAgents {
|
||||||
removeAgentMetrics(agent)
|
removeAgentMetrics(&agent)
|
||||||
}
|
}
|
||||||
lastAgents = agentGuids
|
|
||||||
|
|
||||||
clientCount.Set(float64(len(state.Clients)))
|
clientCount.Set(float64(len(state.Clients)))
|
||||||
|
|
||||||
// with this app
|
// with this app
|
||||||
disconnectedClients := make(map[string]models.Client)
|
disconnectedClients := make(map[string]models.Client)
|
||||||
for k, v := range lastClients {
|
for _, client := range lastState.Clients {
|
||||||
disconnectedClients[k] = v
|
disconnectedClients[client.Guid] = client
|
||||||
}
|
}
|
||||||
for _, client := range state.Clients {
|
for _, client := range state.Clients {
|
||||||
if _, ok := lastClients[client.Guid]; !ok {
|
if _, ok := findClient(lastState, client.Guid); !ok {
|
||||||
cumulativeClientCount.Inc()
|
cumulativeClientCount.Inc()
|
||||||
}
|
}
|
||||||
delete(disconnectedClients, client.Guid)
|
delete(disconnectedClients, client.Guid)
|
||||||
clientGuids[client.Guid] = client
|
clientGuids[client.Guid] = client
|
||||||
clientActive(client)
|
clientActive(&client)
|
||||||
}
|
}
|
||||||
for _, client := range disconnectedClients {
|
for _, client := range disconnectedClients {
|
||||||
removeClientMetrics(client)
|
removeClientMetrics(&client)
|
||||||
}
|
|
||||||
lastClients = clientGuids
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeAgentInfoMetrics(agent models.Agent) bool {
|
lastState = state
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeAgentInfoMetrics(agent *models.Agent) bool {
|
||||||
return agentInfo.Delete(agentLabels(agent))
|
return agentInfo.Delete(agentLabels(agent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeAgentMetrics(agent models.Agent) {
|
func removeAgentMetrics(agent *models.Agent) {
|
||||||
ok1 := removeAgentInfoMetrics(agent)
|
ok1 := removeAgentInfoMetrics(agent)
|
||||||
guidLabels := prometheus.Labels{"agent_guid": agent.Guid}
|
guidLabels := prometheus.Labels{"agent_guid": agent.Guid}
|
||||||
ok2 := agentStartTime.Delete(guidLabels)
|
ok2 := agentStartTime.Delete(guidLabels)
|
||||||
@ -279,11 +296,11 @@ func removeAgentMetrics(agent models.Agent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeClientInfoMetrics(client models.Client) bool {
|
func removeClientInfoMetrics(client *models.Client) bool {
|
||||||
return clientInfo.Delete(clientLabels(client))
|
return clientInfo.Delete(clientLabels(client))
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeClientMetrics(client models.Client) {
|
func removeClientMetrics(client *models.Client) {
|
||||||
ok1 := removeClientInfoMetrics(client)
|
ok1 := removeClientInfoMetrics(client)
|
||||||
guidLabels := prometheus.Labels{"client_guid": client.Guid}
|
guidLabels := prometheus.Labels{"client_guid": client.Guid}
|
||||||
ok2 := clientStartTime.Delete(guidLabels)
|
ok2 := clientStartTime.Delete(guidLabels)
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
CumulativeAgentCount int
|
|
||||||
CumulativeClientCount int
|
|
||||||
|
|
||||||
Agents []Agent
|
Agents []Agent
|
||||||
Clients []Client
|
Clients []Client
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user