|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
//go:build !consulent
|
|
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
|
|
package usagemetrics
|
|
|
|
@ -57,6 +58,11 @@ func TestUsageReporter_emitNodeUsage_OSS(t *testing.T) {
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
getMembersFunc: func() []serf.Member { return []serf.Member{} },
|
|
|
|
|
},
|
|
|
|
@ -114,6 +120,11 @@ func TestUsageReporter_emitNodeUsage_OSS(t *testing.T) {
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
@ -199,6 +210,11 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) {
|
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
getMembersFunc: func() []serf.Member { return []serf.Member{} },
|
|
|
|
|
},
|
|
|
|
@ -276,6 +292,11 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) {
|
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
@ -314,3 +335,156 @@ func TestUsageReporter_emitServiceUsage_OSS(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUsageReporter_emitKVUsage_OSS(t *testing.T) {
|
|
|
|
|
type testCase struct {
|
|
|
|
|
modfiyStateStore func(t *testing.T, s *state.Store)
|
|
|
|
|
getMembersFunc getMembersFunc
|
|
|
|
|
expectedGauges map[string]metrics.GaugeValue
|
|
|
|
|
}
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
|
"empty-state": {
|
|
|
|
|
expectedGauges: map[string]metrics.GaugeValue{
|
|
|
|
|
// --- node ---
|
|
|
|
|
"consul.usage.test.consul.state.nodes;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.nodes",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
// --- member ---
|
|
|
|
|
"consul.usage.test.consul.members.clients;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.members.clients",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.members.servers;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.members.servers",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
// --- service ---
|
|
|
|
|
"consul.usage.test.consul.state.services;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.services",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.service_instances;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.service_instances",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
getMembersFunc: func() []serf.Member { return []serf.Member{} },
|
|
|
|
|
},
|
|
|
|
|
"nodes": {
|
|
|
|
|
modfiyStateStore: func(t *testing.T, s *state.Store) {
|
|
|
|
|
require.NoError(t, s.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}))
|
|
|
|
|
require.NoError(t, s.EnsureNode(2, &structs.Node{Node: "bar", Address: "127.0.0.2"}))
|
|
|
|
|
require.NoError(t, s.EnsureNode(3, &structs.Node{Node: "baz", Address: "127.0.0.2"}))
|
|
|
|
|
|
|
|
|
|
require.NoError(t, s.KVSSet(4, &structs.DirEntry{Key: "a", Value: []byte{1}}))
|
|
|
|
|
require.NoError(t, s.KVSSet(5, &structs.DirEntry{Key: "b", Value: []byte{1}}))
|
|
|
|
|
require.NoError(t, s.KVSSet(6, &structs.DirEntry{Key: "c", Value: []byte{1}}))
|
|
|
|
|
require.NoError(t, s.KVSSet(7, &structs.DirEntry{Key: "d", Value: []byte{1}}))
|
|
|
|
|
require.NoError(t, s.KVSDelete(8, "d", &structs.EnterpriseMeta{}))
|
|
|
|
|
require.NoError(t, s.KVSDelete(9, "c", &structs.EnterpriseMeta{}))
|
|
|
|
|
require.NoError(t, s.KVSSet(10, &structs.DirEntry{Key: "e", Value: []byte{1}}))
|
|
|
|
|
require.NoError(t, s.KVSSet(11, &structs.DirEntry{Key: "f", Value: []byte{1}}))
|
|
|
|
|
},
|
|
|
|
|
getMembersFunc: func() []serf.Member {
|
|
|
|
|
return []serf.Member{
|
|
|
|
|
{
|
|
|
|
|
Name: "foo",
|
|
|
|
|
Tags: map[string]string{"role": "consul"},
|
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "bar",
|
|
|
|
|
Tags: map[string]string{"role": "consul"},
|
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "baz",
|
|
|
|
|
Tags: map[string]string{"role": "node"},
|
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
expectedGauges: map[string]metrics.GaugeValue{
|
|
|
|
|
// --- node ---
|
|
|
|
|
"consul.usage.test.consul.state.nodes;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.nodes",
|
|
|
|
|
Value: 3,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
// --- member ---
|
|
|
|
|
"consul.usage.test.consul.members.servers;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.members.servers",
|
|
|
|
|
Value: 2,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.members.clients;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.members.clients",
|
|
|
|
|
Value: 1,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
// --- service ---
|
|
|
|
|
"consul.usage.test.consul.state.services;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.services",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.service_instances;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.service_instances",
|
|
|
|
|
Value: 0,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
|
|
|
|
Value: 5,
|
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
// Only have a single interval for the test
|
|
|
|
|
sink := metrics.NewInmemSink(1*time.Minute, 1*time.Minute)
|
|
|
|
|
cfg := metrics.DefaultConfig("consul.usage.test")
|
|
|
|
|
cfg.EnableHostname = false
|
|
|
|
|
metrics.NewGlobal(cfg, sink)
|
|
|
|
|
|
|
|
|
|
mockStateProvider := &mockStateProvider{}
|
|
|
|
|
s, err := newStateStore()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
if tcase.modfiyStateStore != nil {
|
|
|
|
|
tcase.modfiyStateStore(t, s)
|
|
|
|
|
}
|
|
|
|
|
mockStateProvider.On("State").Return(s)
|
|
|
|
|
|
|
|
|
|
reporter, err := NewUsageMetricsReporter(
|
|
|
|
|
new(Config).
|
|
|
|
|
WithStateProvider(mockStateProvider).
|
|
|
|
|
WithLogger(testutil.Logger(t)).
|
|
|
|
|
WithDatacenter("dc1").
|
|
|
|
|
WithGetMembersFunc(tcase.getMembersFunc),
|
|
|
|
|
)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
reporter.runOnce()
|
|
|
|
|
|
|
|
|
|
intervals := sink.Data()
|
|
|
|
|
require.Len(t, intervals, 1)
|
|
|
|
|
intv := intervals[0]
|
|
|
|
|
|
|
|
|
|
assertEqualGaugeMaps(t, tcase.expectedGauges, intv.Gauges)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|