From 533e7dbe851462a75af736e4ca3148141972a6cf Mon Sep 17 00:00:00 2001 From: Connor Kelly Date: Thu, 7 Oct 2021 16:16:23 -0500 Subject: [PATCH] Add connect specific prefix to Usage table Ensure that connect Kind's are separate from ConfigEntry Kind's to prevent miscounting --- agent/consul/state/usage.go | 20 ++++++++++++------- agent/consul/state/usage_test.go | 2 +- .../usagemetrics/usagemetrics_oss_test.go | 16 +++++++++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/agent/consul/state/usage.go b/agent/consul/state/usage.go index ab96abe471..607390e7c9 100644 --- a/agent/consul/state/usage.go +++ b/agent/consul/state/usage.go @@ -12,6 +12,7 @@ const ( serviceNamesUsageTable = "service-names" kvUsageTable = "kv-entries" connectNativeInstancesTable = "connect-native" + connectPrefix = "connect-mesh" tableUsage = "usage" ) @@ -202,6 +203,11 @@ func serviceNameChanged(change memdb.Change) bool { return false } +// connectTableEntry is a convenience function to make prefix addition in 1 place +func connectUsageTableName(kind string) string { + return fmt.Sprintf("%s-%s", connectPrefix, kind) +} + func connectDeltas(change memdb.Change, usageDeltas map[string]int, delta int) { // Connect metrics for updated services are more complicated. Check for: // 1. Did ServiceKind change? @@ -212,30 +218,30 @@ func connectDeltas(change memdb.Change, usageDeltas map[string]int, delta int) { before := change.Before.(*structs.ServiceNode) after := change.After.(*structs.ServiceNode) if before.ServiceKind != structs.ServiceKindTypical { - usageDeltas[string(before.ServiceKind)] -= 1 + usageDeltas[connectUsageTableName(string(before.ServiceKind))] -= 1 addEnterpriseConnectServiceInstanceUsage(usageDeltas, before, -1) } if after.ServiceKind != structs.ServiceKindTypical { - usageDeltas[string(after.ServiceKind)] += 1 + usageDeltas[connectUsageTableName(string(after.ServiceKind))] += 1 addEnterpriseConnectServiceInstanceUsage(usageDeltas, after, 1) } if before.ServiceConnect.Native != after.ServiceConnect.Native { if before.ServiceConnect.Native { - usageDeltas[connectNativeInstancesTable] -= 1 + usageDeltas[connectUsageTableName(string(connectNativeInstancesTable))] -= 1 addEnterpriseConnectServiceInstanceUsage(usageDeltas, before, -1) } else { - usageDeltas[connectNativeInstancesTable] += 1 + usageDeltas[connectUsageTableName(connectNativeInstancesTable)] += 1 addEnterpriseConnectServiceInstanceUsage(usageDeltas, after, 1) } } } else { svc := changeObject(change).(*structs.ServiceNode) if svc.ServiceKind != structs.ServiceKindTypical { - usageDeltas[string(svc.ServiceKind)] += delta + usageDeltas[connectUsageTableName(string(svc.ServiceKind))] += delta } if svc.ServiceConnect.Native { - usageDeltas[connectNativeInstancesTable] += delta + usageDeltas[connectUsageTableName(connectNativeInstancesTable)] += delta } addEnterpriseConnectServiceInstanceUsage(usageDeltas, svc, delta) } @@ -329,7 +335,7 @@ func (s *Store) ServiceUsage() (uint64, ServiceUsage, error) { serviceKindInstances := make(map[string]int) for _, kind := range allConnectKind { - usage, err := firstUsageEntry(tx, kind) + usage, err := firstUsageEntry(tx, connectUsageTableName(kind)) if err != nil { return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err) } diff --git a/agent/consul/state/usage_test.go b/agent/consul/state/usage_test.go index 69401bd14f..ce1ab890b2 100644 --- a/agent/consul/state/usage_test.go +++ b/agent/consul/state/usage_test.go @@ -232,7 +232,7 @@ func TestStateStore_Usage_updateUsage_Underflow(t *testing.T) { require.NoError(t, err) require.Equal(t, 0, u.(*UsageEntry).Count) - u, err = txn.First("usage", "id", string(structs.ServiceKindConnectProxy)) + u, err = txn.First("usage", "id", connectUsageTableName(string(structs.ServiceKindConnectProxy))) require.NoError(t, err) require.Equal(t, 0, u.(*UsageEntry).Count) diff --git a/agent/consul/usagemetrics/usagemetrics_oss_test.go b/agent/consul/usagemetrics/usagemetrics_oss_test.go index c3c2b4187c..50e41c88b1 100644 --- a/agent/consul/usagemetrics/usagemetrics_oss_test.go +++ b/agent/consul/usagemetrics/usagemetrics_oss_test.go @@ -584,6 +584,18 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) { require.NoError(t, s.EnsureService(11, "foo", mgw)) require.NoError(t, s.EnsureService(12, "foo", tgw)) require.NoError(t, s.EnsureService(13, "bar", &structs.NodeService{ID: "db-native", Service: "db", Tags: nil, Address: "", Port: 5000, Connect: structs.ServiceConnect{Native: true}})) + require.NoError(t, s.EnsureConfigEntry(14, &structs.IngressGatewayConfigEntry{ + Kind: structs.IngressGateway, + Name: "foo", + })) + require.NoError(t, s.EnsureConfigEntry(15, &structs.IngressGatewayConfigEntry{ + Kind: structs.IngressGateway, + Name: "bar", + })) + require.NoError(t, s.EnsureConfigEntry(16, &structs.IngressGatewayConfigEntry{ + Kind: structs.IngressGateway, + Name: "baz", + })) }, getMembersFunc: func() []serf.Member { return []serf.Member{ @@ -728,7 +740,7 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) { }, "consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": { Name: "consul.usage.test.consul.state.config_entries", - Value: 1, + Value: 3, Labels: []metrics.Label{ {Name: "datacenter", Value: "dc1"}, {Name: "kind", Value: "ingress-gateway"}, @@ -760,7 +772,7 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) { }, "consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": { Name: "consul.usage.test.consul.state.config_entries", - Value: 1, + Value: 0, Labels: []metrics.Label{ {Name: "datacenter", Value: "dc1"}, {Name: "kind", Value: "terminating-gateway"},