From 2deb7e2e48ed4dd9e4173f2ed17516accdd4b80f Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Wed, 18 Mar 2020 14:31:08 -0400 Subject: [PATCH 1/4] Add new 'type' label to weave_connections metric Signed-off-by: Dave Henderson --- prog/weaver/metrics.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/prog/weaver/metrics.go b/prog/weaver/metrics.go index cb47b248..4487a852 100644 --- a/prog/weaver/metrics.go +++ b/prog/weaver/metrics.go @@ -44,14 +44,21 @@ func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) promethe } var metrics = []metric{ - {desc("weave_connections", "Number of peer-to-peer connections.", "state"), + {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]int) + counts := make(map[string]map[string]int) for _, conn := range s.Router.Connections { - counts[conn.State]++ + 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]++ } - for _, state := range allConnectionStates { - ch <- intGauge(desc, counts[state], state) + for connType, stateCounts := range counts { + for state, count := range stateCounts { + ch <- intGauge(desc, count, state, connType) + } } }}, {desc("weave_connection_terminations_total", "Number of peer-to-peer connections terminated."), From f6a3af4a100c509a469f4dd55b0cbf97e4be5baf Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Jul 2020 11:11:33 +0000 Subject: [PATCH 2/4] Add a check for correct metrics in smoke-test --- test/101_cross_hosts_2_test.sh | 1 + test/config.sh | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/test/101_cross_hosts_2_test.sh b/test/101_cross_hosts_2_test.sh index 70039b2b..bc345eaa 100755 --- a/test/101_cross_hosts_2_test.sh +++ b/test/101_cross_hosts_2_test.sh @@ -24,5 +24,6 @@ weave_on $HOST2 attach $C2/24 c2 wait_for_x network_tester_status "network tester status" assert "echo $status" "pass" +assert "connections_metric $HOST1 state=\\\"established\\\"" "1" end_suite diff --git a/test/config.sh b/test/config.sh index 29958867..fd239b10 100644 --- a/test/config.sh +++ b/test/config.sh @@ -267,6 +267,10 @@ wait_for_attached() { wait_for_x "check_attached $1 $2" "$2 on $1 to be attached" } +connections_metric() { + $SSH $1 curl -sS http://127.0.0.1:6784/metrics | awk -e "/^weave_connections.*$2/ { print \$2 }" +} + # assert_dns_record [ ...] assert_dns_record() { local host=$1 From 865e2b726f02e77cd465a58c8e7fedb16095d5e4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Jul 2020 13:47:56 +0000 Subject: [PATCH 3/4] 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) } } From 2cc5c635c9bc3d7bbbeeb3cf5b9a52b152de8e66 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Jul 2020 17:17:20 +0000 Subject: [PATCH 4/4] Add 'encrypted' label to connections metric Extend tests to check it shows up. Tests assume that metrics are published with metrics in alphabetical order, which appears to be the case for now. --- prog/weaver/metrics.go | 17 ++++++++++++----- test/101_cross_hosts_2_test.sh | 2 +- test/110_sleeve_encryption_2_test.sh | 1 + test/111_fastdp_encryption_2_test.sh | 1 + test/115_optional_encryption_2_test.sh | 2 ++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/prog/weaver/metrics.go b/prog/weaver/metrics.go index bba7dd9c..ed00bb53 100644 --- a/prog/weaver/metrics.go +++ b/prog/weaver/metrics.go @@ -44,22 +44,29 @@ func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) promethe } var metrics = []metric{ - {desc("weave_connections", "Number of peer-to-peer connections.", "state", "type"), + {desc("weave_connections", "Number of peer-to-peer connections.", "state", "type", "encryption"), func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) { - counts := make(map[ /*state*/ string]map[ /*type*/ string]int) + counts := make(map[ /*state*/ string]map[ /*type*/ string]struct{ encrypted, unencrypted int }) for _, state := range allConnectionStates { - counts[state] = make(map[string]int) + counts[state] = make(map[string]struct{ encrypted, unencrypted int }) } for _, conn := range s.Router.Connections { typeName := "unknown" if t, ok := conn.Attrs["name"]; ok { typeName = t.(string) } - counts[conn.State][typeName]++ + c := counts[conn.State][typeName] + if e, ok := conn.Attrs["encrypted"]; ok && e.(bool) { + c.encrypted++ + } else { + c.unencrypted++ + } + counts[conn.State][typeName] = c } for state, stateCounts := range counts { for connType, count := range stateCounts { - ch <- intGauge(desc, count, state, connType) + ch <- intGauge(desc, count.encrypted, state, connType, "yes") + ch <- intGauge(desc, count.unencrypted, state, connType, "") } } }}, diff --git a/test/101_cross_hosts_2_test.sh b/test/101_cross_hosts_2_test.sh index bc345eaa..a8cf580a 100755 --- a/test/101_cross_hosts_2_test.sh +++ b/test/101_cross_hosts_2_test.sh @@ -24,6 +24,6 @@ weave_on $HOST2 attach $C2/24 c2 wait_for_x network_tester_status "network tester status" assert "echo $status" "pass" -assert "connections_metric $HOST1 state=\\\"established\\\"" "1" +assert "connections_metric $HOST1 encryption=\\\"\\\",state=\\\"established\\\"" "1" end_suite diff --git a/test/110_sleeve_encryption_2_test.sh b/test/110_sleeve_encryption_2_test.sh index 13ebc161..05463fcd 100755 --- a/test/110_sleeve_encryption_2_test.sh +++ b/test/110_sleeve_encryption_2_test.sh @@ -16,5 +16,6 @@ assert_raises "exec_on $HOST1 c1 $PING $C2" assert_raises "weave_on $HOST1 status connections | grep -P 'encrypted *sleeve'" assert_raises "weave_on $HOST2 status connections | grep -P 'encrypted *sleeve'" +assert "connections_metric $HOST1 encryption=\\\"yes\\\"",state=\\\"established\\\" "1" end_suite diff --git a/test/111_fastdp_encryption_2_test.sh b/test/111_fastdp_encryption_2_test.sh index 4202af85..d56b7a4c 100755 --- a/test/111_fastdp_encryption_2_test.sh +++ b/test/111_fastdp_encryption_2_test.sh @@ -44,6 +44,7 @@ weave_on $HOST2 launch --password wfvAwt7sj $HOST1 assert_raises "weave_on $HOST1 status connections | grep -P 'encrypted *fastdp'" assert_raises "weave_on $HOST2 status connections | grep -P 'encrypted *fastdp'" +assert "connections_metric $HOST1 encryption=\\\"yes\\\"",state=\\\"established\\\" "1" PCAP1=$(mktemp) PCAP2=$(mktemp) diff --git a/test/115_optional_encryption_2_test.sh b/test/115_optional_encryption_2_test.sh index 89870b10..d7f47443 100755 --- a/test/115_optional_encryption_2_test.sh +++ b/test/115_optional_encryption_2_test.sh @@ -17,6 +17,7 @@ weave_on $HOST1 launch --password wfvAwt7sj --trusted-subnets $HOST2_CIDR weave_on $HOST2 launch --password wfvAwt7sj $HOST1 assert_raises "weave_on $HOST1 status connections | grep encrypted" assert_raises "weave_on $HOST2 status connections | grep encrypted" +assert "connections_metric $HOST1 encryption=\\\"yes\\\"",state=\\\"established\\\" "1" weave_on $HOST1 stop weave_on $HOST2 stop @@ -26,5 +27,6 @@ weave_on $HOST1 launch --password wfvAwt7sj --trusted-subnets $HOST2_CIDR weave_on $HOST2 launch --password wfvAwt7sj --trusted-subnets $HOST1_CIDR $HOST1 assert_raises "weave_on $HOST1 status connections | grep unencrypted" assert_raises "weave_on $HOST2 status connections | grep unencrypted" +assert "connections_metric $HOST1 encryption=\\\"\\\"",state=\\\"established\\\" "1" end_suite