From 865e2b726f02e77cd465a58c8e7fedb16095d5e4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Jul 2020 13:47:56 +0000 Subject: [PATCH] Ensure maps for connection counts are created before use We know all the expected states, so can pre-create maps --- prog/weaver/metrics.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/prog/weaver/metrics.go b/prog/weaver/metrics.go index 4487a852..bba7dd9c 100644 --- a/prog/weaver/metrics.go +++ b/prog/weaver/metrics.go @@ -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) - 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" - } - counts[t][conn.State]++ + counts := make(map[ /*state*/ string]map[ /*type*/ string]int) + for _, state := range allConnectionStates { + counts[state] = make(map[string]int) } - for connType, stateCounts := range counts { - for state, count := range stateCounts { + for _, conn := range s.Router.Connections { + typeName := "unknown" + if t, ok := conn.Attrs["name"]; ok { + typeName = t.(string) + } + counts[conn.State][typeName]++ + } + for state, stateCounts := range counts { + for connType, count := range stateCounts { ch <- intGauge(desc, count, state, connType) } }