Eliminating copying of objects in the prometheus integration.
This commit is contained in:
		
							parent
							
								
									bf6667aa81
								
							
						
					
					
						commit
						9a1b78ee63
					
				| @ -143,18 +143,18 @@ func agentActive(agent *models.Agent) { | ||||
| } | ||||
| 
 | ||||
| func findAgent(state *models.State, guid string) (*models.Agent, bool) { | ||||
| 	for _, agent := range state.Agents { | ||||
| 		if agent.Guid == guid { | ||||
| 			return &agent, true | ||||
| 	for i := range state.Agents { | ||||
| 		if state.Agents[i].Guid == guid { | ||||
| 			return &state.Agents[i], 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 | ||||
| 	for i := range state.Clients { | ||||
| 		if state.Clients[i].Guid == guid { | ||||
| 			return &state.Clients[i], true | ||||
| 		} | ||||
| 	} | ||||
| 	return nil, false | ||||
| @ -219,12 +219,14 @@ func updateMetrics(state *models.State) { | ||||
| } | ||||
| 
 | ||||
| func updateDurations() { | ||||
| 	for _, agent := range lastState.Agents { | ||||
| 	for i := range lastState.Agents { | ||||
| 		agent := &lastState.Agents[i] | ||||
| 		agentDuration. | ||||
| 			With(prometheus.Labels{"agent_guid": agent.Guid}). | ||||
| 			Set(float64(time.Now().Sub(agent.StartTime).Seconds())) | ||||
| 	} | ||||
| 	for _, client := range lastState.Clients { | ||||
| 	for i := range lastState.Clients { | ||||
| 		client := &lastState.Clients[i] | ||||
| 		clientDuration. | ||||
| 			With(prometheus.Labels{"client_guid": client.Guid}). | ||||
| 			Set(float64(time.Now().Sub(client.StartTime).Seconds())) | ||||
| @ -232,43 +234,47 @@ func updateDurations() { | ||||
| } | ||||
| 
 | ||||
| func updateMetricsImpl(state *models.State) { | ||||
| 	agentGuids := make(map[string]models.Agent) | ||||
| 	clientGuids := make(map[string]models.Client) | ||||
| 	agentGuids := make(map[string]*models.Agent) | ||||
| 	clientGuids := make(map[string]*models.Client) | ||||
| 
 | ||||
| 	agentCount.Set(float64(len(state.Agents))) | ||||
| 	disconnectedAgents := make(map[string]models.Agent) | ||||
| 	for _, agent := range lastState.Agents { | ||||
| 	disconnectedAgents := make(map[string]*models.Agent) | ||||
| 	for i := range lastState.Agents { | ||||
| 		agent := &lastState.Agents[i] | ||||
| 		disconnectedAgents[agent.Guid] = agent | ||||
| 	} | ||||
| 	for _, agent := range state.Agents { | ||||
| 	for i := range state.Agents { | ||||
| 		agent := &state.Agents[i] | ||||
| 		if _, ok := findAgent(lastState, agent.Guid); !ok { | ||||
| 			cumulativeAgentCount.Inc() | ||||
| 		} | ||||
| 		delete(disconnectedAgents, agent.Guid) | ||||
| 		agentGuids[agent.Guid] = agent | ||||
| 		agentActive(&agent) | ||||
| 		agentActive(agent) | ||||
| 	} | ||||
| 	for _, agent := range disconnectedAgents { | ||||
| 		removeAgentMetrics(&agent) | ||||
| 		removeAgentMetrics(agent) | ||||
| 	} | ||||
| 
 | ||||
| 	clientCount.Set(float64(len(state.Clients))) | ||||
| 
 | ||||
| 	// with this app
 | ||||
| 	disconnectedClients := make(map[string]models.Client) | ||||
| 	for _, client := range lastState.Clients { | ||||
| 	disconnectedClients := make(map[string]*models.Client) | ||||
| 	for i := range lastState.Clients { | ||||
| 		client := &lastState.Clients[i] | ||||
| 		disconnectedClients[client.Guid] = client | ||||
| 	} | ||||
| 	for _, client := range state.Clients { | ||||
| 	for i := range state.Clients { | ||||
| 		client := &state.Clients[i] | ||||
| 		if _, ok := findClient(lastState, client.Guid); !ok { | ||||
| 			cumulativeClientCount.Inc() | ||||
| 		} | ||||
| 		delete(disconnectedClients, client.Guid) | ||||
| 		clientGuids[client.Guid] = client | ||||
| 		clientActive(&client) | ||||
| 		clientActive(client) | ||||
| 	} | ||||
| 	for _, client := range disconnectedClients { | ||||
| 		removeClientMetrics(&client) | ||||
| 		removeClientMetrics(client) | ||||
| 	} | ||||
| 
 | ||||
| 	lastState = state | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user