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"}},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"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"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-18 14:27:15 +00:00
|
|
|
},
|
|
|
|
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"}},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"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"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
2021-08-18 14:27:15 +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{}
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"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"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
|
2021-09-30 21:15:26 +00:00
|
|
|
mgw := structs.TestNodeServiceMeshGateway(t)
|
|
|
|
mgw.ID = "mesh-gateway"
|
|
|
|
|
|
|
|
tgw := structs.TestNodeServiceTerminatingGateway(t, "1.1.1.1")
|
|
|
|
tgw.ID = "terminating-gateway"
|
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-09-30 21:15:26 +00:00
|
|
|
require.NoError(t, s.EnsureService(9, "foo", &structs.NodeService{ID: "db-connect-proxy", Service: "db-connect-proxy", Tags: nil, Address: "", Port: 5000, Kind: structs.ServiceKindConnectProxy}))
|
|
|
|
require.NoError(t, s.EnsureRegistration(10, structs.TestRegisterIngressGateway(t)))
|
|
|
|
require.NoError(t, s.EnsureService(11, "foo", mgw))
|
|
|
|
require.NoError(t, s.EnsureService(12, "foo", tgw))
|
2021-09-30 21:48:21 +00:00
|
|
|
require.NoError(t, s.EnsureService(13, "bar", &structs.NodeService{ID: "db-native", Service: "db", Tags: nil, Address: "", Port: 5000, Connect: structs.ServiceConnect{Native: true}}))
|
2021-10-07 21:16:23 +00:00
|
|
|
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",
|
|
|
|
}))
|
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",
|
2021-09-30 21:15:26 +00:00
|
|
|
Value: 7,
|
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",
|
2021-09-30 21:48:21 +00:00
|
|
|
Value: 9,
|
2021-09-30 21:15:26 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 1,
|
2021-06-03 15:25:53 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
2021-09-30 21:15:26 +00:00
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
2021-06-03 15:25:53 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 1,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 1,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 1,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 1,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"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"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
2021-10-07 21:16:23 +00:00
|
|
|
Value: 3,
|
2021-10-01 18:22:30 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
2021-10-07 21:16:23 +00:00
|
|
|
Value: 0,
|
2021-10-01 18:22:30 +00:00
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 19:36:34 +00:00
|
|
|
|
|
|
|
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"}},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"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"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-17 19:36:34 +00:00
|
|
|
},
|
|
|
|
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"}},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- service mesh ---
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-proxy": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-proxy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=mesh-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:48:21 +00:00
|
|
|
"consul.usage.test.consul.state.connect_instances;datacenter=dc1;kind=connect-native": {
|
|
|
|
Name: "consul.usage.test.consul.state.connect_instances",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "connect-native"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-30 21:15:26 +00:00
|
|
|
// --- kv ---
|
2021-09-17 19:36:34 +00:00
|
|
|
"consul.usage.test.consul.state.kv_entries;datacenter=dc1": {
|
|
|
|
Name: "consul.usage.test.consul.state.kv_entries",
|
2021-09-20 18:44:43 +00:00
|
|
|
Value: 4,
|
2021-09-17 19:36:34 +00:00
|
|
|
Labels: []metrics.Label{{Name: "datacenter", Value: "dc1"}},
|
|
|
|
},
|
2021-10-01 18:22:30 +00:00
|
|
|
// --- config entries ---
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-intentions": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-intentions"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-resolver": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-resolver"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-router": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-router"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=ingress-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "ingress-gateway"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-splitter": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-splitter"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=mesh": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "mesh"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=proxy-defaults": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "proxy-defaults"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=terminating-gateway": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "terminating-gateway"},
|
|
|
|
},
|
|
|
|
},
|
2021-10-20 19:24:18 +00:00
|
|
|
"consul.usage.test.consul.state.config_entries;datacenter=dc1;kind=service-exports": {
|
|
|
|
Name: "consul.usage.test.consul.state.config_entries",
|
|
|
|
Value: 0,
|
|
|
|
Labels: []metrics.Label{
|
|
|
|
{Name: "datacenter", Value: "dc1"},
|
|
|
|
{Name: "kind", Value: "service-exports"},
|
|
|
|
},
|
|
|
|
},
|
2021-09-17 19:36:34 +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{}
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|