Skip to content

Commit

Permalink
Ensure maps for connection counts are created before use
Browse files Browse the repository at this point in the history
We know all the expected states, so can pre-create maps
  • Loading branch information
bboreham committed Jul 29, 2020
1 parent c6afdb7 commit 1a8fd8b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions prog/weaver/metrics.go
Expand Up @@ -46,17 +46,19 @@ func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) promethe
var metrics = []metric{
{desc("weave_connections", "Number of peer-to-peer connections.", "state", "type"),
func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
counts := make(map[string]map[string]int)
counts := make(map[ /*state*/ string]map[ /*type*/ string]int)
for _, state := range allConnectionStates {
counts[state] = make(map[string]int)
}
for _, conn := range s.Router.Connections {
t, ok := conn.Attrs["name"].(string)
if !ok {
// TODO: some kind of log statement here may be useful here?
t = "unknown"
typeName := "unknown"
if t, ok := conn.Attrs["name"]; ok {
typeName = t.(string)
}
counts[t][conn.State]++
counts[conn.State][typeName]++
}
for connType, stateCounts := range counts {
for state, count := range stateCounts {
for state, stateCounts := range counts {
for connType, count := range stateCounts {
ch <- intGauge(desc, count, state, connType)
}
}
Expand Down

0 comments on commit 1a8fd8b

Please sign in to comment.