Add connect specific prefix to Usage table

Ensure that connect Kind's are separate from ConfigEntry Kind's to
prevent miscounting
pull/11222/head
Connor Kelly 2021-10-07 16:16:23 -05:00
parent 024715eb11
commit 533e7dbe85
No known key found for this signature in database
GPG Key ID: 588D34E7812BC0B7
3 changed files with 28 additions and 10 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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"},