2020-09-02 15:24:17 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package usagemetrics
|
|
|
|
|
2020-10-09 16:01:45 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
2020-10-23 19:21:37 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
|
2020-10-09 16:01:45 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
2020-09-02 15:24:17 +00:00
|
|
|
|
2020-10-29 18:52:15 +00:00
|
|
|
func newStateStore() (*state.Store, error) {
|
|
|
|
return state.NewStateStore(nil), nil
|
2020-09-02 15:24:17 +00:00
|
|
|
}
|
2020-10-09 16:01:45 +00:00
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
func TestUsageReporter_emitNodeUsage_OSS(t *testing.T) {
|
2020-10-09 16:01:45 +00:00
|
|
|
type testCase struct {
|
|
|
|
modfiyStateStore func(t *testing.T, s *state.Store)
|
2021-06-03 15:25:53 +00:00
|
|
|
getMembersFunc getMembersFunc
|
2020-10-09 16:01:45 +00:00
|
|
|
expectedGauges map[string]metrics.GaugeValue
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"empty-state": {
|
|
|
|
expectedGauges: map[string]metrics.GaugeValue{
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- node ---
|
2020-10-09 16:01:45 +00:00
|
|
|
"consul.usage.test.consul.state.nodes;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.state.nodes",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- 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 ---
|
2020-10-09 16:01:45 +00:00
|
|
|
"consul.usage.test.consul.state.services;datacenter=dc1": {
|
2021-08-18 14:27:15 +00:00
|
|
|
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"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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"}))
|
|
|
|
},
|
|
|
|
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,
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
{
|
|
|
|
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"}},
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.service_instances;datacenter=dc1": {
|
2021-08-18 14:27:15 +00:00
|
|
|
Name: "consul.usage.test.consul.state.service_instances",
|
|
|
|
Value: 0,
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUsageReporter_emitServiceUsage_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.servers;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.members.servers",
|
2020-10-09 16:01:45 +00:00
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2021-06-03 15:25:53 +00:00
|
|
|
"consul.usage.test.consul.members.clients;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.members.clients",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- 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",
|
2021-06-03 15:25:53 +00:00
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
2021-06-03 15:25:53 +00:00
|
|
|
getMembersFunc: func() []serf.Member { return []serf.Member{} },
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
|
|
|
"nodes-and-services": {
|
|
|
|
modfiyStateStore: func(t *testing.T, s *state.Store) {
|
2021-08-18 14:27:15 +00:00
|
|
|
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.EnsureNode(4, &structs.Node{Node: "qux", Address: "127.0.0.3"}))
|
2020-10-09 16:01:45 +00:00
|
|
|
|
|
|
|
// Typical services and some consul services spread across two nodes
|
2021-08-18 14:27:15 +00:00
|
|
|
require.NoError(t, s.EnsureService(5, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: nil, Address: "", Port: 5000}))
|
|
|
|
require.NoError(t, s.EnsureService(6, "bar", &structs.NodeService{ID: "api", Service: "api", Tags: nil, Address: "", Port: 5000}))
|
|
|
|
require.NoError(t, s.EnsureService(7, "foo", &structs.NodeService{ID: "consul", Service: "consul", Tags: nil}))
|
|
|
|
require.NoError(t, s.EnsureService(8, "bar", &structs.NodeService{ID: "consul", Service: "consul", Tags: nil}))
|
2021-06-03 15:25:53 +00:00
|
|
|
},
|
|
|
|
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", "segment": "a"},
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "qux",
|
|
|
|
Tags: map[string]string{"role": "node", "segment": "b"},
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
},
|
|
|
|
}
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
|
|
|
expectedGauges: map[string]metrics.GaugeValue{
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- node ---
|
2020-10-09 16:01:45 +00:00
|
|
|
"consul.usage.test.consul.state.nodes;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.state.nodes",
|
2021-06-03 15:25:53 +00:00
|
|
|
Value: 4,
|
2020-10-09 16:01:45 +00:00
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- member ---
|
|
|
|
"consul.usage.test.consul.members.servers;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.members.servers",
|
|
|
|
Value: 2,
|
2020-10-09 16:01:45 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2021-06-03 15:25:53 +00:00
|
|
|
"consul.usage.test.consul.members.clients;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.members.clients",
|
|
|
|
Value: 2,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
// --- service ---
|
|
|
|
"consul.usage.test.consul.state.services;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.state.services",
|
|
|
|
Value: 3,
|
2021-06-03 15:25:53 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
"consul.usage.test.consul.state.service_instances;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.state.service_instances",
|
|
|
|
Value: 4,
|
2021-06-03 15:25:53 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
2020-10-09 16:01:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
2020-10-23 19:21:37 +00:00
|
|
|
s := state.NewStateStore(nil)
|
2020-10-09 16:01:45 +00:00
|
|
|
if tcase.modfiyStateStore != nil {
|
|
|
|
tcase.modfiyStateStore(t, s)
|
|
|
|
}
|
|
|
|
mockStateProvider.On("State").Return(s)
|
|
|
|
|
|
|
|
reporter, err := NewUsageMetricsReporter(
|
|
|
|
new(Config).
|
|
|
|
WithStateProvider(mockStateProvider).
|
|
|
|
WithLogger(testutil.Logger(t)).
|
2021-06-03 15:25:53 +00:00
|
|
|
WithDatacenter("dc1").
|
|
|
|
WithGetMembersFunc(tcase.getMembersFunc),
|
2020-10-09 16:01:45 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reporter.runOnce()
|
|
|
|
|
|
|
|
intervals := sink.Data()
|
|
|
|
require.Len(t, intervals, 1)
|
|
|
|
intv := intervals[0]
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
assertEqualGaugeMaps(t, tcase.expectedGauges, intv.Gauges)
|
2020-10-09 16:01:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|